Upload file using spring framework
how to upload file using spring.
Upload a file using spring framework is easy.
first of all we must import the required spring classes(that are given below).
To upload file using spring framework we must import 'MultipartHttpServletRequest' and
'MultipartFile' in to our project.to deal with multi part content in a servelet request we must include
MultipartHttpServletRequest interface. MultipartFile interface is used to store file that are comming with
Multipart HttpServlet Request. Here when the user click the 'Upload Doc' button the corresponding handler function
works in this case 'upLoadHandler()' and the function mapp the request(@RequestMapping(value = "/fileupload.do", method =
RequestMethod.POST)).Then corresponding code executed.after uploading the file server redirect the request to upload page
again.
package fileUpload.document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UploadController {
ModelAndView mav;
//location where to upload file
private static final String destinationDir = "/mnt/disk1/uploads/";
@RequestMapping(value = "/upload.do", method = RequestMethod.GET)
public String uploadPage()
{
return "document/upPage";
}
@RequestMapping(value = "/fileupload.do", method = RequestMethod.POST)
public String upLoadHandler(HttpServletRequest req,
HttpServletResponse res) throws Exception {
res.setContentType("text/plain");
if (!(req instanceof MultipartHttpServletRequest)) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Expected multipart request");
return null;
}
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
//here "fileUploaded" is the name of the html file upload tag in the web page
MultipartFile file = multipartRequest.getFile("fileUploaded");
File destination = new File(destinationDir + file.getOriginalFilename());
file.transferTo(destination);
return "redirect:upload.do";
}
}
public class UploadController {
ModelAndView mav;
//location where to upload file
private static final String destinationDir = "/mnt/disk1/uploads/";
@RequestMapping(value = "/upload.do", method = RequestMethod.GET)
public String uploadPage()
{
return "document/upPage";
}
@RequestMapping(value = "/fileupload.do", method = RequestMethod.POST)
public String upLoadHandler(HttpServletRequest req,
HttpServletResponse res) throws Exception {
res.setContentType("text/plain");
if (!(req instanceof MultipartHttpServletRequest)) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Expected multipart request");
return null;
}
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
//here "fileUploaded" is the name of the html file upload tag in the web page
MultipartFile file = multipartRequest.getFile("fileUploaded");
File destination = new File(destinationDir + file.getOriginalFilename());
file.transferTo(destination);
return "redirect:upload.do";
}
}
file uploading html
this is the html page for uploading file in to server.only required part of the html
is given.
here we can see that the method is POST and encryption type is
multipart/form-data'
<form method="post" action="/fileupload.do" enctype="multipart/form-data">
<input type="file" name="fileUploaded" />
<input type="submit" value="Upload Doc"/>
</form>
before writing the code we must include multi part resolver bean in our application-context.xml file
<!-- Configure the multipart resolver -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>
thank you so much, this is a easy way to upload file ^^
ReplyDelete