java http file_java中远程http文件上传及file2multipartfile

1 public static Map executeDriverServer(String driverUrl, Map param,String multipart, String contentType,int timeout,String picPath) throws Exception { 2 3 String res = ""; // 请求返回默认的支持json串 4 5 Map map = new HashMap(); 6 ContentType ctype = ContentType.create("content-disposition","UTF-8"); 7 8 Map map = new HashMap(); 9 10 HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); 11 12 CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); 13 14 String res = ""; // 请求返回默认的支持json串 15 16 HttpResponse httpResponse = null; 17 18 try { 19 20 HttpPost httpPost = new HttpPost(driverUrl); 21 22 //设置请求和传输超时时间 23 24 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(5000).build(); 25 26 httpPost.setConfig(requestConfig); 27 28 // BTW 4.3版本不设置超时的话,一旦服务器没有响应,等待时间N久(>24小时)。 29 30 if(httpPost!=null){ 31 32 if("formdata".equals(multipart)){ 33 34 MultipartEntityBuilder mentity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532); 35 36 Set keyset = param.keySet(); 37 38 for (String key : keyset) { 39 40 Object paramObj = Validate.notNull(param.get(key)); 41 42 if(paramObj instanceof MultipartFile) { 43 44 mentity.addBinaryBody(key, ((MultipartFile) paramObj).getInputStream(),ctype,((MultipartFile) paramObj).getOriginalFilename()); 45 46 }else if(paramObj instanceof File){ 47 48 mentity.addBinaryBody(key, (File)paramObj);//(key, new FileInputStream((File)paramObj),ctype,((File)paramObj).getName()); 49 50 }else{ 51 52 mentity.addPart(key,new StringBody(paramObj.toString(),ctype)); 53 54 //mentity.addTextBody(key,paramObj.toString()); 55 56 } 57 58 logger.info("key::::"+key); 59 60 logger.info("paramObj::::"+paramObj.toString()); 61 62 } 63 64 HttpEntity entity = mentity.build(); 65 66 HttpUriRequest post = RequestBuilder.post().setUri(driverUrl).setEntity(entity).build(); 67 68 httpResponse = closeableHttpClient.execute(post); 69 70 }else { 71 72 HttpEntity entity = convertParam(param, contentType); 73 74 httpPost.setEntity(entity); 75 76 httpResponse = closeableHttpClient.execute(httpPost); 77 78 } 79 80 if(httpResponse == null) { 81 82 throw new Exception("无返回结果"); 83 84 } 85 86 // 获取返回的状态码 87 88 int status = httpResponse.getStatusLine().getStatusCode(); 89 90 logger.info("Post请求URL="+driverUrl+",请求的参数="+param.toString()+",请求的格式"+contentType+",状态="+status); 91 92 if(status == HttpStatus.SC_OK){ 93 94 HttpEntity entity2 = httpResponse.getEntity(); 95 96 InputStream ins = entity2.getContent(); 97 98 res = toString(ins); 99 100 ins.close(); 101 102 }else{ 103 104 InputStream fis = httpResponse.getEntity().getContent(); 105 106 Scanner sc = new Scanner(fis); 107 108 logger.info("Scanner:::::"+sc.next()); 109 110 logger.error("Post请求URL="+driverUrl+",请求的参数="+param.toString()+",请求的格式"+contentType+",错误Code:"+status); 111 112 } 113 114 map.put("code", String.valueOf(status)); 115 116 map.put("result", res); 117 118 logger.info("执行Post方法请求返回的结果 = " + res); 119 120 } 121 122 } catch (ClientProtocolException e) { 123 124 map.put("code", HttpClientUtil.CLIENT_PROTOCOL_EXCEPTION_STATUS); 125 126 map.put("result", e.getMessage()); 127 128 } catch (UnsupportedEncodingException e) { 129 130 map.put("code", HttpClientUtil.UNSUPPORTED_ENCODING_EXCEPTION_STATUS); 131 132 map.put("result", e.getMessage()); 133 134 } catch (IOException e) { 135 136 map.put("code", HttpClientUtil.IO_EXCEPTION_STATUS); 137 138 map.put("result", e.getMessage()); 139 140 } finally { 141 142 try { 143 144 closeableHttpClient.close(); 145 146 } catch (IOException e) { 147 148 logger.error("调用httpClient出错", e); 149 150 throw new Exception("调用httpClient出错", e); 151 152 } 153 154 } 155 156 157 158 private static String toString(InputStream in) throws IOException{ 159 160 ByteArrayOutputStream os = new ByteArrayOutputStream(); 161 162 byte[] b = new byte[1024]; 163 164 int len; 165 166 while((len = in.read(b)) != -1) { 167 168 os.write(b, 0, len); 169 170 } 171 172 return os.toString("UTF-8"); 173 174 } 175 }

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值