我之前的帖子被刪除了,所以我在這裏重新發布,因爲我沒有得到任何消除此錯誤的線索。當嘗試上傳服務器上的文件時發生java.lang.IndexOutOfBoundsException
錯誤:
java.lang.IndexOutOfBoundsException
java.io.FileOutputStream.writeBytes(Native Method)
java.io.FileOutputStream.write(FileOutputStream.java:297)
com.ninenexus.simplesign.storeanddisplay.StoreAndDisplayImage.doPost(StoreAndDisplayImage.java:85)
javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
代碼:
String contentType = request.getContentType();
if ((contentType != null)
&& (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
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;
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;
saveFile = "d:/" + saveFile;
try
{
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos)); // Line 85
fileOut.flush();
fileOut.close();
}
catch(...)
{
...
}
}
else
{
...
}
線的輸出無。 85 is
dataBytes=[[email protected], startPos=142, (endPos - startPos)=75944.
我想從用戶通過輸入type ='文件',然後將其保存在使用此代碼的目錄中的文件。它在tomcat服務器上正常工作。文件被保存在目錄中。但是當我創建戰爭文件並將其上傳到服務器上時。嘗試在主服務器上運行此代碼時,出現此錯誤。沒有解決這個問題的線索。幫助真的很感激。
你能打印'dataBytes.length'嗎? – partlov
'dataBytes.length = 76132' –
@ shabbir.rang可以肯定的是,這些數字來自您的主服務器(您從哪裏得到錯誤)? – Henry