将MultipartFile转换为java.io.File,而不复制到本地机器 [英] Converting MultipartFile to java.io.File without copying to local machine

查看:2157
本文介绍了将MultipartFile转换为java.io.File,而不复制到本地机器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在我的控制器,我得到它作为 MultipartFile 我可以把它复制到本地机器。



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



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

  MultipartHttpServletRequest mRequest =(MultipartHttpServletRequest)请求; 

迭代器< String> ITR = mRequest.getFileNames(); (itr.hasNext()){
MultipartFile mFile = mRequest.getFile(itr.next());
String fileName = mFile.getOriginalFilename();

fileLoc =/ home / mydocs / my-uploads /+ date +_+ fileName; //日期是当前日期的字符串形式。

然后我使用SpringFramework的FIleCopyUtils

 文件newFile = new File(fileLoc); 

//如果目录不存在,创建它
if(!newFile.getParentFile()。exists()){
newFile.getParentFile()。mkdirs() ;
}
FileCopyUtils.copy(mFile.getBytes(),newFile);

所以它会在本地机器上创建一个新文件。那个文件我在S3中兴高采烈

  S3Object fileObject = new S3Object(newFile); 

s3Service.putObject(myBucket,fileObject);

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



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

解决方案

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



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



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



但是其他问题仍然存在 - 如果S3 API接受一个文件作为参数,做任何事情 - 你需要一个真正的文件。如果你想避免创建文件 - 创建你自己的S3 API。


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

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

But I want to upload the file to Amazone S3 bucket. 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.

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

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

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 MultipartFIle to java.io.File?

解决方案

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)

But MultipartFile is just API, you can implement any other 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.

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.

这篇关于将MultipartFile转换为java.io.File,而不复制到本地机器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆