httpclient上傳多個文件
Ⅰ 如何執行使用webapi HttpClient文件上傳多後
HttpClient c = new HttpClient();
var fileContent = new ByteArrayContent(new byte[100]);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "myFilename.txt"
};
var formData = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("name", "ali"),
new KeyValuePair<string, string>("title", "ostad")
});
MultipartContent content = new MultipartContent();
content.Add(formData);
content.Add(fileContent);
c.PostAsync(myUrl, content);
Ⅱ commons-httpclient如何實現文件上傳
在struts2中結合HttpClient進行文件上傳
最近遇到了用httpclient進行上傳文件的問題,下面我就和大家簡單的說一下:
package com.imps.action;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
public class TabinfoAction extends BaseAction
{
private File[] myFile;
private String[] myFileFileName;// 文件名
private Integer[] myFileFileSize;// 文件大小
private String[] myFileContentType;// 文件類型
public String uploadPost()
{
String posturl ="http://127.0.0.1:8080/settabimage.aspx";
System.out.println(posturl);
String status=null;
for(int i=0;i<myFile.length;i++)
{
FileInputStream file=new FileInputStream(myFile[i]);
InputStream in = new BufferedInputStream(file);
PostMethod post=new PostMethod(posturl);
post.setRequestBody(in);
HttpClient client = new HttpClient();
client.executeMethod(post);
String response=new String(post.getResponseBodyAsString().getBytes("ISO-8859-1"),"UTF-8");
post.releaseConnection();
in.close();
file.close();
}
}
public File[] getMyFile() {
return myFile;
}
public void setMyFile(File[] myFile) {
this.myFile = myFile;
}
public String[] getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String[] myFileFileName) {
this.myFileFileName = myFileFileName;
}
public Integer[] getMyFileFileSize() {
return myFileFileSize;
}
public void setMyFileFileSize(Integer[] myFileFileSize) {
this.myFileFileSize = myFileFileSize;
}
public String[] getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String[] myFileContentType) {
this.myFileContentType = myFileContentType;
}
千萬記住不要記忘記關閉流和釋放http連接
Ⅲ HTTPclient使用MultipartEntity怎麼上傳文件
你先搞清楚 HTTPclient 是做什麼用的
HTTPclient 的作用是在 jsp 中模擬一個瀏覽器,即 HTTP 協議的客戶端(client)
你的後台代碼是將你本地伺服器上的文件像瀏覽器那樣上傳到目標伺服器
於是 new File("C:\\1.txt") 的問題就可以解決了吧?C:\\1.txt 是你本地伺服器中的文件,當然文件名是你自己定的
至於 multipart/form-data 聲明,那是由 HttpPost 的參數 MultipartEntity 自動加上的
Ⅳ HTTPclient使用MultipartEntity怎麼上傳文件
上傳文檔方法:
進入網路文庫後,點「上傳我的文檔」,會出現一個對話框,選擇你想要上傳的文檔,再點擊「開始上傳」。
注意文檔有格式要求,上傳後等待審核,通過後即可與網友共享了。
上傳附件方法:
步驟一:展開回答框,找到「上傳」按鈕
步驟二:點擊「上傳」按鈕,從電腦中選擇要上傳的文件(目前只支持上傳1個文件哦~如果有多個文件,可以進行打包上傳~~)
步驟三:選中文件打開,顯示文件上傳進度。
步驟四:文件上傳成功,進度條顯示100%,可以對文件進行重命名操作~~
步驟五:回答編輯成功後,進入問題頁顯示,可供網友下載~~
Ⅳ 如何上傳多個文件asynchttpclient Android問題,怎麼解決
直接實例化多個file傳到params裡面配搜就可以了
RequestParams params = new RequestParams();
for (int i = 0; i < list.size(); i++) {
try {
params.put("培殲歷images["改叢 + i + "]", new File(list.get(i)));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Ⅵ android-async-http能上傳多個文件嗎
我這有個httpclient寫的工具攔首類
批量上傳的話,只需要傳遞一個FIle對象還可告衡薯以帶上傳進度條
服務端的話就簡單了
如果是struts的話,只要定義一個fileUpload的攔襪者截器,定義一個List<File> 對象就會自動注入了
如果是servlet的話,那就要用FileUpload + io 兩個架包,自己寫代碼去讀取文件了
我這有個客戶端,android寫的
Ⅶ httpclient 怎麼實現多文件上傳 c/s java
雖然在JDK的java.net包中已經提供了訪問HTTP協議的基本功能,但是對於大部分應用程序來說,JDK庫本身提供的功能還不夠豐富和靈活。HttpClient是ApacheJakartaCommon下的子項目,用來提供高效的、最新的、功能豐富的支持HTTP協議的客戶端編程工具包,並且它支持HTTP協議最新的版本和建議。以下是簡單的post例子:Stringurl="bbslogin2.php";PostMethodpostMethod=newPostMethod(url);//填入各個表單域的值NameValuePair[]data={newNameValuePair("id","youUserName"),newNameValuePair("passwd","yourPwd")};//將表單的值放入postMethod中postMethod.setRequestBody(data);//執行postMethodintstatusCode=httpClient.executeMethod(postMethod);//HttpClient對於要求接受後繼服務的請求,象POST和PUT等不能自動處理轉發//301或者302if(statusCode==HttpStatus.SC_MOVED_PERMANENTLY||statusCode==HttpStatus.SC_MOVED_TEMPORARILY){//從頭中取出轉向的地址HeaderlocationHeader=postMethod.getResponseHeader("location");Stringlocation=null;if(locationHeader!=null){location=locationHeader.getValue();System.out.println("Thepagewasredirectedto:"+location);}else{System.err.println("Locationfieldvalueisnull.");}return;}詳情見:/developerworks/cn/opensource/os-httpclient/
Ⅷ [FE] 用 FormData 上傳多個文件到 MultipartFile[] 介面
最近有一個場景,在提交表單的時候,需要實現添加附件的功能,
表單內容要先提交到服務端,創建一個 issue,然後再將附件添加到這個 issue 中。
所以,附件在用戶添加的時候,是 沒有立即上傳 的,
用戶可以隨意在瀏覽器端添加和刪除,issue 創建後再一起上傳。
前端採用的組件庫是 antd ,用到了 upload 組件。
服務端介面是自定義實現的,也許並不支持 antd upload 上傳組件的規范。
服務端接受數據時,使用了 MultipartFile ,這是 Spring 框架中常用的 寫法 。
我們先看看 html input[type=file] 組件默認行為,
點擊 「選擇文件」,瀏覽器會彈出一個窗口,
選中一個文件,點 「打開」,就會觸發 onchange 事件,
在 onchange 事件中,可以通過 e.target.files[0] 拿到剛才上傳的那個 File 對象 。
再來看一下 upload 組件的默認行為,
點擊 「添加」,瀏覽器也會彈出那個選擇文件的窗口,
選中一個文件,點 「打開」,發現上傳失敗了。
打開控制台,看到 upload 組件向 / 這個地址發送了一個 POST 請求,
數據格式如下,
我們可以向 upload 組件傳入 action 參數,修改 POST 請求地址,
但是,選中文件後立即上傳 不符合 我們的場景,我們需要提交表單之後,將多個文件統一上傳。
所以我們得自定義 upload 組件的行為。
upload 組件的有一個 customRequest 屬性( #api ),
它可以配置自定義的上傳行為。
我們的思路是,先將選中後自動上傳的行為取消掉,然後再在提交表單後統一上傳。
取消自動上傳 的實現片段如下,
我們只需要在 customRequest 回調中,調用它的 onSuccess 參數即可。
刪除也是可以用的,
現在我們添加兩個附件,
接著來看前端怎樣將這些附件,統一上傳給服務端,具體實現如下,
可以看到請求成功了(項目中的 url 跟本例稍有不同,下圖只為了示意),
還有幾個需要注意的點:
上文 httpClient.post 實際調用了 XMLHttpRequest 發送請求,可能會遇到 跨域 的問題。
所以在調試上傳介面的時候,需要檢查一下服務端的配置,是否支持跨域請求。
CORS 相關的內容大致如下:
在預檢請求階段,服務端對 OPTIONS 請求的響應頭中會包含 Access-Control-Allow-Origin ,
表明服務端接受該域 http://foo.example 的跨域請求。
註:
這里需要後端實現 OPTIONS 方法,後端框架一般會通過配置方式統一處理(返回 200 或 204,不能是 4xx)。
如果未配置統一處理方式,框架可能會直接返回 404 導致預檢請求失敗,CORS 請求也會失敗。
使用 XMLHttpRequest 發送請求時,也可以攜帶 cookie 信息,
同時 預檢請求中服務端響應頭,也要包含 Access-Control-Allow-Credentials ,否則就不會發送 cookie
對於附帶 cookie 的請求,伺服器不能設置 Access-Control-Allow-Origin 的值為 「 * 」,否則請求將會失敗。
而將 Access-Control-Allow-Origin 的值設置為具體的地址 http://foo.example ,請求才能成功。
我們上傳功能用到了攜帶 cookie 的跨域請求,
可以看到服務端響應頭中確實包含了, Access-Control-Allow-Credentials 和 Access-Control-Allow-Origin 兩個欄位。
Spring: Uploading Files
Spring: org.springframework.web.multipart #MultipartFile
ant-design v4.11.1
Ant Design - Upload #API
MDN: CORS
Ⅸ android的自帶的httpClient 怎麼上傳文件
Android上傳文件到服務端可以使用HttpConnection 上傳文件,也可以使用Android封裝好的HttpClient類。當僅僅上傳文件可以直接使用httpconnection 上傳比較方便快捷。
1、使用HttpConection上傳文件。將文件轉換成表單數據流。主要的思路就自己構造個http協議內容,服務端解析報文獲得表單數據。代碼片段:
[java] view plain
HttpURLConnection con;
try {
con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(C_TimeOut);
/* 允許Input、Output,不使用Cache */
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 設置傳送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
/* 設置DataOutputStream */
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
FileInputStream fStream = new FileInputStream(file);
/* 設置每次寫入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
/* 從文件讀取數據至緩沖區 */
while((length = fStream.read(buffer)) != -1)
{
/* 將資料寫入DataOutputStream中 */
ds.write(buffer, 0, length);
}
fStream.close();
ds.flush();
ds.close();
可以參考
①《在 Android 上通過模擬 HTTP multipart/form-data 請求協議信息實現圖片上傳》 (http://bertlee.iteye.com/blog/1134576)。
②《關於android Http訪問,上傳,用了三個方法 》
2、使用Android HttpClient類上傳參數。下面我在網上搜到得代碼,忘記出處了
[java] view plain
private static boolean sendPOSTRequestHttpClient(String path,
Map<String, String> params) throws Exception {
// 封裝請求參數
List<NameValuePair> pair = new ArrayList<NameValuePair>();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
pair.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
}
// 把請求參數變成請求體部分
UrlEncodedFormEntity uee = new UrlEncodedFormEntity(pair, "utf-8");
// 使用HttpPost對象設置發送的URL路徑
HttpPost post = new HttpPost(path);
// 發送請求體
post.setEntity(uee);
// 創建一個瀏覽器對象,以把POST對象向伺服器發送,並返回響應消息
DefaultHttpClient dhc = new DefaultHttpClient();
HttpResponse response = dhc.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
Log.i("http", "httpclient");
return true;
}
return false;
}
3、使用httpClient上傳文字信息和文件信息。使用httpClient上傳文件非常的方便。不過需要導入apache-mime4j-0.6.jar 和httpmime-4.0.jar兩個.jar包。
[java] view plain
// 封裝請求參數
MultipartEntity mpEntity = new MultipartEntity();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
StringBody par = new StringBody(entry.getValue());
mpEntity.addPart(entry.getKey(), par);
}
}
// 圖片
if (!imagepath.equals("")) {
FileBody file = new FileBody(new File(imagepath));
mpEntity.addPart("photo", file);
}
// 使用HttpPost對象設置發送的URL路徑
HttpPost post = new HttpPost(path);
// 發送請求體
post.setEntity(mpEntity);
// 創建一個瀏覽器對象,以把POST對象向伺服器發送,並返回響應消息
DefaultHttpClient dhc = new DefaultHttpClient();
HttpResponse response = dhc.execute(post);
FileBody類可以把文件封裝到表單中,實現附件的上傳。
關於httpClient上傳文件可以參考鏈接: http://www.eoeandroid.com/forum.php?mod=viewthread&tid=76721&page=1
需要用的的ja下載地址r:http://download.csdn.net/detail/china1988s/3791514
參考:
①《在 Android 上通過模擬 HTTP multipart/form-data 請求協議信息實現圖片上傳》 (http://bertlee.iteye.com/blog/1134576)。
②《關於android Http訪問,上傳,用了三個方法 》
Ⅹ apache httpclient4怎麼實現同時上傳多個文件
1.伺服器認證(Server Authentication)
HttpClient處理伺服器認證幾乎是透明的,僅需要開發人員提供登錄信息(login credentials)。登錄信息保存在HttpState類的實例中,可以通過 setCredentials(String realm, Credentials cred)和getCredentials(String realm)來獲取或設置。
HttpClient內建的自動認證,可以通過HttpMethod類的setDoAuthentication(boolean doAuthentication)方法關閉,而且這次關閉隻影響HttpMethod當前的實例。
2.代理認證(proxy authentication)
除了登錄信息需單獨存放以外,代理認證與伺服器認證幾乎一致。用 setProxyCredentials(String realm, Credentials cred)和 getProxyCredentials(String realm)設、取登錄信息。
3.認證方案(authentication schemes)
是HTTP中規定最早的也是最兼容的方案,遺憾的是也是最不安全的一個方案,因為它以明碼傳送用戶名和密碼。它要求一個UsernamePasswordCredentials實例,可以指定伺服器端的訪問空間或採用默認的登錄信息。