Upload excel (.xlsx) with jsp

Asked

Viewed 137 times

3

I am developing an application to submit Excel files to an application.

However if I try to submit a file .txt,works properly if I send a file .xlsx the file copied to the server directory appears empty. I’m using the lib:

commons-fileupload-1.3.2.jar  
commons-io-2.5.jar

HTML code:
<form name="uploadForm" action="addForm.jsp" method="POST" enctype="multipart/form-data" > <input type="file" name="file" size="50" /> <br /> <input type="submit" value="Submit" name="submit" /> </form>

Java code:

<% 
            String file1="";
            String  saveFile = new String();
            String contentType = request.getContentType();

               if ((contentType!=null) && (contentType.indexOf("multipart/form-data") >= 0)) {
                   DataInputStream in = new DataInputStream(request.getInputStream());
                   int formDataLength = request.getContentLength();
                   byte dataBytes[] = new byte[formDataLength];
                   int byteRead = 0;
                   int totalBytesRead = 0;

                   while(totalBytesRead < formDataLength){
                       byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
                       totalBytesRead += byteRead;
                   }

                   //converter o nosso array de bytes em String
                   String file = new String(dataBytes);
                   file1=file;

                   saveFile= file.substring(file.indexOf("filename=\"")+10);
                   saveFile= saveFile.substring(0, saveFile.indexOf("\n"));
                   saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+1, saveFile.indexOf("\""));

                   int lastIndex = contentType.lastIndexOf("=");

                   String boundary = contentType.substring(lastIndex +1, contentType.length());

                   int pos;
                   pos = file.indexOf("filename=\"");
                   pos = file.indexOf("\n", pos)+1; 
                   pos = file.indexOf("\n", pos)+1;
                   pos = file.indexOf("\n", pos)+1;

                   int boundaryLocation = file.indexOf  (boundary, pos)-4;
                   int startPos = ((file.substring(0,pos)).getBytes()).length;
                   int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
                   saveFile = "/home/cgi/Folder_to_Upload/"+saveFile;


                   File ff = new File(saveFile);

                   try{
                       FileOutputStream fileOut = new FileOutputStream(ff);
                       fileOut.write(dataBytes,startPos, (endPos-startPos));
                       fileOut.flush();
                       fileOut.close();

                   }
                   catch (Exception  e){
                       out.println(e);
                   }

               }
        %>

Where can the error be happening?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.