Tomcat创建0字节文件

发布于 2024-09-07 19:03:42 字数 1596 浏览 3 评论 0原文

我在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();
    }

I have a very simple file upload mechanism in java. I just take the file and save it on the server. I'm testing this simple code with selenium and when a timeout occurs in the selenium test tomcat creates 0 byte files under tomcat_home/work/Catalina/localhost/uploadServlet/ directory as MultiPart* files. It creates thousands of files, until there is no disk space left on device. What may cause this problem? How can I solve this? Is there anyone has an idea about this?

My environment is: Ubuntu - 8.04 server, apache tomcat - 5.5.29, sun java 1.6

Thanks,

Here is the code snippet that i use

    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();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别把无礼当个性 2024-09-14 19:03:42

我使用了 apache commons file upload 类,并且删除了最后部分中的临时文件。通过这个实现,问题得到了解决。

I've used apache commons file upload class and i've deleted the temporary files in the finally section. The problem is solved with this implementation.

找回味觉 2024-09-14 19:03:42

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

Is it possible that some sort of exception is occuring in the middle of this code? Best to close the streams in a finally block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文