Springboot Mutifile 转 file调用3方借口

MultipartFile multfile-->byte[] fileBytes
new FileMessageResource(fileBytes, "upload.txt"));
getRestTemplate().exchange()

service逻辑

public String uploadDutyExcel(HttpServletRequest request,
            @RequestParam("file") MultipartFile multfile) throws Exception {
            
byte[] fileBytes=multfile.getBytes();

Headers headers=new Headers();
header.setContentType(MediaType.MULTIPART_FROM_DATA)
final MultiValueMap<String,Object> data = new LinkedMultiValueMap<String,Object>();
data.add("file", new FileMessageResource(fileBytes, "upload.txt"));
final HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(data);
final ResponseEntity<Map<String,String>> response = getRestTemplate().exchange(url, HttpMethod.POST, requestEntity, new ParameterizedTypeReference<Map<String,String>>() {});
     }

utils.class


public class FileMessageResource extends ByteArrayResource {

    /**
     * The filename to be associated with the {@link MimeMessage} in the form data.
     */
    private final String filename;

    /**
     * Constructs a new {@link FileMessageResource}.
     * @param byteArray A byte array containing data from a {@link MimeMessage}.
     * @param filename The filename to be associated with the {@link MimeMessage} in
     *  the form data.
     */
    public FileMessageResource(final byte[] byteArray, final String filename) {
        super(byteArray);
        this.filename = filename;
    }

    @Override
    public String getFilename() {
        return filename;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容