當前位置:首頁 » 文件管理 » chttpclient文件上傳

chttpclient文件上傳

發布時間: 2022-02-24 22:48:14

1. 如何執行使用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);

2. httpclient上傳文件時,對文件的大小有上限嗎

不清楚, 幫頂,我覺得這個不是關鍵性問題,總有上限的!肯定需要在上傳前檢查一下size type 等。

3. 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連接

4. 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實例,可以指定伺服器端的訪問空間或採用默認的登錄信息。

5. 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/

6. httpclient如何一起上傳內容和圖片

以文件的形式傳參
/**
* 通過拼接的方式構造請求內容,實現參數傳輸以及文件傳輸
*
* @param actionUrl 訪問的伺服器URL
* @param params 普通參數
* @param files 文件參數
* @return
* @throws IOException
*/
public static void post(String actionUrl, Map<String, String> params, Map<String, File> files) throws IOException
{

String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";

URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000); // 緩存的最長時間
conn.setDoInput(true);// 允許輸入
conn.setDoOutput(true);// 允許輸出
conn.setUseCaches(false); // 不允許使用緩存
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);

// 首先組拼文本類型的參數
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet())
{
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
}

DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(sb.toString().getBytes());
InputStream in = null;
// 發送文件數據
if (files != null)
{
for (Map.Entry<String, File> file : files.entrySet())
{
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
// name是post中傳參的鍵 filename是文件的名稱
sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getKey() + "\"" + LINEND);
sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());

InputStream is = new FileInputStream(file.getValue());
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1)
{
outStream.write(buffer, 0, len);
}

is.close();
outStream.write(LINEND.getBytes());
}

// 請求結束標志
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush();
// 得到響應碼
int res = conn.getResponseCode();
if (res == 200)
{
in = conn.getInputStream();
int ch;
StringBuilder sb2 = new StringBuilder();
while ((ch = in.read()) != -1)
{
sb2.append((char) ch);
}
}
outStream.close();
conn.disconnect();
}
// return in.toString();
}

以數據流的形式傳參
public static String postFile(String actionUrl, Map<String, String> params, Map<String, byte[]> files)
throws Exception
{
StringBuilder sb2 = null;
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";

URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(6 * 1000); // 緩存的最長時間
conn.setDoInput(true);// 允許輸入
conn.setDoOutput(true);// 允許輸出
conn.setUseCaches(false); // 不允許使用緩存
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);

// 首先組拼文本類型的參數
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet())
{
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINEND);
sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND);
sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND);
sb.append("Content-Transfer-Encoding: 8bit" + LINEND);
sb.append(LINEND);
sb.append(entry.getValue());
sb.append(LINEND);
}

DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
outStream.write(sb.toString().getBytes());
InputStream in = null;
// 發送文件數據
if (files != null)
{
for (Map.Entry<String, byte[]> file : files.entrySet())
{
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition: form-data; name=\"pic\"; filename=\"" + file.getKey() + "\"" + LINEND);
sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());

// InputStream is = new FileInputStream(file.getValue());
// byte[] buffer = new byte[1024];
// int len = 0;
// while ((len = is.read(buffer)) != -1)
// {
// outStream.write(buffer, 0, len);
// }
// is.close();
outStream.write(file.getValue());

outStream.write(LINEND.getBytes());
}

// 請求結束標志
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush();
// 得到響應碼
int res = conn.getResponseCode();
if (res == 200)
{
in = conn.getInputStream();
int ch;
sb2 = new StringBuilder();
while ((ch = in.read()) != -1)
{
sb2.append((char) ch);
}
System.out.println(sb2.toString());
}
outStream.close();
conn.disconnect();
// 解析伺服器返回來的數據
return ParseJson.getEditMadIconResult(sb2.toString());
}
else
{
return "Update icon Fail";
}
// return in.toString();
}

7. 使用httpclient實現文件的上傳文件需要哪些包

題主是說哪個版本呢

其實 在apache官網中有下載、也有例子

~~~~~~~~~~~~~~~~~~~~~~~~~~

8. android的自帶的httpClient 怎麼上傳文件

在Android開發中,Android SDK附帶了Apache的HttpClient,它是一個完善的客戶端。它提供了對HTTP協議的全面支持,可以使用HttpClient的對象來執行HTTP GET和HTTP POST調用。

HTTP工作原理:

1.客戶端(一般是指瀏覽器,這里是指自己寫的程序)與伺服器建立連接

2.建立連接後,客戶端向伺服器發送請求

3.伺服器接收到請求後,向客戶端發送響應信息

4.客戶端與伺服器斷開連接


HttpClient的一般使用步驟:

1.使用DefaultHttpClient類實例化HttpClient對象

2.創建HttpGet或HttpPost對象,將要請求的URL通過構造方法傳入HttpGet或HttpPost對象。

3.調用execute方法發送HTTP GET或HTTP POST請求,並返回HttpResponse對象。

4.通過HttpResponse介面的getEntity方法返回響應信息,並進行相應的處理。

最後記得要在AndroidManifest.xml文件添加網路許可權

<uses-permission android:name="android.permission.INTERNET" />


附件中包含了一個拍照上傳的源代碼


9. HTTPclient使用MultipartEntity怎麼上傳文件

上傳文檔方法:
進入網路文庫後,點「上傳我的文檔」,會出現一個對話框,選擇你想要上傳的文檔,再點擊「開始上傳」。
注意文檔有格式要求,上傳後等待審核,通過後即可與網友共享了。

上傳附件方法:
步驟一:展開回答框,找到「上傳」按鈕
步驟二:點擊「上傳」按鈕,從電腦中選擇要上傳的文件(目前只支持上傳1個文件哦~如果有多個文件,可以進行打包上傳~~)

步驟三:選中文件打開,顯示文件上傳進度。
步驟四:文件上傳成功,進度條顯示100%,可以對文件進行重命名操作~~
步驟五:回答編輯成功後,進入問題頁顯示,可供網友下載~~

10. 如何接收httpclient 文件上傳

一般的情況下我們都是使用Chrome或者其他瀏覽器來訪問一個WEB伺服器,用來瀏覽頁面查看信息或者提交一些數據、文件上傳下載等等。所訪問的這些頁面有的僅僅是一些普通的頁面,有的需要用戶登錄後方可使用,或者需要認證以及是一些通過加密方式傳輸,例如H

熱點內容
eds伺服器是什麼 發布:2024-10-27 16:02:39 瀏覽:1
mysqlpython連接池 發布:2024-10-27 16:02:35 瀏覽:180
四川阿里雲伺服器選哪裡 發布:2024-10-27 15:55:18 瀏覽:700
soaphp 發布:2024-10-27 15:54:21 瀏覽:218
我的世界暑期紅石伺服器 發布:2024-10-27 15:54:11 瀏覽:958
台灣ip代理伺服器雲伺服器 發布:2024-10-27 15:53:21 瀏覽:645
app編譯原理 發布:2024-10-27 15:52:20 瀏覽:105
qq留言的訪問記錄 發布:2024-10-27 15:49:41 瀏覽:877
androidtextview點擊事件 發布:2024-10-27 15:42:24 瀏覽:692
安卓全民k歌怎麼唱 發布:2024-10-27 15:23:51 瀏覽:964