Java 多部分文件到文件 InputStream

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

Multipart File to File InputStream

javaeclipsespringfile

提问by

How can I convert a MultipartFileto FileInputStreamin memory?

我怎么能转换MultipartFileFileInputStream内存中?

I have tried to below , but i am facing the error as

我已经尝试在下面,但我面临的错误是

org.springframework.web.multipart.commons.CommonsMultipartFile cannot be cast to java.io.File

org.springframework.web.multipart.commons.CommonsMultipartFile 不能转换为 java.io.File

My Code is

我的代码是

FileInputStream fis = new FileInputStream((File)file);

where file is a multipart file

其中 file 是一个多部分文件

回答by Amey Jadiye

Take look at MultipartFile

看看MultipartFile

In that you can go with :

你可以使用:

void    transferTo(File dest)

This method transfer the received file to the given destination file.

此方法将接收到的文件传输到给定的目标文件。

回答by PyThon

You can't create an instance of FileInputStream unless your file is not on file system.

除非您的文件不在文件系统上,否则您无法创建 FileInputStream 的实例。

You have to either first save the multipart file in temporary location on server using

您必须首先使用以下方法将多部分文件保存在服务器上的临时位置

file.transferTo(tempFile);
InputStream stream = new FileInputStream(tempFile);

But multipart file can also be read simply via basic streams methods such as

但是也可以通过基本的流方法简单地读取多部分文件,例如

InputStream inputStream =  new BufferedInputStream(file.getInputStream());

回答by Akshay

For a multipart file eg:

对于多部分文件,例如:

FileMultipartData part =  new FileMultipartData();
InputStream inputStream = part.getFileMultipart().get(0).getByteStream();

This worked for me in my code. Please try it

这在我的代码中对我有用。请尝试一下

回答by P.Saranya

Try using:

尝试使用:

MultipartFile uploadedFile = ((MultipartHttpServletRequest)request).getFile('file_name')
InputStream inputStream = new ByteArrayInputStream(uploadedFile?.getBytes())

回答by Gaurav Kumar

To convert Multipart file to Input Stream

将多部分文件转换为输入流

MultipartFile file;
InputStream inputStream = new InputStream(file.getInputStream());

This worked for me.

这对我有用。