使用JAVA上传PDF文件
就在3天前,当我试图上传PDF文件时,收到以下错误消息。。。不知道为什么它现在不起作用。。。。我没有改变任何东西,可能是服务器吗 这就是我得到的错误:使用JAVA上传PDF文件,java,pdf,file-upload,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.Jaspe
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();
String contentType=request.getContentType();
//这里我们检查内容类型是否不等于Null和
//以及从多部分/表单数据传递的数据大于或
//等于0
if((contentType!=null)&&(contentType.indexOf(“多部分/表单数据”)>=0))
{
DataInputStream in=新的DataInputStream(request.getInputStream());
//我们正在计算内容类型数据的长度
int formDataLength=request.getContentLength();
字节数据字节[]=新字节[formDataLength];
int byteRead=0;
int totalBytesRead=0;
//此循环将上载的文件转换为字节码
while(totalBytesRead
如何计算endPos和startPos?您的问题几乎可以肯定是这两个值中的一个或两个都不正确。您的代码假设了请求的许多方面:它包含一定数量的换行符、字符的位置等等。最好在代码中添加检查,以确保在end>start
,假设成立,等等,因为突然出现的错误向您显示。不确定您显示的是谁的代码…明显的问题是startPos和endPos来自何处。一个或两个都无效。我添加了更多的代码,谢谢您的评论。我添加了更多的代码,请检查好吗?我看不出任何错误。请使用编辑器中的“代码”按钮格式化您的代码,使其可读。老实说,我不确定子字符串检测等业务试图实现什么。我强烈建议放弃您的代码并使用Apache Commons FileUpload()--它解决了所有解析问题,并且有一个简单、健壮的API。Matt,你是说在结束-开始之前添加一个密码?