如何将BufferedImage转换为MultiPart文件而不将文件保存到磁盘?

时间:2016-12-15 11:58:47

标签: java spring-boot multipartform-data bufferedimage resttemplate

我想进行一项服务调用,该服务调用在休息服务中接收以下请求参数。此方法将文件上载到图像服务器。

 @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResponseEntity<String> uploadFile(
            @RequestParam("file") MultipartFile file) {

以下是对服务进行调用的代码 - 我从图像URL中读取BufferedImage对象中的图像

     BufferedImage subImage= ImageIO.read(new URL(<some image url goes here>));
     File outputFile = new File("C:\\" + "myimage" + ".jpg");

    ImageIO.write(subImage, "jpg", outputFile);

    MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>();
    String url="http://serviceurl/upload";

    body.add("file", outputFile);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);

    HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<MultiValueMap<String, Object>>(body, headers);

    restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

如您所见,首先创建文件并将其保存到磁盘。如何避免此步骤并仅使用BufferedImage对象(我不想将文件保存到本地磁盘)。

尝试了下面的解决方案,但在我看来,如果不在磁盘上保存文件,就无法实现这一目标。这是真的吗?

2 个答案:

答案 0 :(得分:2)

你可以这样做..我创建了一个MultipartFile的实现类,我正在使用新创建的类创建一个MultipartFile文件。

MultipartFile Implementation

public class MultipartImage implements MultipartFile {


private byte[] bytes;
String name;
String originalFilename;
String contentType;
boolean isEmpty;
long size;

public MultipartImage(byte[] bytes, String name, String originalFilename, String contentType,
        long size) {
    this.bytes = bytes;
    this.name = name;
    this.originalFilename = originalFilename;
    this.contentType = contentType;
    this.size = size;
    this.isEmpty = false;
}

@Override
public String getName() {
    return name;
}

@Override
public String getOriginalFilename() {
    return originalFilename;
}

@Override
public String getContentType() {
    return contentType;
}

@Override
public boolean isEmpty() {
    return isEmpty;
}

@Override
public long getSize() {
    return size;
}

@Override
public byte[] getBytes() throws IOException {
    return bytes;
}

@Override
public InputStream getInputStream() throws IOException {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
    // TODO Auto-generated method stub

}
}

将Jpg转换为MultipartFile

BufferedImage originalImage = ImageIO.read(new File("path to file"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( originalImage, "jpg", baos );
baos.flush();

MultipartFile multipartFile = new MultipartImage(baos.toByteArray());

如果您不想创建自己的MultipartFile类实现,可以使用spring org.springframework.mock.web.MockMultipartFile

示例:

 MultipartFile multipartFile = MockMultipartFile(fileName, baos.toByteArray());

答案 1 :(得分:0)

Jobin Joseph,您的类MultipartImage是一个很好的起点,但在我看来,这是行不通的。我需要该类以编程方式将图像从数据库重新发送到Post服务,但由于该服务需要其他字段和字段名称(以及可序列化的对象),因此无法正常工作。我在这里将您的类修改为可以用作常规MultipartFile实现:

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

import org.springframework.web.multipart.MultipartFile;

public class MultipartImage implements MultipartFile, Serializable {

    private static final long serialVersionUID = 7417500052547882043L;

    private byte[] bytes;

    String fileName;
    String contentType;
    String fieldName;
    boolean isEmpty;
    long size;

    public MultipartImage(byte[] bytes, String fileName, String fieldName, String contentType, long size) {
        this.bytes = bytes;
        this.fileName = fileName;
        this.fieldName = fieldName;
        this.contentType = contentType;
        this.size = size;
        this.isEmpty = false;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public void setEmpty(boolean isEmpty) {
        this.isEmpty = isEmpty;
    }

    public void setSize(long size) {
        this.size = size;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public boolean isEmpty() {
        return isEmpty;
    }

    @Override
    public long getSize() {
        return size;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return bytes;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        // TODO Auto-generated method stub

    }

    @Override
    public String getName() {
        // TODO Auto-generated method stub
        return fileName;
    }

    @Override
    public String getOriginalFilename() {
        // TODO Auto-generated method stub
        return fileName;
    }

}

我希望这可以帮助其他人。