利用HttpClient进行http文件上传
2012-12-07 01:23
375 查看
1. java.lang.NoClassDefFoundError: org/apache/commons/codec/DecoderException 的解决办法
commons-codec-1.4-bin.zip
应用程序部分:
注意:要载入commons-httpclient-3.1.jar commons-codec.jar commons-logging.jar这三个包
import java.io.File;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
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;
public class Hclient
{
public static void main(String args[])
{
String targetURL = null;// TODO 指定URL
File targetFile = null;// TODO 指定上传文件
targetFile = new File("1.mp3");
targetURL = "http://localhost:8080/test/tt"; //servleturl
PostMethod filePost = new PostMethod(targetURL); //若没有commons-codec-1.4-bin.zip, 这里将会出错
try
{
//通过以下方法可以模拟页面参数提交
//filePost.setParameter("name", "中文");
//filePost.setParameter("pass", "1234");
Part[] parts = { new FilePart(targetFile.getName(), targetFile) };
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();
}
}
}
servlet部分:
注意:需求载入commons-fileupload-1.2.1.jar commons-io.jar这两个包
package servlet;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
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;
public class TestServlets extends HttpServlet
{
private String uploadPath = "D:\\temp"; // 上传文件的目录
private String tempPath = "d:\\temp\\buffer\\"; // 临时文件目录
File tempPathFile;
public void init() throws ServletException {
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
File tempPathFile = new File(tempPath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
Iterator<FileItem> i = items.iterator();
while (i.hasNext())
{
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null)
{
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
System.out.print("upload succeed");
}
catch (Exception e)
{
System.out.println(e.getMessage());
// 可以跳转出错页面
e.printStackTrace();
}
}
}
在web.xml中添加映射,就可以实现应用程序上传文件到servlet的功能了。
下面是一个例子的war文件:
fileUpload.zip
解压之后会先得到一个war,密码FileUpload123
转载地址: http://www.cnblogs.com/zhangxz/archive/2010/09/06/1819333.html
感谢作者终于找到一片关于上传
用cacuts做单元测试servlet时候总报这个错误,原来是bin文件缺少了要一个包commons-codec-1.x.jar 网络编程HttpClient 要一个包commons-codec-1.x.jar HttpClient 用到了 Apache Jakarta common 下的子项目 codec,你可以从这个地址http://commons.apache.org/downloads/download_codec.cgi 下载到最新的 common codec,从下载后的压缩包中取出 commons-codec-1.x.jar 加到 CLASSPATH 中 |
应用程序部分:
注意:要载入commons-httpclient-3.1.jar commons-codec.jar commons-logging.jar这三个包
import java.io.File;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
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;
public class Hclient
{
public static void main(String args[])
{
String targetURL = null;// TODO 指定URL
File targetFile = null;// TODO 指定上传文件
targetFile = new File("1.mp3");
targetURL = "http://localhost:8080/test/tt"; //servleturl
PostMethod filePost = new PostMethod(targetURL); //若没有commons-codec-1.4-bin.zip, 这里将会出错
try
{
//通过以下方法可以模拟页面参数提交
//filePost.setParameter("name", "中文");
//filePost.setParameter("pass", "1234");
Part[] parts = { new FilePart(targetFile.getName(), targetFile) };
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();
}
}
}
servlet部分:
注意:需求载入commons-fileupload-1.2.1.jar commons-io.jar这两个包
package servlet;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
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;
public class TestServlets extends HttpServlet
{
private String uploadPath = "D:\\temp"; // 上传文件的目录
private String tempPath = "d:\\temp\\buffer\\"; // 临时文件目录
File tempPathFile;
public void init() throws ServletException {
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
File tempPathFile = new File(tempPath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件
Iterator<FileItem> i = items.iterator();
while (i.hasNext())
{
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null)
{
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
System.out.print("upload succeed");
}
catch (Exception e)
{
System.out.println(e.getMessage());
// 可以跳转出错页面
e.printStackTrace();
}
}
}
在web.xml中添加映射,就可以实现应用程序上传文件到servlet的功能了。
下面是一个例子的war文件:
fileUpload.zip
解压之后会先得到一个war,密码FileUpload123
转载地址: http://www.cnblogs.com/zhangxz/archive/2010/09/06/1819333.html
感谢作者终于找到一片关于上传
相关文章推荐
- 利用HttpClient进行http文件上传
- 利用HttpClient进行http文件上传
- 利用HttpClient进行http文件上传
- 利用HttpClient进行带参数的http文件上传
- 利用loopj的android-async-http进行文件上传
- 安卓使用HttpClient进行Http Post文件上传
- httpclient通过POST来上传文件,而不是通过流的形式,并在服务端进行解析(通过httpmime.jar来操作)
- 【自动化测试】利用Jmeter对Http接口的文件上传进行测试
- (八)利用apache组件进行文件上传下载
- 利用XMLHttpRequest实现文件上传
- Android笔记(三):整理——利用http上传小文件
- asp.net利用HttpWorkerRequest上传大文件
- 使用HttpClient进行POST和GET请求以及文件上传和下载
- 利用HttpWebRequest上传文件
- Java利用HttpURLConnection发送post请求上传文件
- 利用socket模拟http的混合表单上传(在一个请求中提交表单并上传多个文件)
- 利用HttpUrlConnection 上传 接收文件方法
- 利用HttpURLConnection发送post请求上传文件
- 使用HttpUrlConnection进行post请求上传文件
- jsp中利用cos进行web文件上传(修改版)