1

我在java中有一个非常简单的文件上传机制。我只是把文件保存在服务器上。我正在用 selenium 测试这个简单的代码,当 selenium 测试中发生超时时,tomcat 在 tomcat_home/work/Catalina/localhost/uploadServlet/ 目录下创建 0 字节文件作为 MultiPart* 文件。它会创建数千个文件,直到设备上没有剩余磁盘空间。什么可能导致这个问题?我该如何解决这个问题?有人对此有任何想法吗?

我的环境是:Ubuntu - 8.04 服务器,apache tomcat - 5.5.29,sun 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();
    }
4

2 回答 2

1

我使用了apache commons 文件上传类,并删除了 finally 部分中的临时文件。这个实现解决了这个问题。

于 2010-11-22T14:32:15.020 回答
0

这段代码中间是否可能发生某种异常?最好在一个finally块中关闭流。

于 2010-07-01T11:52:51.270 回答