工作這是我刪除PDF文件簡單的文件刪除代碼未在Java中
try {
File file = new File(docObjectId + ".pdf");
file.setWritable(true);
System.out.println(file.length());
if (file.delete()) {
System.out.println(file.getName() + " is deleted!");
} else {
System.out.println("Delete operation is failed.");
}
} catch (Exception e) {
e.printStackTrace();
}
它轉到代碼的其他部分的代碼。
PDF文件位於項目根文件夾中,我可以手動刪除它。現在抓我的頭。
這裏是完整的方法。它可能對其他一些原因
public Response getContractDocument(@PathParam("docid") String docObjectId) throws Exception {
DocumentumService documentumService = new DocumentumService(documentumConfigUtil);
DocumentumDocumentBean docDocumentBean = documentumService.getContractDocContent(docObjectId, true);
FileInputStream fileInputStream;
fileInputStream = new FileInputStream(docDocumentBean.getDocFile());
compressPdf(fileInputStream,docObjectId + ".pdf");
fileInputStream = new FileInputStream(docObjectId + ".pdf");
ResponseBuilder responseBuilder = Response.ok((Object) fileInputStream);
try {
File file = new File(docObjectId + ".pdf");
System.out.println(file.getAbsolutePath());
file.setWritable(true);
System.out.println(file.length());
File d = new File(file.getAbsolutePath());
if (d.delete()) {
System.out.println(file.getName() + " is deleted!");
} else {
System.out.println("Delete operation is failed.");
}
} catch(Exception e) {
e.printStackTrace();
}
return responseBuilder.build();
}
你沒有刪除該文件。你是如果聲明只是檢查'文件'是否被刪除。 – Jixone
我頭腦中流行的第一件事是你確定你使用的文件路徑是正確的嗎?如果是親戚,我會檢查以確保它是你的想法。 –
試試這個,if(file.exists()){file.delete(); } – Tehmina