如何设置上传的文件';springmvc中的s路径

如何设置上传的文件';springmvc中的s路径,spring,spring-mvc,Spring,Spring Mvc,我希望上传的文件存储在我指定的文件夹中,如下所示: /SpringMVC/tmp 但它存储在此文件夹中: C:\Users\zhanzhex\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\SpringMVC\tmp\spittr\uploads 这是我的控制器处理上传的方法: @RequestMapping(value="/register

我希望上传的文件存储在我指定的文件夹中,如下所示:

/SpringMVC/tmp 

但它存储在此文件夹中:

C:\Users\zhanzhex\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\SpringMVC\tmp\spittr\uploads    
这是我的控制器处理上传的方法:

@RequestMapping(value="/register", method=RequestMethod.POST)
public String processRegistration(
        @RequestPart(name="profilePicture",required=false) Part profilePicture,
        @Valid Spitter spitter, Errors errors) throws IOException
{
    if (errors.hasErrors())
    {
        System.out.println("find errors");
        return "registerForm";
    }
    profilePicture.write(spitter.getId() + "_profile." + profilePicture.getSubmittedFileName().substring(profilePicture.getSubmittedFileName().indexOf(".") + 1));
    spitterRespository.saveSpitter(spitter);
    return "redirect:/spitter/"+spitter.getId();
}      
我只是在我的
web.xml
中为文件上传配置临时文件夹(
/tmp/spittr/uploads
),但我想在调用控制器方法中的write方法时更改文件夹,似乎我不能。如果我这样调用write方法:

profilePicture.write("/tmp/spittr/uploads/" + spitter.getId() + "_profile.jpg");
它将抛出IOException以指示文件夹不存在:

C:\Users\zhanzhex\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\SpringMVC\tmp\spittr\uploads\tmp\spittr\uploads     
因此,在调用profilePicture.write方法时,我必须删除前缀“/tmp/spittr/uploads”

请参见下面的myweb.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
  <display-name>SpringMVCs</display-name>
  <context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
  </context-param>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>spittr.config.RootConfig</param-value>
  </context-param>

  <listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
  </listener>

  <servlet>
    <servlet-name>spittrAppServlet</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </init-param>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>spittr.config.WebConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <multipart-config>
        <location>/tmp/spittr/uploads</location>
        <max-file-size>2097152</max-file-size>
        <max-request-size>4194304</max-request-size>
    </multipart-config>
  </servlet>
  <servlet-mapping>
    <servlet-name>spittrAppServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
 </web-app>

SpringMVCs
上下文类
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
上下文配置位置
spittr.config.RootConfig
org.springframework.web.context.ContextLoaderListener
spittrAppServlet
org.springframework.web.servlet.DispatcherServlet
上下文类
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
上下文配置位置
spittr.config.WebConfig
1.
/tmp/spittr/上传
2097152
4194304
spittrAppServlet
/

我不知道为什么,我可以更改目标文件夹设置吗?如何做到这一点?

您可以使用MultipartFile或FilePart接口来代替Part

@RequestPart(name="profilePicture",required=false) MultipartFile profilePicture
这些接口公开.transferTo()函数,将数据复制到您可以使用的文件中

profilePicture.transferTo(new File("/path/to/file"));

您可以使用MultipartFile或FilePart接口来代替Part

@RequestPart(name="profilePicture",required=false) MultipartFile profilePicture
这些接口公开.transferTo()函数,将数据复制到您可以使用的文件中

profilePicture.transferTo(new File("/path/to/file"));

不幸的是,似乎“transferTo”方法和“write”方法的工作原理相同,我想我需要另一种方法来确定它是否应该工作,因为您正在传递一个使用绝对路径创建的文件对象。然后,我建议使用MultipartFile接口中的.getInputStream()函数,并将数据从inputStream复制到您选择的目标文件。是的,实际上我使用这种方法,效果很好,但您能否看到我的另一个问题:我不确定此问题是否与所描述的问题相关。不幸的是,似乎是“transferTo”方法的工作原理与“write”方法相同,我想我需要另一种方法来确定它应该是有效的,因为您正在传递一个使用绝对路径创建的文件对象。然后,我建议使用MultipartFile接口中的.getInputStream()函数,并将数据从inputStream复制到您选择的目标文件。是的,实际上我使用这种方法,效果很好,但您能否看到我的另一个问题:我不确定此问题是否与所描述的问题相关。