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

3

我希望调用一个REST服务,该服务需要以下请求参数。此方法将文件上传到图像服务器。

 @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对象(我不想将文件保存到本地磁盘)。

尝试下面的解决方案,但我认为您无法在不将文件保存到磁盘的情况下实现此目的。这是真的吗?


1
MultipartFile 是否来自于 org.springframework.web.multipart.MultipartFile - Jobin
2个回答

5
你可以按照以下方式操作。我创建了一个MultipartFile实现类,并使用这个新创建的类来创建一个MultipartFile文件。 MultipartFile实现类
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());

我猜想在创建multipartFile对象之后应该进行flush操作。 - Tisha
1
MultipartFile multipartFile = new MultiPartImage(baos.toByteArray(), imageName+".jpg", MediaType.MULTIPART_FORM_DATA.toString(), baos.size());多部分文件multipartFile = new MultiPartImage(baos.toByteArray(),imageName +“。jpg”,MediaType.MULTIPART_FORM_DATA.toString(),baos.size()); - Tisha
上面的注释 - 这是我在构造函数中为你提到的所有变量设置的内容。 - Tisha
MultipartFile multipartFile = new MultiPartImage(baos.toByteArray(), imageName+".jpg", imageName, MediaType.MULTIPART_FORM_DATA.toString(), baos.size());多部分文件multipartfile =新的MultiPartImage(baos.toByteArray(),imageName+“.jpg”,imageName,MediaType.MULTIPART_FORM_DATA.toString(),baos.size()); - Batek'S
缺失一个属性 @Tisha - Batek'S
显示剩余6条评论

2

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

}

我希望你能为其他人提供帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接