Java BufferedImage is converted to MultipartFile mode
- 2021-11-14 05:54:12
- OfStack
BufferedImage to MultipartFile
In Java, reading pictures or resizing pictures can be operated by BufferedImage (refer to my other article Java to modify picture size), but sometimes we need to change BufferedImage to MultipartFile for other operations, which can be converted as follows:
Method 1
1. Create a new ConvertToMultipartFile class to implement MultipartFile interface
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
public class ConvertToMultipartFile implements MultipartFile {
private byte[] fileBytes;
String name;
String originalFilename;
String contentType;
boolean isEmpty;
long size;
public ConvertToMultipartFile(byte[] fileBytes, String name, String originalFilename, String contentType,
long size) {
this.fileBytes = fileBytes;
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 fileBytes;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(fileBytes);
}
@Override
public void transferTo(File dest) throws IOException, IllegalStateException {
new FileOutputStream(dest).write(fileBytes);
}
}
2. Conversion of BufferedImage to MultipartFile
BufferedImage is first converted to byte [], and then converted to MultipartFile through the above ConvertToMultipartFile class
try {
// Read the picture and convert it to BufferedImage
BufferedImage image = ImageIO.read(new FileInputStream("F:/test/pic1.jpg"));
//BufferedImage Convert to ByteArrayOutputStream
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", out);
//ByteArrayOutputStream Convert to byte[]
byte[] imageByte = out.toByteArray();
// Will byte[] Convert to MultipartFile
MultipartFile multipartFile = new ConvertToMultipartFile(imageByte, "newNamepic", "pic1", "jpg", imageByte.length);
} catch (IOException e) {
e.printStackTrace();
}
Method 2
Introducing dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.2</version>
<scope>compile</scope>
</dependency>
try {
// Read the picture and convert it to BufferedImage
BufferedImage image = ImageIO.read(new FileInputStream("F:/test/pic1.jpg"));
// After resizing the picture BufferedImage . resizeImage The method is to resize the picture can refer to the beginning of the article on me 1 Article
BufferedImage newImage = ImageUtils.resizeImage(image, 200, 200);
// Will newImage Write byte array output stream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( newImage, "jpg", baos );
// Convert to MultipartFile
MultipartFile multipartFile = new MockMultipartFile("pic1.jpg", baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
Transfer from MultipartFile to File in Java data transfer
Error background
Since the file is stored on the third party server, all the need is to convert the received MultipartFile file to File and then transfer it. (Spring MVC)
The following two methods were found through the search engine
Are stated in spring xml. As follows:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
If you need to add maximum and minimum range controls, please refer to Baidu by yourself.
Method 1: Turn strongly
Method 2:
CommonsMultipartFile cf = (CommonsMultipartFile)multfile;
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File file = fi.getStoreLocation();
Pro-test is effective. However, it is found that the setting problem in the later period leads to errors in file conversion, and the file is unreadable, which leads to the program throwing is not a normal file exception.
Because of the randomness of error occurrences, we chose to use buffers to implement this transformation-using the MultipartFile. transferto () method for temporary files created by java.
The code is as follows:
File f = null;
try {
f=File.createTempFile("tmp", null);
file.transferTo(f);<br> f.deleteOnExit();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Pro-test is effective.