Java:本地文件通过表单参数接口发送后大小变成0
问题
发现一个文件生成以后,如果不通过接口发送,大小就正常,通过接口发送,文件大小就变成0了,发送的文件也是0 空文件
代码
MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();FileSystemResource fileSystemResource = new FileSystemResource(fileName);form.add("file", fileSystemResource);HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(form, requestHeaders);String url = Consts.HTTP + IpUtils.getIpAddress(ip) + Consts.SEMICOLON + port + contextPath + uploadServletPath;log.info("导出-begin----------{},{}", url, JSON.toJSONString(form));ResponseEntity<Msg> responseEntity = restTemplate.postForEntity(url, entity, Msg.class);
原因
JSON.toJSONString(form) 序列化form表单时时会清空文件内容
处理办法
不使用JSON.toJSONString处理form表单
原理
todo 闲了再看原理