HttpClient 3.1 文件上传
来源:互联网 发布:c4d r17 mac 编辑:程序博客网 时间:2024/05/31 13:14
客户端:
需要commons-codec,commons-httpclient-3.1,commons-logging-1.1.1
String targetURL = null;// TODO 指定URL File targetFile = null;// TODO 指定上传文件 targetFile = new File("C:\\Users\\Administrator\\Desktop\\1.mp3"); targetURL = "http://localhost:8080/HttpClientDemo/test"; //servleturl PostMethod filePost = new PostMethod(targetURL); 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(10000); 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(); }
服务器端:
需要commons-fileupload-1.2.1.jar commons-io.jar
public class HttpClientTest 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(); } }//@Override//protected void service(HttpServletRequest request, HttpServletResponse response)//throws ServletException, IOException {//////String name=request.getParameter("name");////System.out.println("name: "+name);////request.getRequestDispatcher("index.jsp").forward(request, response);////////}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ try { System.out.println("dopost"); // 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(10*1024*1024); // 设置最大文件尺寸,这里是10MB List<FileItem> items = upload.parseRequest(request);// 得到所有的文件 Iterator<FileItem> i = items.iterator(); while (i.hasNext()) { FileItem fi = (FileItem) i.next(); String fileName = fi.getName(); System.out.println(fileName); 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(); }}}
- HttpClient 3.1 文件上传
- httpclient 上传文件
- httpclient上传文件
- httpclient 上传文件
- httpclient 上传文件
- HttpClient 4 - 文件上传
- common-httpclient上传文件
- android httpclient 上传文件
- Httpclient上传文件
- 使用httpclient上传文件
- HttpClient 4 - 文件上传
- httpclient 上传文件
- HttpClient上传文件
- HttpClient文件上传
- httpclient上传文件
- HttpClient 4 - 文件上传
- HttpClient 上传文件
- HttpClient 文件上传
- JAVA基础之理解JNI原理
- 动态规划法解决装载问题
- 软件测试风险分析
- LPC2103学习之外部中断
- Android生成缩略图-使用Android 2.2新特性完成
- HttpClient 3.1 文件上传
- 资治通鉴,卷一百二十六,刘宋文帝刘义隆最后时光
- Win8探索学习笔记(2)基于JS的metro程序基础
- 逆向序列号生成算法(三)
- Win32ASM学习[12]:位测试指令位扫描指令
- 无符号翻转引发的思考
- Android拖拽ListView(上)
- 资治通鉴,穿越到北魏的人 - 崔浩
- GetWindowLong函数(最好的解释资料,绝对值得一看,此函数在子类化技巧时候经常用到)