开发者

File Upload using Spring MVC

开发者 https://www.devze.com 2023-04-01 18:39 出处:网络
I am new to Spring MVC though not new to Java and working primary on Struts2 and Wicket as my choice for web development

I am new to Spring MVC though not new to Java and working primary on Struts2 and Wicket as my choice for web development

I am trying to do a POC of file upload using spring MVC here is my jsp file

<form  id="fileuploadForm" action="fileupload" method="POST" enctype="multipart/form-data" >
            <fieldset>
                <legend>Upload Fields</legend>

               <input id="file" type="file" name="file" />
                <p><button type="submit">Upload</button></p> 

            </fieldset>
        </form>

and my Controller is

@Controller
@RequestMapping("FileUpload/fileupload")
public class FileUploadController{



    public ModelAndView processUpload(@RequestParam MultipartFile file, WebRequest webRequest, Model model) {


        String orgFileName = file.getOriginalFilename();
        String filePath = "data/input" + orgFileName;
        ModelMap modelMap = new ModelMap(); 
        System.out.println("*******************************************");
        File dest = new File(filePath);
        try {
            file.transferTo(dest);
        } catch (IllegalStateException e) {
            e.printStackTrace();
            modelMap.addAttribute("result", "File uploaded failed:" + orgFileName);
            return new ModelAndView("results", modelMap);
                //return "File uploaded failed:" + orgFileName;
        } catch (IOException e) {
            e.printStackTrace();
            modelMap.addAttribute("result", "File uploaded failed:" + orgFileName);
            return new ModelAndView("results", modelMap);
        }


        modelMap.addAttribute("result", "File uploaded " + orgFileName);
        return new ModelAndView("results", modelMap);



    }

below is the entry for dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

     <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
         p:suffix=".jsp" />

    <bean name="/*" class="com.app.controller.FileUploadController"/>
</beans>

i tried google also but not able to get any help may be not able to find good resource due to lack of knowledge of spring MVC where ever i am hitting my uplaod button i am getting 404 error for this URL

http://localhost:7777/FileUpload/fileupload

i am sure i am doing some configuration mistake but not able to point it out, any help in this will be very helpful

web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>SpringExample17</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-na开发者_如何学运维me>
        <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
        org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>    
</web-app>

Thanks in advance


There are several things to note:

  • You have mapped only URLs ending on .htm to your Spring Dispatcher, so the Controller can never see your request.
  • If FileUpload is the name of your Web-App, you need to remove it from the @RequestMapping annotation.
  • <bean name="/*" class="com.app.controller.FileUploadController"/> should be changed to <bean name="/fileupload.htm" class="com.app.controller.FileUploadController"/>

HTH


I'm always starting my mappings with a forward slash, so try:

 @RequestMapping("/FileUpload/fileupload")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号