模拟PostMan的Post请求 带File和参数
2018-11-28 08:32
519 查看
模拟PostMan的Post请求 带File和参数
带参数及文件的post方法
public static String post(String url, HashMap<String, String> map, HashMap<String, byte[]> fileMap) throws Exception { int CONNECT_TIME_OUT = 30000; int READ_OUT_TIME = 50000; String boundaryString = getBoundary(); HttpURLConnection conne; URL url1 = new URL(url); conne = (HttpURLConnection) url1.openConnection(); conne.setDoOutput(true); conne.setUseCaches(false); conne.setRequestMethod("POST"); conne.setConnectTimeout(CONNECT_TIME_OUT); conne.setReadTimeout(READ_OUT_TIME); conne.setRequestProperty("accept", "*/*"); conne.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundaryString); conne.setRequestProperty("connection", "Keep-Alive"); conne.setRequestProperty("user-agent", "Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1)"); DataOutputStream obos = new DataOutputStream(conne.getOutputStream()); Iterator iter = map.entrySet().iterator(); while(iter.hasNext()){ Map.Entry<String, String> entry = (Map.Entry) iter.next(); String key = entry.getKey(); String value = entry.getValue(); obos.writeBytes("--" + boundaryString + "\r\n"); obos.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"\r\n"); obos.writeBytes("\r\n"); obos.write((value+ "\r\n").getBytes()); } if(fileMap != null && fileMap.size() > 0){ Iterator fileIter = fileMap.entrySet().iterator(); while(fileIter.hasNext()){ Map.Entry<String, byte[]> fileEntry = (Map.Entry<String, byte[]>) fileIter.next(); obos.writeBytes("--" + boundaryString + "\r\n"); obos.writeBytes("Content-Disposition: form-data; name=\"" + fileEntry.getKey() + "\"; filename=\"" + encode(" ") + "\"\r\n"); obos.writeBytes("\r\n"); obos.write(fileEntry.getValue()); obos.writeBytes("\r\n"); } } obos.writeBytes("--" + boundaryString + "--" + "\r\n"); obos.writeBytes("\r\n"); obos.flush(); obos.close(); InputStream ins = null; int code = conne.getResponseCode(); try{ if(code == 200){ ins = conne.getInputStream(); }else{ ins = conne.getErrorStream(); } }catch (SSLException e){ e.printStackTrace(); return ""; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[4096]; int len; while((len = ins.read(buff)) != -1){ baos.write(buff, 0, len); } byte[] bytes = baos.toByteArray(); ins.close(); String str = null; try { str = new String(bytes); }catch (Exception e){ e.printStackTrace(); } return str; } private static String getBoundary() { StringBuilder sb = new StringBuilder(); Random random = new Random(); for(int i = 0; i < 32; ++i) { sb.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-".charAt(random.nextInt("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_".length()))); } return sb.toString(); } private static String encode(String value) throws Exception{ return URLEncoder.encode(value, "UTF-8"); }
是将文件转至Byte类型,放入Map进行Post
将文件转Byte方法
public static byte[] getBytesFromFile(MultipartFile f) { File mfCovFile = null; try { mfCovFile = new File(f.getOriginalFilename()); FileUtils.copyInputStreamToFile(f.getInputStream(), mfCovFile); } catch (IOException e) { e.printStackTrace(); } if (f == null) { return null; } try { FileInputStream stream = new FileInputStream(mfCovFile); ByteArrayOutputStream out = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = stream.read(b)) != -1) out.write(b, 0, n); stream.close(); out.close(); return out.toByteArray(); } catch (IOException e) { } return null; }
好记性不如烂笔头
相关文章推荐
- PostMan 模拟AJAX发起POST请求 SpringMVC
- postman测试post请求参数为json类型
- postman测试post请求参数为json类型
- php 模拟浏览器 发送 post 和 get 请求(兼容curl + file_get_contents)
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- postman的安装与使用方法(模拟Get和Post请求)
- HttpClient模拟get,post请求并发送请求参数(json等)
- HttpClient模拟get,post请求并发送请求参数(json等)
- postman post请求接收不到参数
- postman发送post请求,用request.getParameter()获取请求参数注意事项
- postMan模拟get和post请求,支持局域网和外网
- cURL和file_get_contents实现模拟post请求
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- 模拟POST、Get 请求的工具----postman
- postman的安装与使用(模拟Get和Post请求)
- PHP模拟发送POST请求之四、加强file_get_contents()发送POST请求
- HttpClient模拟get,post请求并发送请求参数(json等)
- Fiddler模拟post和get请求,分析请求的参数、请求的数据
- postman模拟访问具有Session的post请求
- fiddler 模拟 post请求添加参数