转自:http://blog.csdn.net/5iasp/article/details/8669644#comments
public static void main(String[] args) {
String filepath="d:\\test\\1.jpg";
String urlStr = "http://localhost:8080/minicms/picup.jsp";
Map<String, String> txtMap = new HashMap<String, String>();
txtMap.put("name", "testname");
Map<String, String> fMap = new HashMap<String, String>();
fMap.put("userfile", filepath);
String ret = formUpload(urlStr, txtMap, fMap);
System.out.println(ret);
}
public static String formUploadTest(String urlStr, Map<String, String> textMap,Map<String, String> fileMap) {
String res = null;
HttpURLConnection conn = null;
String 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());
// 一、
if (textMap != null && textMap.size>0) {
StringBuffer strBuf = new StringBuffer();
Iterator iter = textMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.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());
}
// 图片
if (fileMap != null && textMap.size>0) {
Iterator iter = fileMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.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();
String contentType = new MimetypesFileTypeMap().getContentType(file);
if (filename!=null && filename.endsWith(".png"))
contentType = "image/png";
if (contentType == null || contentType.equals(""))
contentType = "application/octet-stream";
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;
}
}
相关推荐
php curl模仿form表单提交图片或文件 用的tp5做的例子
php模拟表单上传图片 POST ,做一个b.php,接收数据
使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件。
模拟POST方式上传表单的VC++封转类
控件,form 表单需要设置 enctype=”multipart/form-data” 属性。比如: <body> <form action=UploadFile.php method=post enctype=multipart/form-data> <input type=file name=fileUpload /&...
libcurl 提交post表单数据 上传图片 demo 根据网页请求协议,模拟post表单的方式上传数据,并最返回数据进行处理
本文介绍了Node Js 使用...form表单上传文件流程(PHP和node js) 文件上传操作原理 form表单【注意:enctype=”multipart/form-data”】上传文件时,首先会将文件上传到你本机的temp目录,然后执行move_upload_file
CSharp客户端通过Http模拟表单提交到Java的web服务端.rar C#的wpf客户端使用HttpWebRequest模拟网页表单提交数据到Java的web服务端,...*该程序可以同时提交表单数据和上传文件* **********************************
form表单的多文件上传,具体内容如下 formData对象可以使用一系列的键值对来模拟一个完整的表单,然后使用Ajax来发送这个表单 使用<form>表单初始化FormData对象的方式上传文件 <!--文件上传--> <...
Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
利用FormData对象,我们可以通过JavaScript用一些键值对来模拟一系列表单控件,我们还可以使用XMLHttpRequest的send()方法来异步的提交这个”表单”.比起普通的ajax,使用FormData的最大优点就是我们可以异步上传一个二...
主要介绍了java通过模拟post方式提交表单实现图片上传功能实例,涉及Java针对表单的提交操作响应及文件传输的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
通过HTTP模拟GET或POST请求,提交数据到服务端获取响应,比较常见些;但如上传文件到服务端,使用html form当然简单了,而因环境所限有时需要使用模拟方法去提交有附件(文件上传)的表单。
sapload自己模拟了一个form你只需要告诉它你原来的form表单中处理上传文件的脚本url。它即可开始工作。 你可以把批量上传看作是sapload本身的一个时钟,当年你使用sapload批量选取文件后,sapload会按自己的时钟和...
(ps:说白了就是不使用form表单实现form表单提交数据或文件,如果还是不懂,请自行百度) 实现过程 1.使用type类型为file的input框实现选择文件(顺便记录一下修改input框的默认样式) 2.修改input框的默认样式 3...
此次学习的是HTTP的post协议,使用使用multipart form-data上传文件。 HTTP1.1封装 简单封装了HTTP1.1的get和post,实现http的长连接模式,实现发送和数据接收。简单实现了socket的复用。 详细介绍参考:...
在开发微信支付的小微商户进件接口时,需要通过表单来上传身份证图片等数据。在微信支付接口文档也说明了,需要使用 multipart/form-data 的方式发送请求。.NET 提供了 MultipartFormDataContent 类型,帮助我们构建...
主要介绍了Asp.Net模拟表单提交数据和上传文件的实现代码,本文对3种情况都做了介绍,只有普通数据的表单、只上传文件的表单、包含普通数据和上传文件表单,需要的朋友可以参考下