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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Mac Curl命令「建议收藏」

    Mac系统自带curl命令输出百度首页的html文件内容:curlhttp://www.baidu.com保存当前网页curlhttp://www.baidu.com>duang.html发送post请求curl-Xposthttp://www.baidu.com

    2022年4月16日
    284
  • Python 递归的多种写法

    Python 递归的多种写法题目背景:让我们来看一些例子。要对一个数字列表(或者其他序列)求和,我们可以使用内置的sum函数,或者自己编写一个更加定制化的版本。这里是用递归编写的一个定制求和函数的示例defmysum(L): ifnotL: return0 else: returnL[0]+mysum(L[1:])这是一种最基本的递归写法,通过递归的方式将列表中的所有进行相加,典型的鸭子类型…

    2022年6月16日
    35
  • 天翼云负载均衡配置ssl证书_阿里云服务器证书

    天翼云负载均衡配置ssl证书_阿里云服务器证书1、购买证书(图片有误,应该是购买通配符证书):2、申请证书:填写证书绑定域名:*.tianya.com联系人信息:xxx在自己的域名管理平台配置txt记录值,通过dns的txt记录类型来验证信息3、证书验证(根据上图中的DNS记录类型在域名管理平台进行txt记录验证,验证通过后点击上图中的验证按钮后会提示验证通过)4、申请验证完成:4.1点击验证后返回ssl证书管理平台页面会显示申请审核中4.2审核通过后下载自己需要的相应的证书备注:使用阿里云负载均衡进行https访问网站,

    2022年9月26日
    4
  • 那些年你用过最好的键盘吗_我做夫人那些年正版

    那些年你用过最好的键盘吗_我做夫人那些年正版那些年我买键盘踩过的坑分享给大家

    2022年10月16日
    2
  • Android系统签名以及生成keystore秘钥

    Android系统签名以及生成keystore秘钥Android11系统签名以及keystore一、什么是系统签名Android下所有APP都要进行签名,而有些权限,只有系统签名了的APP可以拥有。而且需要在APP的AndroidManifest中添加以下IDandroid:sharedUserId=”android.uid.system”二、使用signapk.jar签名2.1找到对应文件(1)在根目录下创建keystore文件夹以方便工作(2)查找密钥和证书build/target/product/security/plat

    2022年6月21日
    83
  • CreateMutex() 、ReleaseMutex()

    CreateMutex() 、ReleaseMutex()功能:CreateMutex()用于有独占要求的程序(在其进程运行期间不允许其他使用此端口设备的程序运行,或不允许同名程序运行)。比如运行金山词霸时,一次只能运行一个实例,当运行第二个实例时,实际上是激活第一个实例,将其带到最顶层。原型:1HANDLECreateMutex(2LPSECURITY_ATTRIBUTESlpMutexAttribut…

    2022年6月26日
    35

发表回复

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

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