springmvc上傳文件報錯org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]


在用springmvc+mybatis進行項目開發時,上傳文件拋異常...

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.web.multipart.MultipartFile]: Specified class is an interface
    org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveModelAttribute(HandlerMethodInvoker.java:762)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:356)
    org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

解決方法:在Controller的MultipartFile參數前面加上@RequestParam就行了!

如:

@RequestMapping("/save")
    public String save(User user,
            HttpServletResponse response,
            @RequestParam MultipartFile file_pic//接收圖片
            )throws Exception{
        
        //原始名稱
        String originalFilename = file_pic.getOriginalFilename();
        //上傳圖片
        if(file_pic!=null && originalFilename!=null && originalFilename.length()>0){
            //存儲圖片的物理路徑
            String pic_path = "G:\\nfsysuems\\temp\\";
            //新的圖片名稱
            String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            //新圖片
            File newFile = new File(pic_path+newFileName);
            //將內存中的數據寫入磁盤
            file_pic.transferTo(newFile);
            //新圖片寫入user
            user.setPic(newFileName);
        }
      .......

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



猜您在找 Spring使用mutipartFile上傳文件報錯【Failed to instantiate [org.springframework.web.multipart.MultipartFile]】 org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.hs.model.StudentModel]: No default constructor found; nested exception is java.lang.NoSuchMethodException: c 關於org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]:Specified class is an interface Springboot啟動報錯Error creating bean with name 'xxx' defined in URL [xxx]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate... Java之——java.lang.NoSuchMethodException: [org.springframework.web.multipart.MultipartFile;.() Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface解決方案 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/ 上傳文件Request processing failed;nested exception is org.springframework.web.multipart.MultipartException:Failed to parse multipart servlet request;multipart/form-data request failed.(**沒有權限**) Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'URIType' of bean class 新建項目用flyway數據遷移時報錯找不到flyway org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource
 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM