Springboot Mutifile 转 file调用3方借口

2018-05-16  本文已影响0人  白敏鸢
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;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读