利用ie浏览器插件httpwatch查看form表单上传时的数据封装格式,然后照着这数据格式自己一步一步封装
package com.android.cist.network.form;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class HttpFormUtil {
public static String post(String actionUrl, Map<String, String> params,FormFile[] files) {
try {
String enterNewline = "\r\n";
String fix="--";
String boundary="######";
String MULTIPART_FORM_DATA = "multipart/form-data";
URL url = new URL(actionUrl);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*");
con.setRequestProperty("Accept-Encoding", "gzip, deflate");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("Content-Type", MULTIPART_FORM_DATA+ ";boundary=" + boundary);
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
Set<String> keySet = params.keySet();
Iterator<String> it = keySet.iterator();
while(it.hasNext()){
String key = it.next();
String value = params.get(key);
ds.writeBytes(fix+boundary+enterNewline);
ds.writeBytes("Content-Disposition: form-data; "+"name=\"" + key + "\"" + enterNewline);
ds.writeBytes(enterNewline);
//ds.write(value.getBytes("UTF-8"));
ds.writeBytes(value);//如果有中文乱码,保存改用上面的ds.writeBytes(enterNewline);那句代码
ds.writeBytes(enterNewline);
}
if(files!=null&&files.length>0){
ds.writeBytes(fix+boundary+enterNewline);
ds.writeBytes("Content-Disposition: form-data; "+"name=\"" + files[0].getFormname() + "\"" +"; filename=\""+files[0].getFilname()+"\""+enterNewline);
ds.writeBytes(enterNewline);
ds.write(files[0].getData());
ds.writeBytes(enterNewline);
}
ds.writeBytes(fix+boundary+fix+enterNewline);
ds.flush();
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while((ch = is.read()) != -1){
b.append((char)ch);
}
ds.close();
return b.toString().trim();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String encode(String url) {
try {
return URLEncoder.encode(url, "UTF-8");
} catch (UnsupportedEncodingException ex) {
return url;
}
}
}
- 大小: 40.3 KB
- 大小: 178.5 KB
- 大小: 105.7 KB
分享到:
相关推荐
android中模拟http协议表单上传.docx
Android的HttpURLConnections等并不能直接实现表单形式上传文件,必须另类实现。注册时一般是先利用表单上传头像图片得到头像在服务器的地址,再将得到的地址和其它注册信息提交。
http模拟POST表单上传文件 客户端是模拟POST表单提交数据 服务端负责接收上传的文件并保存到本地
Android模拟表单提交 多图选择+多图上传Demo 对应博文:Android原生模拟表单提交上传多图+PHP接收表单数据和多图存储:http://blog.csdn.net/iwanghang/article/details/65630703
Android 多文件上传,表单上传,上传进度显示
由于iOS无法通过html表单来上传文件,因此想要上传文件,必须实现http请求,而不能像其他语言那样通过html表单的post就能上传。此demo经过xcode5测试通过。此文件上传demo是按照 rfc1867协议实现的。
php模拟表单上传图片 POST ,做一个b.php,接收数据
Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
通用的http请求工具,可以模拟表单提交、上传文件、发送请求、获取网页内容
CSharp客户端通过Http模拟表单提交到Java的web服务端.rar C#的wpf客户端使用HttpWebRequest模拟网页表单提交数据到Java的web服务端,Java服务端使用commons-fileupload.jar,Spring接受数据。 *******************...
用VC写了个MFC环境下的post数据类这次将这个类进行扩展,可以post图片。但这两个post所使用的方法不一样。
c++ 实现,在 linux 平台测试通过,windows 上原理是一样的,下载上传大文件没有问题,
模拟POST方式上传表单的VC++封转类
libcurl 提交post表单数据 上传图片 demo 根据网页请求协议,模拟post表单的方式上传数据,并最返回数据进行处理
android表单映射框架
使用nodejs来模拟form表单进行文件上传,可以同时上传多个文件。
Android简单的web提交表单登录,亲测已通过,不想码代码的同学进来
Java模拟表单post提交 HttpClient,Java开发者常用利器
JAVA表单上传多个文件 java web form表单上传多个文件.zip
通过HTTP模拟GET或POST请求,提交数据到服务端获取响应,比较常见些;但如上传文件到服务端,使用html form当然简单了,而因环境所限有时需要使用模拟方法去提交有附件(文件上传)的表单。