Java 如何将多部分文件转换为文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24339990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:39:42  来源:igfitidea点击:

How to convert a multipart file to File?

javaspringspring-mvcfile-uploadcloudinary

提问by Amila Iddamalgoda

Can any one tell me what is a the best way to convert a multipart file (org.springframework.web.multipart.MultipartFile) to File (java.io.File) ?

谁能告诉我将多部分文件 (org.springframework.web.multipart.MultipartFile) 转换为 File (java.io.File) 的最佳方法是什么?

In my spring mvc web project i'm getting uploaded file as Multipart file.I have to convert it to a File(io) ,there fore I can call this image storing service(Cloudinary).They only take type (File).

在我的 spring mvc web 项目中,我将上传的文件作为多部分文件。我必须将其转换为 File(io) ,因此我可以调用此图像存储服务(Cloudinary)。它们只采用类型(文件)。

I have done so many searches but failed.If anybody knows a good standard way please let me know? Thnx

我做了很多搜索但都失败了。如果有人知道一个好的标准方法,请告诉我?谢谢

采纳答案by Petros Tsialiamanis

You can get the content of a MultipartFileby using the getBytesmethod and you can write to the file using Files.newOutputStream():

您可以MultipartFile使用getBytes方法获取 a 的内容,并且可以使用以下方法写入文件Files.newOutputStream()

public void write(MultipartFile file, Path dir) {
    Path filepath = Paths.get(dir.toString(), file.getOriginalFilename());

    try (OutputStream os = Files.newOutputStream(filepath)) {
        os.write(file.getBytes());
    }
}

You can also use the transferTo method:

您还可以使用transferTo 方法

public void multipartFileToFile(
    MultipartFile multipart, 
    Path dir
) throws IOException {
    Path filepath = Paths.get(dir.toString(), multipart.getOriginalFilename());
    multipart.transferTo(filepath);
}

回答by George Siggouroglou

You can also use the Apache Commons IOlibrary and the FileUtils class. In case you are using maven you can load it using the above dependency.

您还可以使用Apache Commons IO库和FileUtils 类。如果您使用的是 maven,则可以使用上述依赖项加载它。

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

The source for the MultipartFile save to disk.

MultipartFile 保存到磁盘的源。

File file = new File(directory, filename);

// Create the file using the touch method of the FileUtils class.
// FileUtils.touch(file);

// Write bytes from the multipart file to disk.
FileUtils.writeByteArrayToFile(file, multipartFile.getBytes());

回答by Alex78191

You can access tempfile in Spring by casting if the class of interface MultipartFileis CommonsMultipartFile.

如果接口的类MultipartFileCommonsMultipartFile.

public File getTempFile(MultipartFile multipartFile)
{
    CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
    FileItem fileItem = commonsMultipartFile.getFileItem();
    DiskFileItem diskFileItem = (DiskFileItem) fileItem;
    String absPath = diskFileItem.getStoreLocation().getAbsolutePath();
    File file = new File(absPath);

    //trick to implicitly save on disk small files (<10240 bytes by default)
    if (!file.exists()) {
        file.createNewFile();
        multipartFile.transferTo(file);
    }

    return file;
}

To get rid of the trick with files less than 10240 bytes maxInMemorySizeproperty can be set to 0 in @Configuration@EnableWebMvcclass. After that, all uploaded files will be stored on disk.

要摆脱文件小于 10240 字节的技巧,maxInMemorySize可以在@Configuration@EnableWebMvc类中将属性设置为 0 。之后,所有上传的文件都将存储在磁盘上。

@Bean(name = "multipartResolver")
    public CommonsMultipartResolver createMultipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("utf-8");
        resolver.setMaxInMemorySize(0);
        return resolver;
    }

回答by Heisenberg

Although the accepted answer is correct but if you are just trying to upload your image to cloudinary, there's a better way:

虽然接受的答案是正确的,但如果您只是想将图像上传到 cloudinary,则有更好的方法:

Map upload = cloudinary.uploader().upload(multipartFile.getBytes(), ObjectUtils.emptyMap());

Where multipartFile is your org.springframework.web.multipart.MultipartFile.

其中 multipartFile 是您的org.springframework.web.multipart.MultipartFile

回答by Anand Tagore

The answer by Alex78191 has worked for me.

Alex78191 的回答对我有用。

public File getTempFile(MultipartFile multipartFile)
{

CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile) multipartFile;
FileItem fileItem = commonsMultipartFile.getFileItem();
DiskFileItem diskFileItem = (DiskFileItem) fileItem;
String absPath = diskFileItem.getStoreLocation().getAbsolutePath();
File file = new File(absPath);

//trick to implicitly save on disk small files (<10240 bytes by default)

if (!file.exists()) {
    file.createNewFile();
    multipartFile.transferTo(file);
}

return file;
}

For uploading files having size greater than 10240 bytes please change the maxInMemorySize in multipartResolver to 1MB.

要上传大于 10240 字节的文件,请将 multipartResolver 中的 maxInMemorySize 更改为 1MB。

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- setting maximum upload size t 20MB -->
<property name="maxUploadSize" value="20971520" />
<!-- max size of file in memory (in bytes) -->
<property name="maxInMemorySize" value="1048576" />
<!-- 1MB --> </bean>

回答by Swadeshi

small correction on @PetrosTsialiamanis post , new File( multipart.getOriginalFilename())this will create file in server location where sometime you will face write permission issues for the user, its not always possible to give write permission to every user who perform action. System.getProperty("java.io.tmpdir")will create temp directory where your file will be created properly. This way you are creating temp folder, where file gets created , later on you can delete file or temp folder.

对@PetrosTsialiamanis 帖子的小更正, new File( multipart.getOriginalFilename())这将在服务器位置创建文件,有时您将在该位置面临用户的写权限问题,并不总是可以为执行操作的每个用户授予写权限。 System.getProperty("java.io.tmpdir")将创建临时目录,您的文件将在其中正确创建。这样您就可以创建临时文件夹,在其中创建文件,稍后您可以删除文件或临时文件夹。

public  static File multipartToFile(MultipartFile multipart, String fileName) throws IllegalStateException, IOException {
    File convFile = new File(System.getProperty("java.io.tmpdir")+"/"+fileName);
    multipart.transferTo(convFile);
    return convFile;
}

put this method in ur common utility and use it like for eg. Utility.multipartToFile(...)

将此方法放入您的通用实用程序中并使用它,例如。 Utility.multipartToFile(...)

回答by sachintha hewawasam

  private File convertMultiPartToFile(MultipartFile file ) throws IOException
    {
        File convFile = new File( file.getOriginalFilename() );
        FileOutputStream fos = new FileOutputStream( convFile );
        fos.write( file.getBytes() );
        fos.close();
        return convFile;
    }

回答by andrej

MultipartFile.transferTo(File) is nice, but don't forget to clean the temp file after all.

MultipartFile.transferTo(File) 很好,但毕竟不要忘记清理临时文件。

// ask JVM to ask operating system to create temp file
File tempFile = File.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_POSTFIX);

// ask JVM to delete it upon JVM exit if you forgot / can't delete due exception
tempFile.deleteOnExit();

// transfer MultipartFile to File
multipartFile.transferTo(tempFile);

// do business logic here
result = businessLogic(tempFile);

// tidy up
tempFile.delete();

Check out Razzlero's comment about File.deleteOnExit() executed upon JVM exit (which may be extremely rare) details below.

在下面查看 Razzlero 关于在 JVM 退出时执行的 File.deleteOnExit() 的评论(这可能非常罕见)详细信息。

回答by Artem Botnev

if you don't want to use MultipartFile.transferTo(). You can write file like this

如果您不想使用 MultipartFile.transferTo()。你可以这样写文件

    val dir = File(filePackagePath)
    if (!dir.exists()) dir.mkdirs()

    val file = File("$filePackagePath${multipartFile.originalFilename}").apply {
        createNewFile()
    }

    FileOutputStream(file).use {
        it.write(multipartFile.bytes)
    }