У меня есть веб-служба RESTful, в которой есть функция загрузки файла.Это работает хорошо, но моя проблема в том, что когда я пытаюсь удалить загруженный файл вручную в Windows, он говорит, что файл уже используется.
Чтобы удалить эти файлы, мне нужно остановить сервер Glassfish.Меня беспокоит использование памяти, может быть, этот неуправляемый код вызовет некоторые проблемы, когда пользователь продолжает загружать много и большие файлы.Я уже закрыл переменные InputStream
и FileOutputStream
.Пожалуйста, смотрите ниже код, я не знаю, что мне не хватает.
@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public String upload(@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail, @Context HttpServletRequest request) {
String json = "";
FileOutputStream out = null;
try {
File dir = new File(new File(".").getCanonicalPath() + File.separatorChar + "incidentreport" + File.separatorChar + "uploaded" + File.separatorChar + deviceId);
dir.mkdirs();
String location = dir.getAbsolutePath() + File.separatorChar + fileDetail.getFileName();
out = new FileOutputStream(new File(location));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(location));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
com.incidentreport.model.File file = new com.incidentreport.model.File(fileDetail.getFileName(), false);
//FLAG FILE AS UPLOADED
Response response = file.uploaded(deviceId, device.getId(), Util.toSqlTimestamp(dateUtc, "yyyy-MM-dd HH:mm:ss"));
//DELETE UPLADED FILE IF FLAGGING NOT SUCCEED
if(response.getStatus() != Response.SUCCESS) new File(location).delete();
json = new Gson().toJson(response);
} catch (Exception ex) {
Logger.getLogger(FileResource.class.getName()).log(Level.SEVERE, null, ex);
json = new Gson().toJson(new Response(Response.ERROR, ex.getMessage()));
} finally {
try {
uploadedInputStream.close();
uploadedInputStream = null;
out.flush();
out.close();
out = null;
} catch (IOException ex) {
Logger.getLogger(FileResource.class.getName()).log(Level.SEVERE, null, ex);
json = new Gson().toJson(new Response(Response.WARNING, ex.getMessage()));
}
}
return json;
}