/**
* 根据地址获得数据的输入流
*
* @param strUrl 网络连接地址
* @return url的输入流
*/
public static InputStream getInputStreamByUrl(String strUrl) {
HttpURLConnection conn = null;
try {
URL url = new URL(strUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(20 * 1000);
final ByteArrayOutputStream output = new ByteArrayOutputStream();
IOUtils.copy(conn.getInputStream(), output);
return new ByteArrayInputStream(output.toByteArray());
} catch (Exception e) {
log.error("根据地址获得数据的输入流异常 Exception,", e);
throw new RuntimeException("根据地址获得数据的输入流异常 Exception,", e);
} finally {
try {
if (conn != null) {
conn.disconnect();
}
} catch (Exception e) {
log.error("断开输入流异常 Exception,", e);
throw new RuntimeException("断开输入流异常 Exception,", e);
}
}
}
/**
* 以post方式调用第三方接口,以form-data 形式 发送 InputStream 文件数据
*
* @return
*/
public static String doPostFormData(String url, String fileParamName, InputStream input, String token, String id) {
// 创建Http实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建HttpPost实例
HttpPost httpPost = new HttpPost(url);
//携带token
httpPost.addHeader("Authorization",token);
// 请求参数配置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000)
.setConnectionRequestTimeout(10000).build();
httpPost.setConfig(requestConfig);
try {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(java.nio.charset.Charset.forName("UTF-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// 文件流 file是参数
builder.addBinaryBody("file", input, ContentType.MULTIPART_FORM_DATA, fileParamName);
//携带其他参数
builder.addTextBody("classId",id);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);// 执行提交
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 返回
String res = EntityUtils.toString(response.getEntity(), java.nio.charset.Charset.forName("UTF-8"));
return res;
}
} catch (Exception e) {
e.printStackTrace();
log.error("调用HttpPost失败!" + e.toString());
} finally {
if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
log.error("关闭HttpPost连接失败!");
}
}
}
return null;
}
@Override
public JhMlRealBimUploadOutVO uploadFile(InputStream input, String fileName, String modelId){
JhMlRealBimUploadOutVO result = null;
//认证请求头
String token ="Bearer "+this.getBimToken();
log.info(token);
String api_url = url + "***";//访问接口
String ret = null;
try {
ret = FileHttpUitls.doPostFormDataRealBim(api_url,fileName,input,token,modelId);
log.info(ret);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
JSONObject retObj = JSONObject.parseObject(ret);
if(retObj.getBoolean("success")) {
//返回数据处理
String jsonObject = retObj.get("data").toString();
JSONObject data = JSONObject.parseObject(jsonObject);
result = data.toJavaObject(JhMlRealBimUploadOutVO.class);
}else {
throw new RuntimeException(retObj.get("message").toString());
}
return result;
}
调用
public Result<?> test(@RequestParam("url") String url,
@RequestParam("fileName") String fileName,
@RequestParam("modelId") String modelId){
try {
InputStream input = FileUtils.getInputStreamByUrl(url);
data = jhMlRealbimApiService.uploadFile(input,"file",modelId);
} catch (Exception e) {
log.info(e.getMessage());
}
return Result.OK(data);
}