DiskFileItemを使用しているときに、無決定なファイル削除エラーがあるのはなぜですか?
-
25-10-2019 - |
質問
私のアップロードサーブレットは、(コードの端の近くで)交換しようとしているファイルを(一見)ランダムで削除できないという例外を投げ続けています。ストリームを使用しておらず、ブラウザでファイルが開いていないため、何が原因であるのかわかりません。何がこれを引き起こしているのか知っている人はいますか?コードが私には正しいように見えるので、私はこれについて完全に無知です。 DiskFileItemを使用したのはこれが初めてなので、そこで処理するニュアンスがあるかどうかはわかりません。
時には機能することもありますが、時にはそうではないことに留意してください。私はそれで迷子になります。
問題領域:
File destination = new File(wellnessDir + File.separator + fileName + ".pdf");
System.out.println("destination file exists: " + destination.exists());
System.out.println("file to be moved exists: " + uploadedFile.exists());
if(destination.exists()){
boolean deleted = destination.delete();
if(!deleted)
throw new Exception("Could not delete file at " + destination);
}
私のシステムアウトは、ファイルと宛先の両方が存在すると常に言っています。既存のファイルを上書きするためにアップロードを取得しようとしています。
完全なコード:(& パステビン)
private void uploadRequestHandler(ServletFileUpload upload, HttpServletRequest request)
{
// Handle the request
String fileName = "blank";
try{
List items = upload.parseRequest(request);
//Process the uploaded items
Iterator iter = items.iterator();
File uploadedFile = new File(getHome() + File.separator + "temp");
if(uploadedFile.exists()){
boolean tempDeleted = uploadedFile.delete();
if(!tempDeleted)
throw new Exception("Existing temp file could not be deleted.");
}
//write the file
while (iter.hasNext()) {
DiskFileItem item = (DiskFileItem) iter.next();
if(item.isFormField()){
String fieldName = item.getFieldName();
String fieldValue = item.getString();
if(fieldName.equals("fileName"))
fileName = fieldValue;
//other form values would need to be handled here, right now only need for fileName
}else{
item.write(uploadedFile);
}
}
if(fileName.equals("blank"))
throw new Exception("File name could not be parsed.");
//move file
File wellnessDir = new File(getHome() + File.separator + "medcottage" + File.separator + "wellness");
File destination = new File(wellnessDir + File.separator + fileName + ".pdf");
System.out.println("destination file exists: " + destination.exists());
System.out.println("file to be moved exists: " + uploadedFile.exists());
if(destination.exists()){
boolean deleted = destination.delete();
if(!deleted)
throw new Exception("Could not delete file at " + destination);
}
FileUtil.move(uploadedFile, new File(wellnessDir + File.separator + fileName + ".pdf"));
writeResponse();
} catch (Exception e) {
System.out.println("Error handling upload request.");
e.printStackTrace();
}
}
編集:追加するには、gethome()と「home」は実際にはコードにありません。それは私のホームパスを保護するためだけです
解決
多くのテストと悪化の後、最終的に別のマシンでそれを試しました、同じコードはうまく機能しました。ワークマシンでドメインを転送することと関係があり、許可をいじります。
所属していません StackOverflow