文件以二进制流POST的HTTP请求

 
 /**
  * 描述:文件以二进制流POST的HTTP请求
  *
  * @param reqUrl
  *            HTTP请求URL
  * @param fileUrl
  *            文件地址
  * @return HTTP响应的字符串
  * @throws Exception
  */
 public static String doPostFile(String reqUrl, String fileUrl) {
  return doPostFile(reqUrl, fileUrl, ENCODEING);
 }

 private static String doPostFile(String reqUrl, String fileUrl, String encoding) {
  HttpURLConnection url_con = null;
  String responseContent = null;
  try {
  
   

   URL url = new URL(reqUrl);

   url_con = (HttpURLConnection) url.openConnection();
   url_con.setRequestMethod("POST");
   url_con.setConnectTimeout(CONNECTTIMEOUT);
   url_con.setDoOutput(true);
   url_con.setRequestProperty("Content-type","application/x-java-serialized-object");
   
   File file = new File(fileUrl);
      InputStream ins = new FileInputStream(file);
      byte[] data = IOUtils.toByteArray(ins);
    
   url_con.getOutputStream().write(data, 0, data.length);
   url_con.getOutputStream().flush();
   url_con.getOutputStream().close();

   InputStream in = url_con.getInputStream();

   BufferedReader rd = new BufferedReader(new InputStreamReader(in,
     ENCODEING));
   String tempLine = rd.readLine();
   StringBuffer tempStr = new StringBuffer();
   String crlf = System.getProperty("line.separator");
   while (tempLine != null) {
    tempStr.append(tempLine);
    tempStr.append(crlf);
    tempLine = rd.readLine();
   }
   responseContent = tempStr.toString();
   rd.close();
   in.close();
  } catch (IOException e) {
   System.err.println("网络故障");
   logger.info("--------------------->网络故障");
  } finally {
   if (url_con != null) {
    url_con.disconnect();
   }
  }
  return responseContent;
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值