HttpClient 3.1 文件上传

HttpClient 3.1 文件上传

客户端:

需要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(); } } }

转载于:https://my.oschina.net/joeytai/blog/521140

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109282.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • jar包下载(全)

    jar包下载(全)转自:https://blog.csdn.net/meow_meow/article/details/78584696显示不出来请点击阅读更多作为初学者很多jar包不知道去哪里下载,给大家分享一个地址:这个网址是maven仓库的国内镜像地址:http://mvnrepository.com步骤图解:1.2.3….

    2022年5月15日
    100
  • 算法的力量

    算法的力量李开复 算法是计算机科学领域最重要的基石之一,但却受到了国内一些程序员的冷落。许多学生看到一些公司在招聘时要求的编程语言五花八门就产生了一种误解,认为学计算机就是学各种编程语言,

    2021年12月26日
    42
  • 10天学通Android开发(5)-项目实战:计算器

    10天学通Android开发(5)-项目实战:计算器

    2021年9月3日
    56
  • ANSI编码表_ansi配列

    ANSI编码表_ansi配列在日常开发中,在思考一个问题,为什么是英文来编程,结果ASCII这个玩意在处理。ASCII,AmericanStandardCodeforInformationInterchange念起来像是”阿斯key”,定义从0到127的一百二十八个数字所代表的英文字母或一样的结果与意义。由于只使用7个位元(bit)就可以表示从0到127的数字,大部分的电脑都使用8个位元来存取字元集(characterset),所以从128到255之间的数字可以用来代表另一组一百二十八个符号,称…

    2022年9月23日
    2
  • linux禁止防火墙命令,LINUX关闭打开防火墙命令

    Linux下打开和关闭防火墙1.及时生效,重启后复原关闭:serviceiptablesstop开启:serviceiptalbesstart查看状态:serviceiptablesstatus(关闭状态的话会提示firewalisnotrunning)2.非及时性生效,重启后永久性生效关闭:chkconfigiptbalesoff开启:chkconfigipt…

    2022年4月9日
    80
  • threadid=1: thread exiting with uncaught except…

    threadid=1: thread exiting with uncaught except…

    2021年8月25日
    59

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号