简体   繁体   中英

java.lang.IndexOutOfBoundsException when try to upload file on server

My previous post got deleted so i am reposting this here as i am not getting any clue of removing this error.

Error :

java.lang.IndexOutOfBoundsException
java.io.FileOutputStream.writeBytes(Native Method)
java.io.FileOutputStream.write(FileOutputStream.java:297)
com.ninenexus.simplesign.storeanddisplay.StoreAndDisplayImage.doPost(StoreAndDisplayImage.java:85)
javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

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;
    }
    String file = new String(dataBytes);
    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 = "d:/" + saveFile;

    try 
    {            
        File f = new File(saveFile);            
        FileOutputStream fileOut = new FileOutputStream(f);
        fileOut.write(dataBytes, startPos, (endPos - startPos)); // Line 85
        fileOut.flush();
        fileOut.close();
    }
    catch(...)
    {
       ...
    }
}
else 
{
    ...
}

The output of line no. 85 is

dataBytes=[B@676e3f, startPos=142, (endPos - startPos)=75944.

I am trying to take file from user through input type='file' and then save it in directory using this code. It works fine on tomcat server. File gets saved in the directory. But when I create the war file and upload it on the server. While trying running this code on main server I get this error. Got no clue of resolving this. Help really appreciated.

The error is most likely caused by the "interesting" way you determine the start and end position. You use a byte to String conversion and back with the current default encoding of the machine. Most likely, the default encoding is different between your development and production environments. This explains the difference between the environments.

For many encodings, there are byte sequences that cannot be decoded to a character. The String will then contain some replacement character. When you convert the string back to a byte sequence the length can be different as it was before and you get wrong positions.

change the following lines and try ..

String file = new String(dataBytes); to String file = new String(dataBytes,"CP1256");

int startPos = ((file.substring(0, pos)).getBytes()).length; to int startPos = ((file.substring(0, pos)).getBytes("CP1256")).length;

int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length; to int endPos = ((file.substring(0, boundaryLocation)).getBytes("CP1256")).length;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM