4

RestTemplate上传文件 - 佛祖让我来巡山

 2 years ago
source link: https://www.cnblogs.com/sun-10387834/p/16554574.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

1、上传的文件是File类型

如果文件保存在本地,即可以通过File file = new File(path) 或者 文件路径地址获取到指定文件

public String uploadFile(File file) {
    // 1、封装请求头
    HttpHeaders headers = new HttpHeaders();
    MediaType type = MediaType.parseMediaType("multipart/form-data");
    headers.setContentType(type);
    headers.setContentLength(file.length());
    headers.setContentDispositionFormData("media", file.getName());
    // 2、封装请求体
    MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
    FileSystemResource resource = new FileSystemResource(file);
    param.add("file", resource);
    // 3、封装整个请求报文
    HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(param, headers);
    // 4、发送请求
    ResponseEntity<String> data = restTemplate.postForEntity(tempMaterialUploadUrl, formEntity, String.class);
    // 5、请求结果处理
    JSONObject weChatResult = JSONObject.parseObject(data.getBody());
    return weChatResult;
}

这种方式可直接将File文件或者文件路径传递给FileSystemResource资源对象。然后将该资源放入请求体中。

2、上传的文件是InputStream流

如果文件不存在本地,而是在阿里云OSS或者其他只能够通过URL获取文件流,并且不想将文件存到本地而直接通过restTemplate发送,则可以使用下面方式。

例子:在工作中用到这个例子:我将系统上传的附件通过阿里云API上传至阿里云OSS,紧接着要讲文件存到企业微信服务器,而且系统服务器不存储任何附件,就用到了下面的方式,通过阿里云API将上传的附件A通过InputStream形式获取到,然后向企业微信服务端上传该输入流即可。

public String uploadInputStream(InputStream inputStream,String fileName,long cententLength) {
    // 1、封装请求头
    HttpHeaders headers = new HttpHeaders();
    MediaType type = MediaType.parseMediaType("multipart/form-data");
    headers.setContentType(type);
    headers.setContentDispositionFormData("media", fileName);
    // 2、封装请求体
    MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
    InputStreamResource resource = new InputStreamResource(inputStream){
        @Override
        public long contentLength(){
            return cententLength;
        }
        @Override
        public String getFilename(){
            return fileName;
        }
    };
    param.add("file", resource);
    // 3、封装整个请求报文
    HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(param, headers);
    // 4、发送请求
    ResponseEntity<String> data = restTemplate.postForEntity(tempMaterialUploadUrl, formEntity, String.class);
    // 5、请求结果处理
    JSONObject weChatResult = JSONObject.parseObject(data.getBody());
    // 6、返回结果
    return weChatResult;
}

在需要输入流进行上传文件时,需要使用InputStreamResource构建资源文件,注意要重写contentLength() 和 getFilename()方法,否则不成功。至于参数中的fileNamecontentLength要提前通过文件的URL获取到,因为我是从阿里云OSS读取的文件,所以直接能够获取到文件的大小和文件名。

3、上传的是MultipartFile类型文件

有时候我们需要直接将系统上传SpringMVC通过MultipartFile类型接收的文件,此时就可使用下面方式解决。

public String uploadFileWithInputStream(MultipartFile file) throws IOException {
    // 1、封装请求头
    HttpHeaders headers = new HttpHeaders();
    MediaType type = MediaType.parseMediaType("multipart/form-data");
    headers.setContentType(type);
    headers.setContentLength(file.getSize());
    headers.setContentDispositionFormData("media", file.getOriginalFilename());
    // 2、封装请求体
    MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
    // 将multipartFile转换成byte资源进行传输
    ByteArrayResource resource = new ByteArrayResource(file.getBytes()) {
        @Override
        public String getFilename() {
            return file.getOriginalFilename();
        }
    };
    param.add("file", resource);
    // 3、封装整个请求报文
    HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<>(param, headers);
    // 4、发送请求
    ResponseEntity<String> data = restTemplate.postForEntity(tempMaterialUploadUrl, formEntity, String.class);
    // 5、请求结果处理
    JSONObject weChatResult = JSONObject.parseObject(data.getBody());
    // 6、返回结果
    return weChatResult;
}

注意点:使用ByteArrayResource构建资源时,应重写ByteArrayResourcegetFilename()方法,不然不成功。


以上就是常用的RestTemplate上传不同形式文件的方式。有用点赞~感谢~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK