就在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();
答案 0 :(得分:0)
如何计算endPos和startPos?您的问题几乎肯定是这些值中的一个或两个都是错误的。
答案 1 :(得分:0)
您的代码假定了一些有关请求的内容:它包含一定数量的换行符,=
字符的位置等。最好在代码中添加检查以确保{{ 1}},这些假设都是正确的,因为突然出现的错误会显示出来。