原文地址:http://hyacinth.blog.sohu.com/187223085.html
httpclient上传文件及传参数
2011-10-12 14:25 阅读(2497)评论(0)
用到的包有commons-httpclient-3.0.1.jar、commons-codec-1.3.jar
客户端代码:
import java.io.File;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;
public class HttpClientDemo {
public static void main(String arg[]){
String targetURL = null;// TODO 指定URL
File targetFile = null;// TODO 指定上传文件
targetFile = new File("c:\\tzh.txt");
targetURL = "http://localhost:8080/nel/uploadservice";
PostMethod filePost = new PostMethod(targetURL);
// filePost.setRequestHeader("Content-type", "multipart/form-data");
try
{
Part[] parts = {
new FilePart(targetFile.getName(), targetFile),
new StringPart( "filepath" , "/data/2011-08-08/"),
new StringPart( "machinename" , "machine1")
};
filePost.setRequestEntity(new MultipartRequestEntity(parts,filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if (status == HttpStatus.SC_OK)
{
System.out.println("上传成功");
// 上传成功
}
else
{
System.out.println("上传失败");
// 上传失败
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
filePost.releaseConnection();
}
}
}
服务器端代码:
package cn.vlabs.nel.ui.uploadmanage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import cn.vlabs.app.common.tools.CommonTools;
import cn.vlabs.nel.service.clb.CLBFileStorage;
/**
* @author
[email protected]
* 2011-10-11
*/
public class UploadServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
try{
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 缓冲区大小4kb
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(419430400); // 最大文件尺寸是400MB
List items = upload.parseRequest(request);// 得到所有的文件
Iterator i = items.iterator();
String[] restr = null;
while (i.hasNext()){
FileItem fi = (FileItem) i.next();
if(fi.isFormField()){
String strname = fi.getFieldName();
String value = fi.getString();
System.out.println(strname+"="+value);
}else{
String fileName = fi.getName();
InputStream in=fi.getInputStream();
CLBFileStorage clbFileStorage=(CLBFileStorage) CommonTools.getBeanFactory().getBean("clbFileStorage");
int rei= clbFileStorage.createFile(fileName, fi.getSize(), in);
System.out.println(rei);
}
}
}
catch (Exception e){
System.out.println(e.getMessage());
// 可以跳转出错页面
e.printStackTrace();
}
}
}