RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", addToken);
MediaType type = MediaType.parseMediaType("multipart/form-data;charset=UTF-8");
headers.setContentType(type);
MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
FileSystemResource fileSystemResource = new FileSystemResource("c:\\测试.txt");
form.add("parent_dir", uploadPath);
form.add("file", fileSystemResource);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(form, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
Content-Disposition 我查了下好像只支持 ISO8859-1 编码,
尝试用这种方法手动转换过去也不行。
HttpHeaders headers2 = new HttpHeaders();
String cd = "form-data; name=\"file\"; filename=\"" + "测试" + "\"";
String cdEncode = new String(cd.getBytes("utf-8"), "iso8859-1");
headers2.add("Content-Disposition", cdEncode);
HttpEntity<FileSystemResource> fileSystemResourceHttpEntity = new HttpEntity<FileSystemResource>(fileSystemResource, headers2);
form.add("file", fileSystemResourceHttpEntity);
请问 java 后台上传文件应该怎么处理中文文件名?谢谢
1
arvintian 2017-05-31 18:26:12 +08:00
解决了吗?怎么解决的,分享一下,谢谢~
|