嗨,大家好我一直在尋找解決在java中的錯誤500。我已經閱讀了同一主題的另一個問題,但我無法修復它。你們能幫我解決我的錯誤嗎?錯誤500:java.lang.StringIndexOutOfBoundsException
我使用HTML輸入文件。將文件上傳到C文件夾中:此代碼適用於小於1或2kb的文件,但當我上傳較大的文件時,我會得到Out Of Bounds Index Error。如果重複,請提前致謝並對不起。
String saveFile = new String();
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 = "C:/uploadDir2/" + saveFile;
File ff = new File(saveFile);
try{
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
}//fin try
catch(Exception e){
// out.println(e);
}//fin catch
}//fin while
}//finif
嘿感謝您的回答不幸,它沒有工作,因爲getContentLengthLong()是未定義的類型HttpServletRequest任何建議?提前致謝! –