Multipart File to File InputStream - java

How can I convert a MultipartFile to FileInputStream in memory?
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
My Code is
FileInputStream fis = new FileInputStream((File)file);
where file is a multipart file

You can't create an instance of FileInputStream unless your file is not on file system.
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());

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

Take look at MultipartFile
In that you can go with :
void transferTo(File dest)
This method transfer the received file to the given destination file.

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

To convert Multipart file to Input Stream
MultipartFile file;
InputStream inputStream = new InputStream(file.getInputStream());
This worked for me.

We may just cast and use like below
FileInputStream file = (FileInputStream) multipartFile.getInputStream();

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

Related

How to download the folder from minio as zip file?

The steps I followed are.
get all objects from recursive objects
Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder()
.bucket(bucketName).recursive(true).build());
Then getting all streams of matching the prefix
InputStream stream = minioClient.getObject(GetObjectArgs.builder()
.bucket(bucketName).object(objectName).build());
the list of multiple stream got by the InputStream stream How do we convert it into zip file ?
tried the following code but it's (zipOut) coming as null.
downloading empty zip, How do we fix this ?
ByteArrayOutputStream fos = new ByteArrayOutputStream();
ZipOutputStream zipOut = new ZipOutputStream(fos);
ZipEntry zipEntry1 = new ZipEntry(objectName);
zipEntry1.setSize(resource.contentLength());
zipEntry1.setTime(System.currentTimeMillis());
zipOut.putNextEntry(zipEntry1);
StreamUtils.copy(stream.readAllBytes(), zipOut);
zipOut.closeEntry();
Thanks in advance.

how to get the inputStream from Workbook object in java

When I upload the file i am passing the inputstream to the workbook. Now I want to use this InputStream from workbook in another method like save where I save the file InputStream in to DB. Here is my code.
public void FileUpload(FileUploadEvent event) throws ParseException {
UploadedFile item = event.getUploadedFile();
Workbook workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create(item.getInputStream());
}
Now I want to make Workbook object as instance variable and pass to another method like below.
public String save() throws SQLException, IOException{
fileId = dao.savefile(workbook,fileName);
}
In my savefile method
InputStream inptest= **workbook.getStream**
ps.setBinaryStream(2,fin,fin.available());
So inptest variable accepts InputStream which I wanted to get it from Workbook.
It sounds like what you are asking for is a way to use the InputStream for multiple purposes:
To create a Workbook object (which you're already doing)
To save the content of that InputStream somewhere else
Since reading from an InputStream is usually a one-time-only operation that cannot be repeated, then you can do the following:
Save the full content of the InputStream to a buffer.
Open two new InputStreams from the buffer.
Pass your InputStreams to your two methods.
Code might look like this:
public void FileUpload(FileUploadEvent event) throws ParseException {
UploadedFile item = event.getUploadedFile();
InputStream originalInputStream = item.getInputStream();
byte[] buffer = IOUtils.toByteArray(originalInputStream);
InputStream is1 = new ByteArrayInputStream(buffer);
InputStream is2 = new ByteArrayInputStream(buffer);
Workbook workbook = org.apache.poi.ss.usermodel.WorkbookFactory.create(is1);
}
InputStream inptest = is2;
ps.setBinaryStream(2,fin,fin.available());
Note: this uses Apache Commons IO library for IOUtils.
If you are trying to save the Workbook object to a file, there is a method write() which takes in an OutputStream. Saving to a file can then be accomplished by
FileOutputStream fos = new FileOutputStream("path/to/file/[filename]");
workbook.write(fos);
fos.close();

How to convert InputStream to FileStream?

I want convert InputStream to FileStream on Android
Process process = Runtime.getRuntime().exec(cmd);
InputStream stdout = process.getInputStream();
FileInputStream fis = (FileInputStream)stdout;
FileDescriptor fd = fis.getFD();
"cmd" is stream command.
Is it impossible?
If possible, how can I fix it?
Use ClassLoader#getResource() instead.
URL resource = classLoader.getResource("Resource_Name");
File file = new File(resource.toURI());
FileInputStream input = new FileInputStream(file);
That said, I really don't see any benefit of doing so, or it must be required by a poor helper class/method which requires FileInputStream instead of InputStream. If you can, just use InputStream instead.

how to Typecast File object into InputStream

How to Typecast File object into InputStream.
File file=new File("c:\\abc.txt");
Thanks
File file=new File("c:\\abc.txt");
InputStream is = new FileInputStream(file);
or
InputStream is = new FileInputStream("c:\\abc.txt");
You don't typecast the file to Input stream, you create an InputStream object using the file as parameter. You can use FileInputStream:
FileInputStream fis = new FileInputStream(file);
Use file as a parameter in a FileInputStream Object.
Like this,
FileInputStream fis = new FileInputStream(file);
Creates a FileInputStream by opening a connection to an actual file,
the file named by the File object file in the file system. A new
FileDescriptor object is created to represent this file connection.

File input output question with Inputstreamreader

I made an android app which writes to a file in an activity.
The writing to file, it works like a charm:
FileOutputStream fOut = openFileOutput("myfeeds.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(file);
osw.flush();
osw.close();
But when I want to read it back from another acivity it can't find the file...the file exists I checked with DDMS file explorer.
Reading file contents:
FileInputStream fis = new FileInputStream("myfeeds.txt"); // cant find file
InputSource input = new InputSource(fis);
xr.setContentHandler(this);
xr.parse(input);
What is the correct location to my file?
Use openFileInput to get FileInputStream object for those files which are written using openFileOutputStream
use the following code
FileInputStream fiss = openFileInput("myfeeds.txt");
InputSource input = new InputSource(fis);
xr.setContentHandler(this);
xr.parse(input);
You should use
openFileInput( String name )
to read your file.
Regards,
STéphane

Categories