将 MultipartFile 转换为 java.io.File 而不复制到本地机器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21089106/
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-13 06:29:35  来源:igfitidea点击:

Converting MultipartFile to java.io.File without copying to local machine

javaspring-mvcfile-iomultipart

提问by Shiju K Babu

I have a Java Spring MVC web application. From client, through AngularJS, I am uploading a file and posting it to Controller as webservice.

我有一个 Java Spring MVC Web 应用程序。从客户端,通过 AngularJS,我上传一个文件并将其作为 web 服务发布到控制器。

In my Controller, I am gettinfg it as MultipartFileand I can copy it to local machine.

在我的控制器中,我将它作为MultipartFile 获取,我可以将它复制到本地机器。

But I want to upload the file to Amazone S3bucket. So I have to convert it to java.io.File. Right now what I am doing is, I am copying it to local machine and then uploading to S3 using jets3t.

但我想将文件上传到Amazone S3存储桶。所以我必须将它转换为java.io.File。现在我正在做的是,我将它复制到本地机器,然后使用jets3t上传到 S3 。

Here is my way of converting in controller

这是我在控制器中转换的方式

MultipartHttpServletRequest mRequest=(MultipartHttpServletRequest)request;

Iterator<String> itr=mRequest.getFileNames();
        while(itr.hasNext()){
            MultipartFile mFile=mRequest.getFile(itr.next());
            String fileName=mFile.getOriginalFilename();

            fileLoc="/home/mydocs/my-uploads/"+date+"_"+fileName; //date is String form of current date.

Then I am using FIleCopyUtils of SpringFramework

然后我使用 SpringFramework 的 FIleCopyUtils

File newFile = new File(fileLoc);

                  // if the directory does not exist, create it
                  if (!newFile.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();  
                  }
                FileCopyUtils.copy(mFile.getBytes(), newFile);

So it will create a new file in the local machine. That file I am uplaoding in S3

所以它会在本地机器上创建一个新文件。我在 S3 中上传的那个文件

S3Object fileObject = new S3Object(newFile);

s3Service.putObject("myBucket", fileObject);

It creates file in my local system. I don't want to create.

它在我的本地系统中创建文件。我不想创造。

Without creating a file in local system, how to convert a MultipartFIleto java.io.File?

不在本地系统中创建文件,如何将MultipartFIle转换为java.io.File

采纳答案by Boris

MultipartFile, by default, is already saved on your server as a file when user uploaded it. From that point - you can do anything you want with this file. There is a method that moves that temp file to any destination you want. http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/multipart/MultipartFile.html#transferTo(java.io.File)

默认情况下,MultipartFile 在用户上传时已作为文件保存在您的服务器上。从那时起 - 您可以对这个文件做任何你想做的事情。有一种方法可以将该临时文件移动到您想要的任何目的地。 http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/multipart/MultipartFile.html#transferTo(java.io.File)

But MultipartFile is just API, you can implement any other MultipartResolverhttp://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/multipart/MultipartResolver.html

但是 MultipartFile 只是 API,您可以实现任何其他MultipartResolver http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/multipart/MultipartResolver.html

This API accepts input stream and you can do anything you want with it. Default implementation (usually commons-multipart) saves it to temp dir as a file.

这个 API 接受输入流,你可以用它做任何你想做的事情。默认实现(通常是 commons-multipart)将它作为文件保存到临时目录。

But other problem stays here - if S3 API accepts a file as a parameter - you cannot do anything with this - you need a real file. If you want to avoid creating files at all - create you own S3 API.

但其他问题仍然存在——如果 S3 API 接受一个文件作为参数——你不能用它做任何事情——你需要一个真实的文件。如果您想完全避免创建文件 - 创建您自己的 S3 API。

回答by Kostas Filios

The question is already more than one year old, so I'm not sure if the jets35link provided by the OP had the following snippet at that time.

这个问题已经有一年多了,所以我不确定OP 提供的jets35链接当时是否有以下片段。

If your data isn't a Fileor Stringyou can use any input streamas a data source, but you must manually set the Content-Length.

// Create an object containing a greeting string as input stream data.
String greeting = "Hello World!";

S3Object helloWorldObject = new S3Object("HelloWorld2.txt");

ByteArrayInputStream greetingIS = new ByteArrayInputStream(greeting.getBytes());

helloWorldObject.setDataInputStream(greetingIS);
helloWorldObject.setContentLength(
    greeting.getBytes(Constants.DEFAULT_ENCODING).length);
helloWorldObject.setContentType("text/plain");

s3Service.putObject(testBucket, helloWorldObject);

如果您的数据不是File或者String您可以使用任何输入流作为数据源,但您必须手动设置 Content-Length

// Create an object containing a greeting string as input stream data.
String greeting = "Hello World!";

S3Object helloWorldObject = new S3Object("HelloWorld2.txt");

ByteArrayInputStream greetingIS = new ByteArrayInputStream(greeting.getBytes());

helloWorldObject.setDataInputStream(greetingIS);
helloWorldObject.setContentLength(
    greeting.getBytes(Constants.DEFAULT_ENCODING).length);
helloWorldObject.setContentType("text/plain");

s3Service.putObject(testBucket, helloWorldObject);

It turns out you don't have to create a local file first. As @Boris suggests you can feed the S3Objectwith the Data Input Stream, Content Typeand Content Lengthyou'll get from MultipartFile.getInputStream(), MultipartFile.getContentType()and MultipartFile.getSize()respectively.

事实证明,您不必先创建本地文件。作为@Boris建议你可以喂S3ObjectData Input StreamContent Type并且Content Length你会从中获取MultipartFile.getInputStream()MultipartFile.getContentType()MultipartFile.getSize()分别。