java - 使用JAVA上传PDF文件

标签 java pdf file-upload

就在 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 是如何计算的?您的问题几乎可以肯定是这些值之一或两个都不正确。

关于java - 使用JAVA上传PDF文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6644605/

相关文章:

Java - 匹配器在访问组后抛出 IllegalStateException

java - 当包含反斜杠时,从用户输入创建正则表达式模式不起作用

java - ANTLR语法中的无限递归

azure - SelectPdf - NET Core 3.1 - 无法在 Azure 应用服务上将 HTML 转换为 PDF

php - 使用 jquery ajax 模式表单上传文件并保存文件名失败

jQuery文件上传: cannot upload file in Safari 11

java - 在 Java 中,我们可以声明 lambda 表达式存储在堆中吗?

iphone - 在ios中创建PDF到图像

javascript - 从 angular.js 表单生成 PDF

amazon-web-services - 如何使用 STSAssumeRoleSessionCredentialsProvider 和 roleSessionName 和 roleArn 为 AWS SDK for Java 2.x 进行配置?