2014-01-21 104 views
0

朋友你好保存圖像,在Web應用程序創建的文件夾,並在

我有以下代碼在文件夾的WebContent一個圖像上傳到我的Web應用程序 - >圖像 - > menuitemimg

private String doUploadFile(String menuItemName,Long menuItemId){ 
     FacesContext facesContext = FacesContext.getCurrentInstance(); 
     ExternalContext externalContext = facesContext.getExternalContext(); 
     InputStream inputStream = null; 
     OutputStream out = null; 
     String imageName = null; 
     try{ 
      String dirPath = externalContext.getRealPath(File.separator+"images"+File.separator+"menuitemimg"); 
      File targetFolder = new File(dirPath); 
      targetFolder.mkdirs(); 
      imageName = file.getFileName(); 
      imageName = menuItemName+"_"+menuItemId+"."+FilenameUtils.getExtension(imageName); 
      inputStream = file.getInputstream(); 
      out = new FileOutputStream(new File(targetFolder+File.separator+imageName)); 
      int read = 0; 
      byte[] bytes = new byte[1024]; 
      while ((read = inputStream.read(bytes)) != -1) { 
       out.write(bytes, 0, read); 
      } 
      inputStream.close(); 
      out.flush(); 
      out.close(); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return imageName; 
} 

的代碼工作正常。但是當我重新啓動服務器時,menuitemimg文件夾中上傳的圖像會消失。我不想將圖像存儲在光盤或數據庫中。所以我想要做的。

在此先感謝。

+2

您不應該將圖像保存在您的Web應用程序中,就好像這更像是臨時文件夾,每次上傳新的WAR文件時都會刷新該臨時文件夾。 – Makky

+2

保存到不同的文件夾(外部戰爭)或數據庫。 – Makky

+0

感謝您的重播Makky。我會按照你的建議。 –

回答

1

我經歷了你的代碼,看來你是在你的項目文件夾本身存儲圖像。 我建議你將圖片上傳到其他地方以外的容器

相關問題