就在3天前,当我尝试上传PDF文件时,我收到以下错误消息...不确定为什么它现在不能正常工作...。我没有做任何更改,可以是服务器吗?

这是我得到的错误:

java.lang.IndexOutOfBoundsException
java.io.FileOutputStream.writeBytes(Native Method)
java.io.FileOutputStream.write(FileOutputStream.java:260)


例外

org.apache.jasper.JasperException: Exception in JSP: /drawingUp.jsp:48

45:         File savedFile = new File(folder +"/IngDemo/drawings/"+saveFile) ;
46:         FileOutputStream fileOut = new FileOutputStream(savedFile);
47:         //FileOutputStream fileOut = new FileOutputStream(savedFile);
48:         fileOut.write(dataBytes, startPos, (endPos - startPos));
49:         fileOut.flush();
50:         fileOut.close();


任何可能的线索将不胜感激.....
谢谢

这是更多的代码:

String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and
 //as well as the passed data from multipart/form-data is greater than or
 //equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
 {
    DataInputStream in = new DataInputStream(request.getInputStream());
    //we are taking the length of Content type data
    int formDataLength = request.getContentLength();
    byte dataBytes[] = new byte[formDataLength];
    int byteRead = 0;
    int totalBytesRead = 0;
    //this loop converting the uploaded file into byte code
    while (totalBytesRead < formDataLength) {
        byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
        totalBytesRead += byteRead;
        }
    String file = new String(dataBytes);
    //for saving the file name
    String 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;
    //extracting the index of file
    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;
    // creating a new file with the same name and writing the
//content in new file
    String folder = (String) new File(config.getServletContext().getRealPath("/")).getParent();
    folder= (String)folder.replace(File.separatorChar, '/');
    File savedFile = new File(folder +"/IngDemo/drawings/"+saveFile) ;
    FileOutputStream fileOut = new FileOutputStream(savedFile);
    //FileOutputStream fileOut = new FileOutputStream(savedFile);
    fileOut.write(dataBytes, startPos, (endPos - startPos));
    fileOut.flush();
    fileOut.close();

最佳答案

如何计算endPos和startPos?您的问题几乎可以肯定是这些值之一或全部都不正确。

10-07 23:29