需求
上传文件服务中的文件到其他平台
接口描述:用于上传工程日志相关资料
请求url:/cq-szh-projectdocumentscomputesvc/api/service/addEngineerLog
请求方式:POST
请求类型:form/data
请求参数:包含对象参数 和 多个文件参数
其中recordPerson和uploadPerson为对象参数,
files为List< MultipartFile >类型,
fileInfos为JSONArray参数
/**
* 通过url获取文件资源
* @param url
* @param fileName
* @return
* @throws IOException
*/
private static ByteArrayResource getResourceByUrl(String url,String fileName) throws IOException {
// 通过url获取输入流
InputStream inputStream = getFileInputStream(url);
// 读取输入流到字节数组
byte[] bytes = readBytes(inputStream);
// 将自己数组转为文件资源
return new ByteArrayResource(bytes) {
@Override
public String getFilename() {
// 指定文件名称
return fileName;
}
};
}
/*读取网络文件*/
public static InputStream getFileInputStream(String path) {
URL url = null;
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
return conn.getInputStream();
} catch (Exception e) {
logger.error("读取网络文件异常:"+path);
}
return null;
}
/**
* 读取输入流到字节数组
* @param in
* @return
* @throws IOException
*/
public static byte[] readBytes(InputStream in) throws IOException {
//读取字节的缓冲
byte[] buffer = new byte[1024];
//最终的数据
byte[] result = new byte[0];
int size = 0;
while ((size = in.read(buffer)) != -1) {
int oldLen = result.length;
byte[] tmp = new byte[oldLen + size];
if (oldLen > 0) {//copy 旧字节
System.arraycopy(result, 0, tmp, 0, oldLen);
}
//copy 新字节
System.arraycopy(buffer, 0, tmp, oldLen, size);
result = tmp;
}
return result;
}
/**
* form-data的post 请求
* @param params 请求参数
* @param modelUrl 模块url
* @return
*/
public static JSONObject postFormData( MultiValueMap<String, Object> params, String modelUrl) {
HttpHeaders headers = new HttpHeaders();
headers.add("token", token);
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(params, headers);
//String urlParams = url + "?params={json}";
ResponseEntity<String> responseEntity = restTemplate.postForEntity(urlHeader+modelUrl, httpEntity, String.class);
//ResponseEntity<String> responseEntity = restTemplate.exchange(urlHeader+modelUrl, HttpMethod.POST, httpEntity, String.class);
return JSON.parseObject(responseEntity.getBody());
}
// 测试市工程项目数字化管理平台接口
MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
params.add("accessCode",accessCode);
params.add("secretKey",secretKey);
params.add("type","GC0019001");
params.add("name","日志名称1");
params.add("weather","天气");
params.add("dairyDate",new Date());
params.add("recordPerson",OperationInfo.builder().operator("张三").longitude("106.57").latitude("29.55").build());
params.add("content","内容");
params.add("uploadPerson",OperationInfo.builder().operator("张三").longitude("106.57").latitude("29.55").build());
//FileSystemResource fileSystemResource = new FileSystemResource("D:/浏览器下载/图/test1.jpg");
//FileSystemResource fileSystemResource = new FileSystemResource("http://192.168.5.91:9300/statics/project_61/safety/2021/12/22/cfc8b2e9-4536-44cb-a098-25cc6ed84e6b.jpg");
//通过url获取文件资源
ByteArrayResource contentsAsResource = getResourceByUrl("http://192.168.5.91:9300/statics/project_61/safety/2021/12/22/cfc8b2e9-4536-44cb-a098-25cc6ed84e6b.jpg","ces.jpg");
params.add("files", contentsAsResource);
List<FileInfo> fileInfos = new ArrayList<>();
// 同上面指定的文件名
fileInfos.add(FileInfo.builder().fileName("ces.jpg").documentType("GC0013001")
.unitProjectId("dc77d2c538cb4491a4b6aea006f9e48f").documentDirectoryId("3015").build());
params.add("fileInfos", JSONArray.parseArray(JSONObject.toJSONString(fileInfos)));
JSONObject jsonObject = postFormData(params, "/cq-szh-projectdocumentscomputesvc/api/service/addEngineerLog");
System.out.println(jsonObject);