私は自分のコードを最初に置きます:
@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)
ます。メソッド内から delete を呼び出しpost
ても、post が返されていないため、削除されません。これを回避する方法はありますか?