我正试图从URL打开一个文件,将其复制到一个临时文件,然后在处理工作完成后删除该临时文件。但是,我无法删除该文件。我已经尝试关闭所有流,我已经尝试使用deleteOnExit()方法和Delete()方法进行删除。这是我正在处理的一个excel文件。当我不使用excel文件,或者更具体地说,不使用下面代码中显示的工作簿对象时,该文件删除得很好。一旦我将文件传递给workbookFactory.create()方法,代码就无法删除我的文件。
下面是代码:
public class URLtoFileWriting {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
static File destinationFile;
public static void getFile() throws IOException {
try {
// TODO code application logic here
URL fileURL = new URL("http://www.testURL.com/testfile.xlsx");
URLtoFileWriting.destinationFile = File.createTempFile("remotefile",".xlsx");
try {
URLConnection URLconnection = fileURL.openConnection();
InputStream inputStream = URLconnection.getInputStream();
FileOutputStream fileoutputStream = new FileOutputStream(URLtoFileWriting.destinationFile);
IOUtils.copy(inputStream, fileoutputStream);
Workbook wb = WorkbookFactory.create(URLtoFileWriting.destinationFile);
System.out.println(URLtoFileWriting.destinationFile.getAbsolutePath());
inputStream.close();
fileoutputStream.close();
System.out.println("Deleted testWB?!" + URLtoFileWriting.destinationFile.delete());
} catch (IOException ex) {
Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidFormatException ex) {
Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我正在使用的所有进口商品:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
我怀疑这与workbookFactory.create()方法有关,因为一旦我将文件传递给该方法,删除就变得不可能了。
我在这里做错了什么?
更新。我遇到了一个解决方案:您可以将File传递给FileInputStream,然后将此流传递给WorkbookFactory.Create()方法。
更新了以下代码以反映这一点:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class URLtoFileWriting {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
static File destinationFile;
public static void getFile() throws IOException {
try {
// TODO code application logic here
URL fileURL = new URL("http://www.testURL.com/testfile.xlsx");
URLtoFileWriting.destinationFile = File.createTempFile("remotefile",".xlsx");
try {
URLConnection URLconnection = fileURL.openConnection();
InputStream inputStream = URLconnection.getInputStream();
FileOutputStream fileoutputStream = new FileOutputStream(URLtoFileWriting.destinationFile);
IOUtils.copy(inputStream, fileoutputStream);
FileInputStream FIS = new FileInputStream(URLtoFileWriting.destinationFile);
Workbook wb = WorkbookFactory.create(FIS);
FIS.close();
inputStream.close();
fileoutputStream.close();
System.out.println(URLtoFileWriting.destinationFile);
System.out.println(URLtoFileWriting.destinationFile.delete());
} catch (IOException ex) {
Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidFormatException ex) {
Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(URLtoFileWriting.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我希望这能有所帮助。
在这里找到解决方案Close Filehandle for Workbook(apache poi)。
使用NPOIFSFileSystem#close()或OPCPackage#close。
有关更多信息,请查看WorkbookFactory类的重载构造函数。