我编写了一个servlet来使用表单上传文件。在我使用绝对路径之前:
String path = "C:\\Folder\\SubFolder\\";
并使用此路径保存我上传的文件,并且工作正常。我将上传的路径更改为:String path = request.getServletContext().getRealPath("")+"\\Folder\\SubFolder\\";
,现在我发现文件会在一段时间后自动删除。
我的问题是,它是否被视为临时文件并在一段时间后被删除或是否是其他原因?这是我的代码:
uploadFile file = new uploadFile();// upload the file!
boolean checker = false;
String uploadPath = request.getServletContext().getRealPath("")+"\\uploads\\riparazione\\";
checker = file.doImport(idRiparazione, request, response, sourceFile, uploadPath);
doImport
功能如下:
public boolean doImport(String name, HttpServletRequest request, HttpServletResponse response, FileItem item, String uploadPath) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(THRESHOLD_SIZE);
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(MAX_FILE_SIZE);
upload.setSizeMax(MAX_REQUEST_SIZE);
// creates the directory if it does not exist
File uploaddir = new File(uploadPath);
if ( !uploaddir.exists())
uploaddir.mkdirs();
String fileName = new File(item.getName()).getName();
fileName = name + "-" + fileName;
String filePath = uploadPath + File.separator + fileName;
try {
// saves the file on disk
File storeFile = new File(filePath);
if ( !storeFile.exists())
item.write(storeFile);
else return false;
} catch(Exception e) {
e.printStackTrace();
}
return true;
}
我正在使用xampp作为Apache tomcat 7.0,eclipse和OS是windows 7.