I wrote a code to upload file using spring mvc.not showing any errors or warning but still getting 404 error after compiling it -
i wrote code upload file in directory named images. every time run on eclipse using apache tomcat 7 gives me http status 404 - /fileupload/. using maven plugin in eclipse build spring framework. gives build success. while compiling apache , running on server there http error 404. have attached jsp codes , xml codes along question. please help
pom.xml
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.upload</groupid> <artifactid>fileupload</artifactid> <version>0.0.1-snapshot</version> <packaging>war</packaging> <build> <sourcedirectory>src</sourcedirectory> <plugins> <plugin> <artifactid>maven-compiler-plugin</artifactid> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactid>maven-war-plugin</artifactid> <version>2.3</version> <configuration> <warsourcedirectory>webcontent</warsourcedirectory> <failonmissingwebxml>false</failonmissingwebxml> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupid>commons-fileupload</groupid> <artifactid>commons-fileupload</artifactid> <version>1.3.1</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>3.8.2</version> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-aop</artifactid> <version>4.0.4.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-beans</artifactid> <version>4.0.4.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>4.0.4.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>4.0.4.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>4.0.0.release</version> </dependency> </dependencies> </project>
hellocontroller.java
package com.upload; import java.io.bufferedoutputstream; import java.io.file; import java.io.fileoutputstream; import javax.servlet.servletcontext; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; import org.apache.commons.fileupload.disk.diskfileitemfactory; import org.apache.commons.fileupload.servlet.servletfileupload; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import org.springframework.web.multipart.commons.commonsmultipartfile; import org.springframework.web.servlet.modelandview; import org.springframework.web.servlet.config.annotation.enablewebmvc; @enablewebmvc @controller public class hellocontroller { private static final string upload_directory ="/images"; @requestmapping("uploadform") public modelandview uploadform(){ return new modelandview("uploadform"); } @requestmapping(value="savefile",method=requestmethod.post) public modelandview saveimage( @requestparam commonsmultipartfile file, httpsession session) throws exception{ servletcontext context = session.getservletcontext(); string path = context.getrealpath(upload_directory); string filename = file.getoriginalfilename(); system.out.println(path+" "+filename); byte[] bytes = file.getbytes(); bufferedoutputstream stream =new bufferedoutputstream(new fileoutputstream( new file(path + file.separator + filename))); stream.write(bytes); stream.flush(); stream.close(); return new modelandview("uploadform","filesuccess","file saved!"); } }
spring-servlet.xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <mvc:default-servlet-handler/> <mvc:annotation-driven /> <context:component-scan base-package="com.upload"></context:component-scan> <bean class="org.springframework.web.servlet.view.internalresourceviewresolver"> <property name="viewclass" value="org.springframework.web.servlet.view.jstlview"/> <property name="prefix" value="/web-inf/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> <bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"/> </beans>
web.xml
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
index.jsp
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>insert title here</title> </head> <body> <a href="uploadform.jsp">upload image</a> </body> </html>
uploadform.jsp
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <!doctype html> <html> <head> <title>image file upload</title> </head> <body> <h1>file upload example </h1> <h3 style="color:red">${filesuccess}</h3> <form:form method="post" action="savefile" enctype="multipart/form-data"> <p><label for="image">choose image</label></p> <p><input name="file" id="filetoupload" type="file" /></p> <p><input type="submit" value="upload"></p> </form:form> </body> </html>
Comments
Post a Comment