从普通Web页面上传文件很简单,只需要在form标签叫上enctype="multipart/form-data"即可,剩余工作便都交给浏览器去完成数据收集并发送Http请求。但是如果没有页面的话要怎么上传文件呢?

由于脱离了浏览器的环境,我们就要自己去完成数据的收集并发送请求,所以就很麻烦了。首先我们来写个JSP页面并看看浏览器发出的Http请求是什么样的

JSP页面:

  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <title>TestSubmit</title>
  5. </head>
  6. <body>
  7. <form name="upform" action="upload.do" method="POST" enctype="multipart/form-data">
  8.   参数<input type="text" name="username"/><br/>
  9.   文件1<input type="file" name="file1"/><br/>
  10.   文件2<input type="file" name="file2"/><br/>
  11.   <input type="submit" value="Submit" /><br/>
  12. </form>
  13. </body>
  14. </html>

假如我参数写的内容是hello word,然后二个文件是二个简单的txt文件,form提交的信息为:

  1. -----------------------------7da2e536604c8
  2. Content-Disposition: form-data; name="username"
  3. hello word
  4. -----------------------------7da2e536604c8
  5. Content-Disposition: form-data; name="file1"; filename="D:/haha.txt"
  6. Content-Type: text/plain
  7. haha
  8. hahaha
  9. -----------------------------7da2e536604c8
  10. Content-Disposition: form-data; name="file2"; filename="D:/huhu.txt"
  11. Content-Type: text/plain
  12. messi
  13. huhu
  14. -----------------------------7da2e536604c8--

研究下规律发现有如下几点特征:

1. 第一行是“-----------------------------7da2e536604c8”作为分隔符,然后是“/r/n”回车换行符。 这个7da2e536604c8分隔符浏览器是随机生成的。

2. 第二行是Content-Disposition: form-data; name="username"。代表form表单的数据域,name对应页面input标签的name值。

3. 第三行是“/r/n”回车换行符。

4. 第四行是参数username的值。

5. 第五行是7da2e536604c8分隔符。

6. 从第六行到第十行和从第十二行到第十六行,分别是上传的两个文件的数据域。

7. 第十二行是Content-Disposition: form-data; name="file2"; filename="D:/huhu.txt"。name对应页面input标签的name值,filename对应要上传的文件名(包括路径在内)。

8. 第十三行如果是文件就有Content-Type: text/plain。这里上传的是txt文件所以是text/plain,如果上穿的是jpg图片的话就是image/jpg了,可以自己试试看看。然后就是回车换行符。

9. 第十五、十六行就是文件的内容了。如:

  1. messi
  2. huhu

10. 最后一行是-----------------------------7da2e536604c8--。注意最后多了二个“--”,作为结束的标志。

那么我们只要模拟这个数据,并写入到Http请求中便能实现文件的上传。

其实,在我之前的文章:HttpClient使用详解 ,就已经有利用HttpClient工具包上传文件的例子,HttpClient是Apache的一个强大的模拟并发送所有Http请求的开源类库,有时间的,大家可以学习学习,但本篇文章中,并不以HttpClient为例,而是采用Java自带的HttpURLConnection实现的。

  1. import java.io.BufferedReader;
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.Map;
  13. import net.sf.jmimemagic.Magic;
  14. import net.sf.jmimemagic.MagicMatch;
  15. public class HttpPostUploadUtil {
  16. /**
  17. * @param args
  18. */
  19. public static void main(String[] args) {
  20. String filepath = "E:\\ziliao\\0.jpg";
  21. String urlStr = "http://127.0.0.1:8080/minicms/up/up_result.jsp";
  22. Map<String, String> textMap = new HashMap<String, String>();
  23. textMap.put("name", "testname");
  24. Map<String, String> fileMap = new HashMap<String, String>();
  25. fileMap.put("userfile", filepath);
  26. String ret = formUpload(urlStr, textMap, fileMap);
  27. System.out.println(ret);
  28. }
  29. /**
  30. * 上传图片
  31. * @param urlStr
  32. * @param textMap
  33. * @param fileMap
  34. * @return
  35. */
  36. public static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap) {
  37. String res = "";
  38. HttpURLConnection conn = null;
  39. String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符
  40. try {
  41. URL url = new URL(urlStr);
  42. conn = (HttpURLConnection) url.openConnection();
  43. conn.setConnectTimeout(5000);
  44. conn.setReadTimeout(30000);
  45. conn.setDoOutput(true);
  46. conn.setDoInput(true);
  47. conn.setUseCaches(false);
  48. conn.setRequestMethod("POST");
  49. conn.setRequestProperty("Connection", "Keep-Alive");
  50. conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
  51. conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
  52. OutputStream out = new DataOutputStream(conn.getOutputStream());
  53. // text
  54. if (textMap != null) {
  55. StringBuffer strBuf = new StringBuffer();
  56. Iterator<Map.Entry<String, String>> iter = textMap.entrySet().iterator();
  57. while (iter.hasNext()) {
  58. Map.Entry<String, String> entry = iter.next();
  59. String inputName = (String) entry.getKey();
  60. String inputValue = (String) entry.getValue();
  61. if (inputValue == null) {
  62. continue;
  63. }
  64. strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
  65. strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
  66. strBuf.append(inputValue);
  67. }
  68. out.write(strBuf.toString().getBytes());
  69. }
  70. // file
  71. if (fileMap != null) {
  72. Iterator<Map.Entry<String, String>> iter = fileMap.entrySet().iterator();
  73. while (iter.hasNext()) {
  74. Map.Entry<String, String> entry = iter.next();
  75. String inputName = (String) entry.getKey();
  76. String inputValue = (String) entry.getValue();
  77. if (inputValue == null) {
  78. continue;
  79. }
  80. File file = new File(inputValue);
  81. String filename = file.getName();
  82. MagicMatch match = Magic.getMagicMatch(file, false, true);
  83. String contentType = match.getMimeType();
  84. StringBuffer strBuf = new StringBuffer();
  85. strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
  86. strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename + "\"\r\n");
  87. strBuf.append("Content-Type:" + contentType + "\r\n\r\n");
  88. out.write(strBuf.toString().getBytes());
  89. DataInputStream in = new DataInputStream(new FileInputStream(file));
  90. int bytes = 0;
  91. byte[] bufferOut = new byte[1024];
  92. while ((bytes = in.read(bufferOut)) != -1) {
  93. out.write(bufferOut, 0, bytes);
  94. }
  95. in.close();
  96. }
  97. }
  98. byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
  99. out.write(endData);
  100. out.flush();
  101. out.close();
  102. // 读取返回数据
  103. StringBuffer strBuf = new StringBuffer();
  104. BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  105. String line = null;
  106. while ((line = reader.readLine()) != null) {
  107. strBuf.append(line).append("\n");
  108. }
  109. res = strBuf.toString();
  110. reader.close();
  111. reader = null;
  112. } catch (Exception e) {
  113. System.out.println("发送POST请求出错。" + urlStr);
  114. e.printStackTrace();
  115. } finally {
  116. if (conn != null) {
  117. conn.disconnect();
  118. conn = null;
  119. }
  120. }
  121. return res;
  122. }
  123. }

在上述代码中,关于contentType的获取方式,我在上篇文章中已经讲述过了,有兴趣的可以看看。

最新文章

  1. About_PHP读写文件
  2. Html5 布局方式
  3. 序列化效率比拼——谁是最后的赢家Newtonsoft.Json
  4. vi命令的基础知识
  5. hdu----(2586)How far away ?(DFS/LCA/RMQ)
  6. Android开发-API指南-&lt;uses-sdk&gt;
  7. “我爱淘”冲刺阶段Scrum站立会议8
  8. NetBeansRCP-添加/修改NetBeans的JVM启动参数
  9. 我的Android进阶之旅------&gt;HTTP 返回状态值详解
  10. 关闭和释放JDBC
  11. Linux向文件添加内容的几种方法
  12. Web渗透测试(sql注入 access,mssql,mysql,oracle,)
  13. ROS_Kinetic_14 ROS工具roswtf的基本使用方法等
  14. Python中的sys.path.append()
  15. 数据泵导出报ORA-01555 ORA-22924
  16. 接口压测初识java GC
  17. eclipse中tomcat可以start启动,无法debug启动的解决
  18. 推荐几个Adobe Flex Builder 3的插件(代码格式化和fms服务器通讯文件(main.asc)编写)
  19. html04
  20. 我所理解的 Laravel 请求 生命周期

热门文章

  1. Markdown标记语言
  2. 01《UML大战需求分析》阅读笔记之一
  3. Day 05 流程控制
  4. sklearn学习6----交叉验证
  5. plsql 中出现 Dynamic Performance Tables not accessible 问题解决
  6. typedef和define混用产生的错误
  7. thinkphp 同一字段不同查询条件实现
  8. linux内核(五)虚拟文件系统
  9. HDU 4307 Contest 1
  10. UBUNTU 16.04 下安装动态链接库方法(使用ln命令可以随意映射动态库,ldd查看缺少的动态库)