我先把我的代码:
@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()
,它说它不能删除文件。它是解压缩后删除 zip 的理想选择。
编辑:我只能在post(InputStream zip)
返回后删除文件。如果我从post
方法中调用 delete,它不会删除,因为 post 没有返回。有没有解决的办法?