springmvc使用servlet3.0上传文件错误 20C

文件结构,设定的file在WebContent下

WebAppInitializer配置如下

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{

    @Override
    protected Class<?>[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return new Class<?>[]{RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class<?>[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[]{"/"};
    }
//  @Override
//   protected void customizeRegistration(Dynamic registration) {
//      registration.setMultipartConfig(
//          new MultipartConfigElement("/file", 2097152, 4194304, 0));
//    }
    @Override  
    protected void customizeRegistration(ServletRegistration.Dynamic registration) {  
        registration.setMultipartConfig(getMultipartConfigElement());  
    }  

    private MultipartConfigElement getMultipartConfigElement() {  
        MultipartConfigElement multipartConfigElement = new MultipartConfigElement( LOCATION, MAX_FILE_SIZE, MAX_REQUEST_SIZE, FILE_SIZE_THRESHOLD);  
        return multipartConfigElement;  
    }  

    private static final String LOCATION = "/file/"; // Temporary location where files will be stored  

    private static final long MAX_FILE_SIZE = 5242880; // 5MB : Max file size.  
                                                        // Beyond that size spring will throw exception.  
    private static final long MAX_REQUEST_SIZE = 20971520; // 20MB : Total request size containing Multi part.  

    private static final int FILE_SIZE_THRESHOLD = 0;
}

WebConfig.class配置如下


    @Bean(name = "multipartResolver")  
    public StandardServletMultipartResolver resolver() {  
        return new StandardServletMultipartResolver();  
    }  

public class User {
private MultipartFile multipartFile;
get set
}

controller配置如下

@Controller
public class WebControllerr {
    @Autowired
    UserService userService;
    @Autowired
    HttpServletRequest httpServletRequest;

    @RequestMapping(value="/photo")
    public String update(Model model){
        model.addAttribute("user", new User());
        return "photo";
    }

    @RequestMapping("/filedown")
    public void fileDownLoad(HttpServletRequest servletRequest,
            @ModelAttribute User user,Model model){
        MultipartFile multipartFile=user.getMultipartFile();
        String filename=multipartFile.getOriginalFilename();
        try {
            File file=new File(servletRequest.getServletContext().getRealPath("/file"),filename);
            multipartFile.transferTo(file);
        } catch (Exception e) {
            e.printStackTrace();
        }

Could not complete request
org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: The temporary upload location [C:\Tomcat7\work\Catalina\localhost\WebProtect3\file] is not valid
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:99)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.(StandardMultipartHttpServletRequest.java:77)
at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:75)
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1041)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:887)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.logging.log4j.core.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:66)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:122)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java:170)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)

5个回答

是不是在页面form 里少个属性 enctype="multipart/form-data"
图片说明

qq_29789613
qq_641843909 回复BestZhao:你加上这个试试 public void readExcel(String name,@RequestParam(value="excel",required=false) MultipartFile file
2 年多之前 回复
qq_37910658
BestZhao 有这个<form:form commandName="user" action="filedown" method="post" enctype="multipart/form-data"> <input type="file" name="multipartFile" /> <input type="submit" value="提交"> </form:form>
2 年多之前 回复
qq_37910658
BestZhao 写这个了
2 年多之前 回复

加上上面的属性之后还要在controller配置如下里面配置@RequestPart

我在eclipse下面直接WEB-INF下面建立文件夹file,把/WEB-INF/file设置为临时缓存文件,但是文件上传就会报错误,我调试了就是路径问题,我现在还不是很理解tomcat的工作原理和文件编译和存放,所以现在不太好解决

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问