Converting a Spring MultipartFile to a File – 将Spring MultipartFile转换为文件

最后修改: 2020年 6月 25日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll cover various ways of converting a Spring MultipartFile to a File.

在这个快速教程中,我们将介绍将Spring的MultipartFile转换为File的各种方法。

2. MultipartFile#getBytes

2.MultipartFile#getBytes

MultipartFile has a getBytes() method that returns a byte array of the file’s contents. We can use this method to write the bytes to a file:

MultipartFile有一个getBytes()方法,可以返回文件内容的字节数组。我们可以使用这个方法来将字节写入文件

MultipartFile multipartFile = new MockMultipartFile("sourceFile.tmp", "Hello World".getBytes());

File file = new File("src/main/resources/targetFile.tmp");

try (OutputStream os = new FileOutputStream(file)) {
    os.write(multipartFile.getBytes());
}

assertThat(FileUtils.readFileToString(new File("src/main/resources/targetFile.tmp"), "UTF-8"))
  .isEqualTo("Hello World");

The getBytes() method is useful for instances where we want to perform additional operations on the file before writing to disk, like computing a file hash.

getBytes()方法对于我们想在写入磁盘前对文件进行额外操作的情况很有用,比如计算文件哈希值。

3. MultipartFile#getInputStream

3.MultipartFile#getInputStream

Next, let’s look at MultipartFile‘s getInputStream() method:

接下来,让我们看看MultipartFilegetInputStream()方法

MultipartFile multipartFile = new MockMultipartFile("sourceFile.tmp", "Hello World".getBytes());

InputStream initialStream = multipartFile.getInputStream();
byte[] buffer = new byte[initialStream.available()];
initialStream.read(buffer);

File targetFile = new File("src/main/resources/targetFile.tmp");

try (OutputStream outStream = new FileOutputStream(targetFile)) {
    outStream.write(buffer);
}

assertThat(FileUtils.readFileToString(new File("src/main/resources/targetFile.tmp"), "UTF-8"))
  .isEqualTo("Hello World");

Here we’re using the getInputStream() method to get the InputStream, read the bytes from the InputStream, and store them in the byte[] buffer. Then we create a File and OutputStream to write the buffer contents.

这里我们使用getInputStream()方法来获取InputStream,从InputStream中读取字节,并将其存储在byte[] buffer中。然后我们创建一个FileOutputStream来写入buffer的内容。

The getInputStream() approach is useful in instances where we need to wrap the InputStream in another InputStream, say for example a GZipInputStream if the uploaded file was gzipped.

getInputStream()方法在我们需要将InputStream包裹在另一个InputStream中的情况下很有用,例如,如果上传的文件是gzipped的,那么就是GZipInputStream

4. MultipartFile#transferTo

4. MultipartFile#transferTo

Finally, let’s look at MultipartFile‘s transferTo() method:

最后,让我们看看MultipartFiletransferTo()方法

MultipartFile multipartFile = new MockMultipartFile("sourceFile.tmp", "Hello World".getBytes());

File file = new File("src/main/resources/targetFile.tmp");

multipartFile.transferTo(file);

assertThat(FileUtils.readFileToString(new File("src/main/resources/targetFile.tmp"), "UTF-8"))
  .isEqualTo("Hello World");

Using the transferTo() method, we simply have to create the File that we want to write the bytes to, then pass that file to the transferTo() method.

使用transferTo()方法,我们只需要创建我们想要写入字节的文件,然后将该文件传递给transferTo()方法。

The transferTo() method is useful when the MultipartFile only needs to be written to a File.

transferTo()方法很有用MultipartFile只需要被写入一个文件

5. Conclusion

5.总结

In this tutorial, we explored ways of converting a Spring MultipartFile to a File.

在本教程中,我们探讨了将Spring的MultipartFile转换为File的方法。

As usual, all code examples can be found over on GitHub.

像往常一样,所有的代码例子都可以在GitHub上找到