利用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;           }       }   }


更多相关文章

  1. Linux 解决文件名 中文乱码问题
  2. Android开发指南中文版(七)Content Providers
  3. 借助“手说”免费应用软件,实现中文朗读(Android TTS实践)
  4. android TTS语音播报
  5. Android 模拟HTTP 协议进行表单提交
  6. Android Design Android 设计指南非官方简体中文版
  7. Android—API中文文档

随机推荐

  1. 如何用 Python 给照片换色
  2. 原创丨我在 GitHub 上发现了哪些好的学习
  3. 还记得当年你是如何接触Python的吗?
  4. 我们终于可以把 bug 留给子孙后代了
  5. 牛逼了!Python代码补全利器,提高效率告别99
  6. 分析B站100万+视频,发现竟然有这么多干货
  7. 如何做出好看的可视化视频?这是我最舍不得
  8. 使用Flask在服务器实现一个API接口。
  9. 是今天网不好吗?
  10. Python 中更优雅的环境变量设置方案