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 data = new LinkedMultiValueMap();
data.add("file", new FileMessageResource(fileBytes, "upload.txt"));
final HttpEntity> requestEntity = new HttpEntity>(data);
final ResponseEntity> response = getRestTemplate().exchange(url, HttpMethod.POST, requestEntity, new ParameterizedTypeReference>() {});
     }

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;
    }
}

你可能感兴趣的:(Springboot Mutifile 转 file调用3方借口)