【发布时间】:2011-03-10 14:04:02
【问题描述】:
我在java中有一个非常简单的文件上传机制。我只是把文件保存在服务器上。我正在用 selenium 测试这个简单的代码,当 selenium 测试中发生超时时,tomcat 在 tomcat_home/work/Catalina/localhost/uploadServlet/ 目录下创建 0 字节文件作为 MultiPart* 文件。它会创建数千个文件,直到设备上没有剩余磁盘空间。什么可能导致这个问题?我该如何解决这个问题?有人对此有想法吗?
我的环境是:Ubuntu - 8.04 server,apache tomcat - 5.5.29,sun java 1.6
谢谢,
这是我使用的代码 sn-p
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();
}
【问题讨论】:
-
请通过发布您的实际代码帮助我们帮助您:-)
-
写完文件后,您是否在
OutputStream或Writer上调用flush()或close()? -
我已经编辑了帖子并插入了简单的代码
-
使用 available() 对流泵来说是不正确的!您只能依赖 read() 的返回。 available() 只是“即时”检查;在该调用之后,某些字节可能“很快”可用,例如在较低级别的流控制恢复之后。