Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 File uploading example in Spring MVC:-

It is used to upload the external file under the server from the local machine, spring MVC provide Multipart Resolve class to upload the file

Create Dynamic Web Project from eclipse and create dispatcher servlet and web.xml file under the project.

Step1st:-

Create Web.xml file

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>jobportal</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

  <servlet>

    <servlet-name>job</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>job</servlet-name>

    <url-pattern>*.do</url-pattern>

  </servlet-mapping>

  

</web-app>


2)  Create Dispatcher Servlet

<?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: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"> 


    <context:component-scan base-package="bao"></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <property name="prefix" value="/uao/"></property>

    <property name="suffix" value=".jsp"></property>

    </bean>

   <bean id="multipartResolver"   

    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>  

    </beans>


3)  Create an images folder under web content

4)  Create Controller:-

package bao;

import java.io.*;

import javax.persistence.Entity;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
@Entity
public class FileuploadController {

@RequestMapping("/uploadview")
public ModelAndView uploadview()
{
return new ModelAndView();
}
@RequestMapping("/uploadlogic")
public ModelAndView uploadlogic(@RequestParam CommonsMultipartFile file,  
           HttpSession session) throws IOException
{
//ServletContext context = session.getServletContext();  
    //String path = context.getRealPath("images");  
String path= session.getServletContext().getRealPath("/")+"images";  
        String filename=file.getOriginalFilename();  
   // String filename = file.getOriginalFilename();  
  
    System.out.println(path+" "+filename);        
  
    byte[] bytes = file.getBytes();  
    BufferedOutputStream stream =new BufferedOutputStream(new FileOutputStream(  
         new File(filename)));  
    stream.write(bytes);  
    stream.flush();  
    stream.close();  
           
    return new ModelAndView("uploadform","filesuccess",filename);  
}
}


5)  Create a view of the design upload form.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>
<form action="uploadlogic.do" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<br>
<br>
<input type="submit" name="btnsubmit" value="Upload" />
</form>
</body>
</html>

6)  create a view file to show images



<%@ 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>
<img src="images/${filesuccess}" height="300" width="400"  />
</body>
</html>



Post a Comment

POST Answer of Questions and ASK to Doubt

Previous Post Next Post