formdata文件数据转发

	/**
	 * post请求formdata数据
	 * @author zongx
	 * @throws Exception 
	 *
	 */
    public static String postFormData(String postUrl,Map<String,String> postParam,List<MultipartFile> postFiles) throws Exception{
    	
    	URL url=new URL(postUrl);
	    HttpURLConnection conn=(HttpURLConnection)url.openConnection();
	    int timeout = (StringUtils.isNumeric(httpTimeout) ? Integer.parseInt(httpTimeout) : 10) * 1000;
	    
	    conn.setConnectTimeout(timeout*1000);
	    conn.setReadTimeout(timeout*1000);
	    conn.setDoOutput(true);
	    conn.setUseCaches(false);
	    conn.setRequestMethod("POST");
	    conn.setRequestProperty("Accept-Charset", "utf-8");
	    conn.setRequestProperty("Connection", "keep-alive");
	    conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=1234");
	    
	    OutputStream out=conn.getOutputStream();
	    
	    // 参数上传
	    Iterator<Entry<String, String>> it = postParam.entrySet().iterator();
	    while (it.hasNext()) {
			Entry<String, String> entry = it.next();
		    // key
		    out.write("--1234\r\n".getBytes());
		    String key = entry.getKey();
		    out.write(("Content-Disposition: form-data; name=\"" + key + "\"\r\n").getBytes());
		    out.write("\r\n".getBytes());
		    // value
		    String value = entry.getValue();
		    out.write(value.getBytes());
		    out.write("\r\n".getBytes());			
		}
	    
	    
	    // photo 上传
	    InputStream fin =null;
	    byte[] fileBytes = null;
	    for(int i = 0; i < postFiles.size(); i++) {
	    	if(i != postFiles.size() - 1) {
	    	    out.write("--1234\r\n".getBytes());
	    	    String name = postFiles.get(i).getOriginalFilename();
	    	    out.write(("Content-Disposition: form-data; name=\""+name+"\";filename=\""+name+"\"\r\n").getBytes());
	    	    out.write("Content-Type: application/octet-stream\r\n".getBytes());
	    	    fin=postFiles.get(i).getInputStream();
	    	    fileBytes=new byte[fin.available()];
	    	    fin.read(fileBytes);
	    	    out.write("\r\n".getBytes());
	    	    out.write(fileBytes);
	    	    out.write("\r\n".getBytes());
	    	    fin.close();
	    	}else{
	    	    out.write("--1234\r\n".getBytes());
	    	    String name = postFiles.get(i).getOriginalFilename();
	    	    out.write(("Content-Disposition: form-data; name=\""+name+"\";filename=\""+name+"\"\r\n").getBytes());
	    	    out.write("Content-Type: application/octet-stream\r\n".getBytes());
	    	    fin=postFiles.get(i).getInputStream();
	    	    fileBytes=new byte[fin.available()];
	    	    fin.read(fileBytes);
	    	    out.write("\r\n".getBytes());
	    	    out.write(fileBytes);
	    	    out.write("\r\n".getBytes());
	    	    out.write("--1234--\r\n".getBytes());
	    	    fin.close();
	    	}
	    }
	    out.flush();
	    out.close();
	    //返回的参数
	    int respCode=conn.getResponseCode();
	    if(respCode==200) {
	      BufferedReader reader=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
	      StringBuilder builder=new StringBuilder();
	      String line;
	      while((line=reader.readLine())!=null) {
	        builder.append(line);
	      }
	      System.out.println(builder.toString());
	      return builder.toString();
	    }
		return null;   		  
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值