2014-01-11 75 views
0

我有這樣的項目結構,資源目錄。這一次,我有這樣的代碼:保存文件使用Spring


public String fileUpload(UploadedFile uploadedFile) { 
     InputStream inputStream = null; 
     OutputStream outputStream = null; 
     MultipartFile file = uploadedFile.getFile(); 
     String fileName = file.getOriginalFilename(); 
     File newFile = new File("/res/img/" + fileName); 

     try { 
      inputStream = file.getInputStream(); 

      if (!newFile.exists()) { 
       newFile.createNewFile(); 
      } 
      outputStream = new FileOutputStream(newFile); 
      int read = 0; 
      byte[] bytes = new byte[1024]; 

      while ((read = inputStream.read(bytes)) != -1) { 
       outputStream.write(bytes, 0, read); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return newFile.getAbsolutePath(); 
    } 

但將文件保存到user.dir目錄,這是~/Work/Tomcat/bin/。 那麼我怎樣才能將文件上傳到res目錄?

+2

不要在那裏上傳文件。選擇一個專用於該目錄的單獨目錄。 –

+0

@SotiriosDelimanolis爲什麼'res'目錄不合適? –

+0

首先,因爲它不一定存在。 'Servlet'容器可能選擇不擴展你的'.war'。 –

回答

10

你不應該在那裏上傳文件。

如果您使用的是戰爭,重新部署將會刪除它們。如果他們打算是臨時的,那麼使用指定的臨時位置。

如果您打算之後發佈這些文件,請選擇一個位置來將文件存儲在您的服務器上,使該位置對於應用程序來說是已知的,並保存並從該位置加載文件。

如果你試圖取代資源動態,如這是在HTML或CSS模板引用的圖像,然後再考慮單獨發佈外部位置,您可以使用MVC:資源此例如:

<mvc:resources mapping="/images/**" location="file:/absolute/path/to/image/dir"/> 

,你會將你的文件保存到該位置。這將使它在部署之間更加永久。

要使用你的代碼的圖像保存到該位置,你將需要添加到您的bean定義(假設你使用沒有註釋的XML配置):

<property name="imagesFolder" value="/absolute/path/to/image/dir"/> 

,並保持你的代碼儘可能相似將其更改爲:

private String imagesFolder; 
public void setImagesFolder(String imagesFolder) { 
    this.imagesFolder = imagesFolder; 
} 
public String fileUpload(UploadedFile uploadedFile) { 
    InputStream inputStream = null; 
    OutputStream outputStream = null; 
    MultipartFile file = uploadedFile.getFile(); 
    String fileName = file.getOriginalFilename(); 
    File newFile = new File(imagesFolder + fileName); 

    try { 
     inputStream = file.getInputStream(); 

     if (!newFile.exists()) { 
      newFile.createNewFile(); 
     } 
     outputStream = new FileOutputStream(newFile); 
     int read = 0; 
     byte[] bytes = new byte[1024]; 

     while ((read = inputStream.read(bytes)) != -1) { 
      outputStream.write(bytes, 0, read); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return newFile.getAbsolutePath(); 
} 

請記住,您需要更改/絕對/路徑/到/圖像/目錄到一個真實存在的實際路徑,也向我推薦一下Spring Resources documentation更好的方法處理文件和資源。

+0

謝謝!你能告訴我,我可以在代碼中調用'/ absolute/path/..'來存儲數據嗎?' –

+0

我的意思是另一個。 是否有機會通過方法調用在控制器中接收此地址?或者我需要永遠寫下這條路? –

3

請參考 here的FileUploadController將文件保存到指定的目錄。

public String fileUpload(UploadedFile uploadedFile) { 
    InputStream inputStream = null; 
    OutputStream outputStream = null; 
    MultipartFile file = uploadedFile.getFile(); 

    String rootPath = System.getProperty("user.dir"); 
    File dir = new File(rootPath + File.separator + "webapp"+File.separator+"res"+File.separator+"img"); 
    if (!dir.exists()) 
     dir.mkdirs(); 
    String fileName = file.getOriginalFilename(); 
    File serverFile = new File(dir.getAbsolutePath() + File.separator + fileName); 

    try { 
     inputStream = file.getInputStream(); 

     if (!newFile.exists()) { 
      newFile.createNewFile(); 
     } 
     outputStream = new FileOutputStream(newFile); 
     int read = 0; 
     byte[] bytes = new byte[1024]; 

     while ((read = inputStream.read(bytes)) != -1) { 
      outputStream.write(bytes, 0, read); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return newFile.getAbsolutePath(); 
}