내 업로드 서블릿에서 예외적으로 (코드의 끝 부분에서) 대체하려고하는 파일을 (겉으로보기에) 무작위로 삭제할 수 없다는 메시지가 나타납니다. 스트림을 사용하지 않고 파일이 내 브라우저에서 열리지 않기 때문에이 문제의 원인을 알 수 없습니다. 아무도이 원인을 알 수 있습니까? 나는이 코드에 대해 완전히 우둔한데, 코드가 맞다. DiskFileItem을 처음 사용하기 때문에 거기에서 처리 할 뉘앙스가 있는지 확실하지 않습니다.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);
}
내 시스템 아웃은 항상 모두 파일 및 대상이 존재한다는 것을 말한다. 기존 파일을 덮어 쓸 업로드를 얻으려고합니다.
전체 코드 : (& pastebin)
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를 (추가) 및 "집"그건 그냥 내 집에 경로를 보호 할 수있어, 코드에서 정말없는
Windows 또는 Linux를 사용하고 있습니까? –
나는 창문을 사용하고있다. – buddyp450
Windows에서 인덱싱을 설정 했습니까? 때로는 새 디렉토리를 색인하기 시작하므로 삭제할 수 없습니다. 색인 생성을 해제하고 그 결과가 향상되는지 확인하십시오. –