0

I have a web program which reads data from excel and insert it into database.

I am reading it from jsp as parts and then convert it into input stream.

    Part filePart = request.getPart("file");
    InputStream fileContent = filePart.getInputStream();

Then the below code is used to create a work book(org.apache.poi.ss.usermodel.Workbook) from it

    Workbook wb = null;
    Sheet sheet = null;
    try {
        wb = WorkbookFactory.create(fileContent);
        sheet = wb.getSheetAt(0);
    } catch (InvalidFormatException | EncryptedDocumentException | NullPointerException ex) {
        return null;
    }

from here I found this

create

public static Workbook create(java.io.InputStream inp)

Creates the appropriate HSSFWorkbook / XSSFWorkbook from the given InputStream.

Note that using an InputStream has a higher memory footprint than using a File.

So is there any way to convert an InputStream to File

or Parts to File

Thanks....

Community
  • 1
  • 1
muhammed aslam
  • 44
  • 1
  • 11
  • Use [Files.copy()](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#copy-java.io.InputStream-java.nio.file.Path-java.nio.file.CopyOption...-). – vanje Mar 22 '18 at 11:21
  • 1
    @muhammedaslam, why do you want to write your part to file here? Because of some note in documentation? Do you realize that you are trying to obtain a byte stream, write it to a hard drive, read it from a hard drive and only then create a workbook out of what you've read? Do you realize that doing that is much slower than converting to book directly, because working with harddrives is *much* slower than working with app memory? Do you have any other reason for doing any of this other than this small note on the side? – M. Prokhorov Mar 22 '18 at 11:26
  • @M.Prokhorov That was removed instantly. Don't even see how you could catch that ;) I flagged to quickly, only read 40% of the question – AxelH Mar 22 '18 at 11:29
  • @M.Prokhorov , a bit more info :- this is a web project, where the user uploads the file, I read it as I said above then check some column headings and if they are good it will insert the records to DB, otherwise it will show an error message – muhammed aslam Mar 22 '18 at 11:36
  • I don't know how I could read a excel file without getting it in the memory and if there is such a method it will be very useful for me ... thanks for checking out guys... – muhammed aslam Mar 22 '18 at 11:37
  • You already have all methods you need. You can create a `Workbook` using an `InputStream`. What else is there to require? The only thing I see which might stop you here is the need to process files that are far larger than available memory. In which case you need a save-consume processing queue for files, and whole upload resource becomes asynchronous, without ability to return a meaningful processing-based response quickly. – M. Prokhorov Mar 22 '18 at 11:40
  • @M.Prokhorov , I didn't get you. I do have memory problem when the work book is created, that is why I mentioned the quote I found in the documentation. I hope if I put File instead of InputStream it will use less memory. If there is other ways please mention – muhammed aslam Mar 22 '18 at 11:47
  • @muhammedaslam, I feel this whole explanation should've been in main question from the start. In which case, I see no further problem with this code: this is a duplicate question about writing multipart into a file. – M. Prokhorov Mar 22 '18 at 11:51
  • Possible duplicate of [Easy way to write contents of a Java InputStream to an OutputStream](https://stackoverflow.com/questions/43157/easy-way-to-write-contents-of-a-java-inputstream-to-an-outputstream) – M. Prokhorov Mar 22 '18 at 11:58
  • convert `inputStream` into `String` and write into file – Muhammad Usman Mar 22 '18 at 12:05
  • 1
    @Usman: I wouldn't recommend that. Converting an input stream to a String would mean to hold the whole content in memory at once. Writing it directly to a file as I recommended above works even with big files without additional memory consumption. – vanje Mar 22 '18 at 12:27

1 Answers1

1

Try FileUtils from Apache Commons IO

Part filePart = request.getPart("file");
InputStream fileContent = filePart.getInputStream();
File tempFile = File.createTempFile( "myfile", ".xls" )
FileUtils.copyToFile( fileContent, tempFile );

You'd also need:

import org.apache.commons.io.FileUtils;

And the appropriate dependency if you're using Maven:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>
Michael Akerman
  • 309
  • 2
  • 7