Java使用HttpURLConnection上传文件
从普通Web页面上传文件非常easy。仅仅须要在form标签叫上enctype="multipart/form-data"就可以,剩余工作便都交给浏览器去完毕数据收集并发送Http请求。可是假设没有页面的话要怎么上传文件呢?
因为脱离了浏览器的环境,我们就要自己去完毕数据的收集并发送请求。所以就非常麻烦了。首先我们来写个JSP页面并看看浏览器发出的Http请求是什么样的
JSP页面:
<html>
<head>
<meta charset="UTF-8">
<title>TestSubmit</title>
</head>
<body>
<form name="upform" action="upload.do" method="POST" enctype="multipart/form-data">
參数<input type="text" name="username"/><br/>
文件1<input type="file" name="file1"/><br/>
文件2<input type="file" name="file2"/><br/>
<input type="submit" value="Submit" /><br/>
</form>
</body>
</html>
假如我參数写的内容是hello word,然后二个文件是二个简单的txt文件。form提交的信息为:
-----------------------------7da2e536604c8
Content-Disposition: form-data; name="username" hello word
-----------------------------7da2e536604c8
Content-Disposition: form-data; name="file1"; filename="D:/haha.txt"
Content-Type: text/plain haha
hahaha
-----------------------------7da2e536604c8
Content-Disposition: form-data; name="file2"; filename="D:/huhu.txt"
Content-Type: text/plain messi
huhu
-----------------------------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. 第十五、十六行就是文件的内容了。如:
messi
huhu
10. 最后一行是-----------------------------7da2e536604c8--。注意最后多了二个“--”。作为结束的标志。
那么我们仅仅要模拟这个数据,并写入到Http请求中便能实现文件的上传。
事实上。在我之前的文章:HttpClient使用具体解释 ,就已经有利用HttpClient工具包上传文件的样例。HttpClient是Apache的一个强大的模拟并发送全部Http请求的开源类库,有时间的。大家能够学习学习,但本篇文章中。并不以HttpClient为例。而是採用Java自带的HttpURLConnection实现的。
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import net.sf.jmimemagic.Magic;
import net.sf.jmimemagic.MagicMatch; public class HttpPostUploadUtil { /**
* @param args
*/
public static void main(String[] args) {
String filepath = "E:\\ziliao\\0.jpg";
String urlStr = "http://127.0.0.1:8080/minicms/up/up_result.jsp";
Map<String, String> textMap = new HashMap<String, String>();
textMap.put("name", "testname");
Map<String, String> fileMap = new HashMap<String, String>();
fileMap.put("userfile", filepath);
String ret = formUpload(urlStr, textMap, fileMap);
System.out.println(ret);
} /**
* 上传图片
* @param urlStr
* @param textMap
* @param fileMap
* @return
*/
public static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap) {
String res = "";
HttpURLConnection conn = null;
String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); OutputStream out = new DataOutputStream(conn.getOutputStream());
// text
if (textMap != null) {
StringBuffer strBuf = new StringBuffer();
Iterator<Map.Entry<String, String>> iter = textMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
strBuf.append(inputValue);
}
out.write(strBuf.toString().getBytes());
} // file
if (fileMap != null) {
Iterator<Map.Entry<String, String>> iter = fileMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, String> entry = iter.next();
String inputName = (String) entry.getKey();
String inputValue = (String) entry.getValue();
if (inputValue == null) {
continue;
}
File file = new File(inputValue);
String filename = file.getName();
MagicMatch match = Magic.getMagicMatch(file, false, true);
String contentType = match.getMimeType(); StringBuffer strBuf = new StringBuffer();
strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" + filename + "\"\r\n");
strBuf.append("Content-Type:" + contentType + "\r\n\r\n"); out.write(strBuf.toString().getBytes()); DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
}
} byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
out.write(endData);
out.flush();
out.close(); // 读取返回数据
StringBuffer strBuf = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
strBuf.append(line).append("\n");
}
res = strBuf.toString();
reader.close();
reader = null;
} catch (Exception e) {
System.out.println("发送POST请求出错。" + urlStr);
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
conn = null;
}
}
return res;
} }
在上述代码中,关于contentType的获取方式。我在上篇文章中已经讲述过了,有兴趣的能够看看。
最新文章
- [LeetCode] Design Phone Directory 设计电话目录
- WebForm简单控件,复合控件
- 更改(修改)mysql自动增序列改变从1000开始
- Windows Azure 使用体验
- galera cluster DDL节点间状态不一致的问题
- on delete cascade 和on update cascade
- linux下xargs命令用法详解
- VC判断当前用户有无Administrator的权限(用EqualSid API函数判断与Admin是否在一个组的Sid)
- 关于sql server 代理(已禁用代理xp)
- firebug js版
- PHP的简单易懂文件管理,可实现基本功能
- 关于Yuri Boykov and Vladimir Kolmogorov 于2004年提出的max flow / min cut的算法的详解
- xadmin 数据添加报错: IndexError: list index out of range
- python一些好文章链接收藏
- 理解Python闭包概念
- 两个Integer比较
- 循环更新sqlserver数据库表ID
- BZOJ1222[HNOI2001]产品加工——DP
- spring事务详解(一)初探讨
- git 管理 Linux 文件系统
热门文章
- Webservice-WSDL详解(三)
- Selenium Grid跨浏览器-兼容性测试
- Linux Object-C 编译环境安装
- netbeans 将项目打包生成单个可执行的 jar
- 转:携程App的网络性能优化实践
- mockito中两种部分mock的实现,spy、callRealMethod
- 基于visual Studio2013解决C语言竞赛题之1009补数
- AdapterView&;lt;?&;gt; arg0, View arg1, int arg2, long arg3參数含义
- 使用数组实现队列----《数据结构与算法分析---C语言描述》
- poj1573&;amp;&;amp;hdu1035 Robot Motion(模拟)