Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback, please email oracle-forums_moderators_us@oracle.com

File Upload is corrupting the Excel file and failing to process

user-a96ccMay 25 2023

Hi Team,

I am working on an application where we are using Struts, Spring, JSP(UI) and the application server is Weblogic server.

When I am uploading any excel file using the application the file is getting corrupted and unable to process further.

Java version : 1.8

Struts Frame work : 1.3x

WebLogic Server Version: 12.2.1.4.0

commons-fileupload.jar, poi-3.10.1.jar, poi-ooxml-3.10.1.jar and poi-ooxml-schemas-3.10.1.jar

In JSP

Using encoding='UTF-8'

<form name="CCFormBean" method="post" enctype="multipart/form-data">

<input type="file" id="fileUpLoader" size="10" onchange="updateFileName()"
name="ccVO.fileVO.myUploadFile" class="inputField6"
onmouseup="return false;" onmousedown="return false;"
onkeydown="return false;" accept=".xls,.xlsx"/>

in Java code

fileToCreate = new File(newFileLocation, fileName); // newFileLocation : server path to save Excel file and fileName: name of file

if (!fileToCreate.exists()) {
FileOutputStream fileOutStream = new FileOutputStream(
fileToCreate);
fileOutStream.write(myFile.getFileData());
fileOutStream.flush();
fileOutStream.close();
System.out.println(" File write sucessfully:------>"+fileName);
}

This code saves the uploaded Excel file in the unix directory but it got corrupted . when we try to open the excel file it says “File is corrupt and cannot be opened”

If we use the file in code then it gives the below exception in server:

java.io.IOException: ZIP entry size is too large
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:117)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:55)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:84)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:272)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:218)
at com.fritolay.ses.action.osha.UploadAction.excelToBean(UploadAction.java:252)
at com.fritolay.ses.action.osha.UploadAction.uploadCntryGrpDetailsToDatabase(UploadAction.java:135)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.struts.actions.ActionDispatcher.dispatchMethod(ActionDispatcher.java:362)
at org.apache.struts.actions.ActionDispatcher.dispatchMethod(ActionDispatcher.java:336)
at org.apache.struts.actions.ActionDispatcher.execute(ActionDispatcher.java:223)

Kindly help me.

I am using the code in my local machine it is working fine with out any issues and I am able to save the uploaded file and open the uploaded file without any issue.

Comments
Post Details
Added on May 25 2023
0 comments
561 views