Загрузка файла PDF с использованием JAVA - PullRequest
1 голос
/ 11 июля 2011

всего 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();

Ответы [ 2 ]

0 голосов
/ 11 июля 2011

Ваш код предполагает несколько вещей в запросе: он содержит определенное количество новых строк, размещение символа = и т. Д. Лучше всего добавить проверки в ваш код, чтобы убедиться, что end > start,что предположения верны и т. д., как показывает внезапное появление ошибок.

0 голосов
/ 11 июля 2011

Как рассчитываются конечные и начальные позиции? Ваша проблема почти наверняка в том, что одно или оба из этих значений неверны.

...