No serializer found for class java.io.FileDescriptor
如第三方的文件上传接口: ?
/**
* 上传文件
* @param request
* @param response
* @param file
* @param sysFile
* @return
*/
@RequestMapping(value = "/sysFile/uploadFile", method = RequestMethod.POST, produces = "text/html;charset=utf-8")
@ResponseBody
public Object uploadFile(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile file, SysFile sysFile, String crossDomain) {
Json json = sysFileService.updateUploadFile(file, sysFile);
String str = JSONObject.toJSONString(json);
if (!StringUtil.isBlank(crossDomain) && crossDomain.equals("1")){
return "<script type='text/javascript'>var result='" + str + "';window.parent.postMessage(result, '*')</script>";
} else {
return str;
}
}
需要注意:
# 1. multiValueMap设置的"file" ,需要进行文件转化处理 # 2. 设置head为 headers.setContentType(MediaType.MULTIPART_FORM_DATA);
调用这接口时:
# /sysFile/uploadFile 为第三方接口,
# 1. multiValueMap设置的"file" ,需要进行文件转化处理
# 2. 设置head为 headers.setContentType(MediaType.MULTIPART_FORM_DATA);
String thirdUrl = "https://xxxx.net/xxbb" + "/sysFile/uploadFile";
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("fileProvenance", 1);
multiValueMap.add("fileType", 1);
multiValueMap.add("fileSerialNo", 1);
//multiValueMap.add("file", file);
multiValueMap.add("file", new FileSystemResource(convert(file)));
public File convert(MultipartFile file)
{
File convFile = new File("temp_image", file.getOriginalFilename());
if (!convFile.getParentFile().exists()) {
System.out.println("mkdir:" + convFile.getParentFile().mkdirs());
}
try {
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(file.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return convFile;
}
private static volatile RestTemplate restTemplate;
public static R postWithFormFile(String url, MultiValueMap<String, Object> multiValueMap) {
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity(multiValueMap, headers);
ResponseEntity<String> responseEntity = getRestTemplate().postForEntity(url, httpEntity, String.class, new Object[0]);
logger.info("调用第三方服务,url为:{}, 返回结果为:{}", url, JSONObject.toJSONString(responseEntity));
if (!ObjectUtil.isEmpty(responseEntity) && responseEntity.getStatusCodeValue() == 200) {
String body = (String)responseEntity.getBody();
return R.data(body);
} else {
return R.fail(ResultCode.THIRD_REQUEST_FAIL);
}
} catch (RestClientException var6) {
logger.error("调用第三方应用请求异常,异常原因:{}", var6.getMessage());
return R.fail(ResultCode.FAILURE);
}
}
private static RestTemplate getRestTemplate() {
if (ObjectUtil.isEmpty(restTemplate)) {
Class var0 = RestTemplateUtil.class;
synchronized(RestTemplateUtil.class) {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(100000);
factory.setConnectTimeout(10000);
restTemplate = new RestTemplate(factory);
}
}
return restTemplate;
}
参考: Springboot 中使用 RestTemplate 向另一个 RESTful api 请求 multipart file 报错_thothsun 的博客-CSDN博客
|