Tomcat创建0字节文件 [英] Tomcat creates 0 byte files

查看:177
本文介绍了Tomcat创建0字节文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我的环境是:Ubuntu-8.04服务器,apache tomcat-5.5.29,sun java 1.6

My environment is: Ubuntu - 8.04 server, 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();
    }

推荐答案

我使用过 apache通用文件上传类,并且我已经在finally部分中删除了这些临时文件.这个实现解决了这个问题.

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.

这篇关于Tomcat创建0字节文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆