本文講述的是用httpclient通過http協定通路網頁上傳文檔的執行個體。伺服器端的就不用多說了,和一般的上傳是一樣的。下面就講下客戶段。廢話就不多說了,代碼見分曉。
寫代碼前準備:
httpclient 3.1 官網下載下傳
commons-codec-1.4 官網下載下傳
commons-logging-1.1.1 官網下載下傳
開始編寫代碼:
package my;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.Properties;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.MultipartPostMethod;
public class Upload {
public static void main(String[] args) {
String message="";
String filepath="";
String url="";
Properties properties = new Properties();
try {
//加載配置檔案
properties.load(new FileInputStream("config.properties"));
url=properties.getProperty("url").trim();
filepath=properties.getProperty("filepath").trim();
properties.clone();
} catch (FileNotFoundException e) {
message="/t找不到配置檔案(config.properties)";
} catch (IOException e) {
message="/t檔案讀取出現異常(config.properties)";
}
if("".equals(message)){
File file=new File(filepath);
if(file.length()>0){
HttpClient client=new HttpClient();
MultipartPostMethod mPost = new MultipartPostMethod(url);
try {
//為什麼沒設定編碼格式呢?後面幫你解答
//添加檔案
mPost.addParameter("upload", file);
} catch (FileNotFoundException e) {
message="/t"+filepath+"/t找不到上傳檔案";
}
//檔案名稱
mPost.addParameter("uploadFileName", file.getName());
int state=0;
if("".equals(message)){
try {
//執行
System.out.println(url);
state=client.executeMethod(mPost);
mPost.releaseConnection();
} catch (HttpException e) {
message="/t"+filepath+" "+String.format("%.2f",file.length()/1024.0f )+"KB/t找不到上傳檔案/t"+url+"/t檔案上傳出現異常";
} catch (IOException e) {
message="/t"+url+"/t連接配接不存在";
}
}
if("".equals(message)){
switch (state) {
case 200:
message="/t"+filepath+" "+String.format("%.2f",file.length()/1024.0f )+"KB/t"+url+"/t檔案上傳成功";
break;
case 404:
message="/t"+filepath+" "+String.format("%.2f",file.length()/1024.0f )+"KB/t"+url+"/t未找到";
break;
case 408:
message="/t"+filepath+" "+String.format("%.2f",file.length()/1024.0f )+"KB/t"+url+"/t連結逾時";
break;
default:
message="/t"+filepath+" "+String.format("%.2f",file.length()/1024.0f )+"KB/t"+url+"/t上傳失敗";
break;
}
}
}else{
message="/t"+filepath+"/t上傳檔案不存在或為空";
file=null;
}
}
try {
//寫日志
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("upload.log", true)));
out.write(new Date().toLocaleString()+message);
out.newLine();
out.flush();
out.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
}
config.properties
#待上傳檔案的位址
filepath=D:/文網文我.txt
#上傳位址
url=http://127.0.0.1:9080/tdcreport/tsc.action
關于httpclient亂碼問題我改寫了源碼,下載下傳位址
改寫的地方
org.apache.commons.httpclient.util.EncodingUtil
public static byte[] getAsciiBytes(final String data) {
if (data == null) {
throw new IllegalArgumentException("Parameter may not be null");
}
try {
return data.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new HttpClientError("HttpClient requires ASCII support");
}
}
org.apache.commons.httpclient.methods.multipart.StringPart
public StringPart(String name, String value, String charset) {
super(
name,
DEFAULT_CONTENT_TYPE,
charset == null ? "UTF-8" : charset,
DEFAULT_TRANSFER_ENCODING
);
if (value == null) {
throw new IllegalArgumentException("Value may not be null");
}
if (value.indexOf(0) != -1) {
// See RFC 2048, 2.8. "8bit Data"
throw new IllegalArgumentException("NULs may not be present in string parts");
}
this.value = value;
}
org.apache.commons.httpclient.methods.multipart.FilePart
public FilePart(String name, PartSource partSource, String contentType, String charset) {
super(
name,
contentType == null ? DEFAULT_CONTENT_TYPE : contentType,
charset == null ? "UTF-8" : charset,
DEFAULT_TRANSFER_ENCODING
);
if (partSource == null) {
throw new IllegalArgumentException("Source may not be null");
}
this.source = partSource;
}