文件正忙于另一个进程Commons FileUpload Java

时间:2017-11-20 22:32:40

标签: java file apache-commons-fileupload

库commons fileupload。有上传文件的代码:

DiskFileItemFactory factory = new DiskFileItemFactory();

factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

File uploadDir = new File(getRoot() + separator + tempDir);
if (!uploadDir.exists()) {
    uploadDir.mkdir();
}

ServletFileUpload upload = new ServletFileUpload(factory);

@SuppressWarnings("unchecked")
List<FileItem> items = upload.parseRequest(getRequest());

int findex = 0;

for (Object item1 : items) {
    FileItem item = (FileItem) item1;
    if (!item.isFormField()) {

        System.out.println(item.getName());

        File FILE = new File(uploadDir + separator + (new Date().getTime()+findex)+
               getFileExt(item.getName()));

        item.write(FILE);

        getFiles().addFile(new WebFile(getRoot(), getHashes(), item.getFieldName(), 
              FILE.getPath(), FILE.getName(), getFileExt(FILE)));

        findex++;

    }
}

但是当我想删除下载到临时文件夹的文件时会出现问题。

我删除文件如下:Files.deleteIfExists(FILE)。

我收到此错误:

java.nio.file.FileSystemException ::进程无法访问该文件,因为该文件被另一个进程占用。

    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269)
    at sun.nio.fs.AbstractFileSystemProvider.deleteIfExists(AbstractFileSystemProvider.java:108)
    at java.nio.file.Files.deleteIfExists(Files.java:1165)
    at ru.tsyklop.web.WebFile.delete(WebFile.java:114)
    at com.chat2help.model.AdminModel.AddSite(AdminModel.java:597)
    at com.chat2help.model.AdminModel.execute(AdminModel.java:90)
    at com.chat2help.controller.Controller.Admin(Controller.java:77)
    at com.chat2help.controller.Server.doPost(Server.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at com.chat2help.filter.Server.doFilter(Server.java:170)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

有什么不对?

是WebFile.class。我不是在代码中使用OutpupStream和InputStream。

public class WebFile {

private String ROOT;

private String FIELD;

private String TEMP;
private String NAME;
private String EXT;

private boolean FILE;

private WebHashes HASHES;

private final String SEPARATOR = File.separator;

public WebFile() {
    this.FILE = false;
}

public WebFile(String ROOT, WebHashes HASHES, String field, String temp, String name, String ext) {
    if(temp!=null&&!temp.isEmpty()&&new File(temp).exists()) {
        this.FILE = true;
        this.ROOT = ROOT;

        this.FIELD = field;

        this.TEMP = temp;
        this.NAME = name;
        this.EXT = ext;
        this.HASHES=HASHES;
    } else {
        this.FILE = false;
    }
}

private void setFile(boolean file) {
    FILE = file;
}

public boolean isFile() {
    return this.FILE;
}

public String getTemp() {
    return isFile()?TEMP:"";
}

public String getName() {
    return isFile()?NAME:"";
}

public String getExt() {
    return isFile()?EXT:"";
}

public String getField() {
    return isFile()?this.FIELD:"";
}

public Path tempToPath() {
    return isFile()?Paths.get(this.TEMP):Paths.get("");
}

public File tempToFile() {
    return isFile()?tempToPath().toFile():new File("");
}

public String move(String to, boolean replace) throws Exception {
    if(isFile()) {
        return move(HASHES.generate(30), to, replace);
    }
    return "";
}

public String move(String name, String to, boolean replace) throws IOException {
    if(isFile()) {
        File d = new File(to);
        if (!d.exists()) {
            d.mkdir();
        }

        name = name+EXT;

        if(replace) {
            Files.move(Paths.get(TEMP), Paths.get(d + SEPARATOR + name), StandardCopyOption.REPLACE_EXISTING);
        } else {
            Files.move(Paths.get(TEMP), Paths.get(d + SEPARATOR + name));
        }
        setFile(false);
        return name;
    }
    return "";
}

public void delete(){
    if(isFile()){
        setFile(false);
        try {
            Files.deleteIfExists(tempToPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案