1. Explanation of terms and business explanation
1. Specific business process
Get the binary file stream:
- Files can come from multiple sources, such as files uploaded by users, binary data stored in the database, files in the file system, etc.
- Read the file contents and convert it into a byte array.
createMockMultipartFile
Object:
- use
MockMultipartFile
Class, passing by byte array, file name, and file type as parameters into the constructor. -
MockMultipartFile
It is a class provided by Spring, which is mainly used to simulate file uploads in tests.
Process upload logic:
- Will
MockMultipartFile
Pass it to the business logic layer to save, process or parse files and other operations. - You can define the interface for file upload in the controller to process the received files.
Return response:
- According to the results of the business processing, the corresponding response information is returned to the client.
2. Transform object explanation
1. MockMultipartFile
Function: Used to simulate file upload scenarios, especially in testing.
Specific source code:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MockMultipartFile implements MultipartFile { private final String name; private final String originalFilename; @Nullable private final String contentType; private final byte[] content; public MockMultipartFile(String name, @Nullable byte[] content) { this(name, "", (String)null, (byte[])content); } public MockMultipartFile(String name, InputStream contentStream) throws IOException { this(name, "", (String)null, (byte[])(contentStream)); } public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) { (name, "Name must not be empty"); = name; = originalFilename != null ? originalFilename : ""; = contentType; = content != null ? content : new byte[0]; } public MockMultipartFile(String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream) throws IOException { this(name, originalFilename, contentType, (contentStream)); } public String getName() { return ; } @NonNull public String getOriginalFilename() { return ; } @Nullable public String getContentType() { return ; } public boolean isEmpty() { return == 0; } public long getSize() { return (long); } public byte[] getBytes() throws IOException { return ; } public InputStream getInputStream() throws IOException { return new ByteArrayInputStream(); } public void transferTo(File dest) throws IOException, IllegalStateException { (, dest); } }
Construct parameters:
-
name
: The corresponding field name in the form (such as"file"
)。 -
originalFilename
: The original file name of the uploaded file (such as""
)。 -
contentType
: The content type of the file (e.g."text/plain"
)。 -
content
: The byte content of the file (such asbyte[]
)。
2. Binary file stream
definition: The original data of the file is stored in bytes and can be any type of file (such as pictures, documents, etc.).
How to obtain:
- Read from the input stream (e.g.
InputStream
)。 - Read binary data from the database.
- Read directly from the file system.
2. Coding process
1.Introduce spring dependencies
//Introduce spring-test dependency <dependency> <groupId></groupId> <artifactId>spring-test</artifactId> <version>5.3.8</version> </dependency>
2. Writing method
private String extracted(byte[] data) { //Create cache output stream BufferedOutputStream bos = null; // FileOutputStream stream refers to file byte output stream, which is specifically used to output original byte streams such as image data, etc. It inherits the OutputStream class and has the basic characteristics of output streams. FileOutputStream fos = null; //Create a file object File file = null; String fileName1 = ""; String filePath = ("") + + ; try { File file1 = new File(filePath); if (!() && ()) { (); } // file to assign value file = new File(filePath + "\\" + fileName1); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); //Write data into file (data); } catch (IOException e) { (); } finally { if (bos != null) { try { (); } catch (IOException e) { (); } } if (fos != null) { try { (); } catch (IOException e) { (); } } } //Create MockMultipartFile object MockMultipartFile implements MultipartFile MockMultipartFile mockMultipartFile = null; try { //Convert local files to output stream to convert format FileInputStream inputStream = new FileInputStream(file); //Create objects and assign values through MockMultipartFile parameter structure mockMultipartFile = new MockMultipartFile((), (), ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); } catch (IOException e) { (); }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.