1. 程式人生 > >web專案中將上傳的檔案儲存到伺服器的某個資料夾,保證clean tomcat時不會被清理掉

web專案中將上傳的檔案儲存到伺服器的某個資料夾,保證clean tomcat時不會被清理掉

 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));