当文件是参数时,java不能删除它
我会把我的代码放在第一位:
@Post
public Representation post(InputStream zip) throws Throwable {
createFile(zip, "C:/temp\abc.zip");
return new StringRepresentation("File uploaded");
}
public void createFile(InputStream zipStream, uploadedFileLocation) throws Exception {
try {
writeToFile(zipStream, uploadedFileLocation);
FileUtils.forceDelete(new File(uploadedFileLocation));
} catch (Exception e) {
throw e;
}
}
private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
uploadedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我正在尝试制作一个允许用户上传zip文件的服务器。然后服务器将zip文件写入磁盘,解压,然后删除zip,同时将解压部分保留在服务器上。但是,当我将zip文件发送到服务器时,它无法被删除。当使用FileUtils.forceDelete()
时,它表示无法删除该文件。这是理想的拉链被删除后,解压缩
编辑:我只能在post(InputStream zip)
返回后删除文件。如果我在post
方法中调用delete,它不会删除,因为post没有返回。有办法吗
共 (0) 个答案