web專案中將上傳的檔案儲存到伺服器的某個資料夾,保證clean tomcat時不會被清理掉
阿新 • • 發佈:2019-01-09
String testPath = request.getSession().getServletContext().getRealPath("");//tomcat下
File directory = new File(testPath);//設定為當前資料夾
File file = new File(directory.getParentFile().toString()).getParentFile();
testPath = file.toString();
testPath = testPath+"/briefing/Template/"+fileUrl.getOriginalFilename();
File fileIsIsts = new File(testPath);
if (!fileIsIsts.getParentFile().exists())
fileIsIsts.getParentFile().mkdirs();
if (!fileIsIsts.getParentFile().getParentFile().exists())
fileIsIsts.getParentFile().getParentFile().mkdirs();
fileUrl.transferTo(new File(testPath));
File directory = new File(testPath);//設定為當前資料夾
File file = new File(directory.getParentFile().toString()).getParentFile();
testPath = file.toString();
testPath = testPath+"/briefing/Template/"+fileUrl.getOriginalFilename();
File fileIsIsts = new File(testPath);
if (!fileIsIsts.getParentFile().exists())
fileIsIsts.getParentFile().mkdirs();
if (!fileIsIsts.getParentFile().getParentFile().exists())
fileIsIsts.getParentFile().getParentFile().mkdirs();
fileUrl.transferTo(new File(testPath));