我在java中有一個非常簡單的文件上傳機制。我只是把這個文件保存在服務器上。我正在用selenium測試這個簡單的代碼,並且當在selenium測試中發生超時時,tomcat會在作爲MultiPart *文件的tomcat_home/work/Catalina/localhost/uploadServlet /目錄下創建0個字節的文件。它會創建數千個文件,直到設備上沒有磁盤空間。什麼可能導致這個問題?我該如何解決這個問題?有沒有人對此有所瞭解?Tomcat創建0字節文件
我的環境是:Ubuntu的 - 8.04服務器,Apache Tomcat上 - 5.5.29,太陽的Java 1.6
感謝,
這裏是代碼片段,我使用
String strFileName = request.getParameter("FileName");
String strPath = request.getParameter("Path");
File fFile = (File) request.getAttribute("Content");
int index = strPath.length() - 1;
if (strPath.charAt(index) != '/') {
strPath += "/";
}
if (! new File(strPath).exists()) {
new File(strPath).mkdirs();
}
File file = new File(strPath + strFileName);
FileOutputStream fileOutputStream = new FileOutputStream(file);
FileInputStream fileInputStream = new FileInputStream(fFile);
byte[] bBuf = new byte[1024];
int iBufLen = 0;
int iReadLen = 1024;
int iTotelLen = 0;
/*read 1024 bytes at a time*/
while ((iBufLen = fileInputStream.read(bBuf)) != -1) {
fileOutputStream.write(bBuf);
fileOutputStream.flush();
iTotelLen += iBufLen;
if (fileInputStream.available() < iReadLen) {
iReadLen = fileInputStream.available();
break;
}
}
byte[] tempbBuf = new byte[iReadLen];
fileInputStream.read(tempbBuf, 0, iReadLen);
fileOutputStream.write(tempbBuf);
fileOutputStream.close();
fileInputStream.close();
if (fFile.exists()) {
fFile.delete();
}
請幫助我們通過發佈您的實際代碼來幫助您:-) – 2010-07-01 11:18:17
在完成寫入文件後,您是否在'OutputStream'或'Writer'上調用'flush()'和/或close()'? – Jesper 2010-07-01 11:22:54
我編輯了帖子並插入了簡單代碼 – 2010-07-01 11:25:48