欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RestTemplate 调用 MultipartFile

程序员文章站 2022-03-04 19:07:16
...
package com.sf.eaoms.service;

import com.sf.eaoms.entity.CommonDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * @Author
 * @Classname UploadService
 * @Description 调用第三方获取可访问的url
 *         注意点的:1. contentType   multipart/form-data
 *                 2. 用原生的或者spring的 RestTemplate
 *                 3. MultipartFile 不可以序列化 (可能会包json 解析异常)
 * @Date 2021/3/11 22:32
 * @Created by windBird
 */
@Service
public class UploadService {

    @Autowired
    private RestTemplate restTemplate;

    @Value("{$third.item.url}")
    private String baseUrl;


    /**
     * 文件流 + 注入 RestTemplate +multipart/form-data
     * @param image
     * @return
     * @Description 调用第三方 获取可访问的 URL 第三方接受的参数类型  MultipartFile
     */
    public String getImageUrl(MultipartFile image) {
        String openImageUrl = "";
        LinkedMultiValueMap param = new LinkedMultiValueMap<>();
        param.add("file", image.getResource());
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.parseMediaType("multipart/form-data"));
        HttpEntity upParam = new HttpEntity<>(param, httpHeaders);
        String url = baseUrl + "/uploadImage";
        CommonDto resultData = restTemplate.postForEntity(url, upParam, CommonDto.class).getBody();
        if (null != resultData) {
            LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>) resultData.getData();
            openImageUrl = String.valueOf(linkedHashMap.get("fileUrl"));
        }
        return openImageUrl;
    }

    /**
     *  数组流+ new RestTemplate + multipart/form-data
     * @param image
     * @return
     * @throws IOException
     */
    public String getUrlByArrayByte(MultipartFile image) throws IOException {
        String openImageUrl = "";
        ByteArrayResource arrayResource = new ByteArrayResource(image.getBytes()) {
            @Override
            public String getFilename() {
                return image.getOriginalFilename();  //必须要重写路径
            }
        };
        LinkedMultiValueMap param = new LinkedMultiValueMap<>();
        param.add("file", arrayResource);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.parseMediaType("multipart/form-data"));
        HttpEntity upParam = new HttpEntity<>(param, httpHeaders);
        RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
        String url = baseUrl + "/uploadImage";
        CommonDto resultData = restTemplate.postForEntity(url, upParam, CommonDto.class).getBody();
        if (null != resultData) {
            LinkedHashMap<String, Object> linkedHashMap = (LinkedHashMap<String, Object>) resultData.getData();
            openImageUrl = String.valueOf(linkedHashMap.get("fileUrl"));
        }
        return openImageUrl;
    }
}