2014-01-11 68 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(); 
}