Required Package: Spring 21 Pack, commons-fileupload/io/logging three pack, standard Tag Library 2 pack
Exception handling: If reported Beancreationexception:lookup method resolution failed may be missing the necessary package;
Spring-servlet.xml configuration:
<?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:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /mvc Http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsdhttp://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context-4.3.xsd "><context:component-scan base-package=" Com.hanqi.controller "></context:component-scan><!--Note scanner--><bean class=" Org.springframework.web.servlet.view.InternalResourceViewResolver "><!--View resolver--><property name=" Prefix "value="/web-inf/page/"></property><property name=" suffix "value=". JSP ></property> </bean><!--Configure spring-brought textItem Upload class--><bean id= "Multipartresolver" class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver "><!--the total size of files that can be uploaded, in units (b), 1kb=1024b-- ><property name= "maxuploadsize" value= "1000000000" ></property><!--the size of individual files that can be uploaded, in units (b)-->< Property Name= "Maxuploadsizeperfile" value= "100000000" ></property><!--default Character set--><property name= " Defaultencoding "value=" UTF-8 "></property></bean></beans>
File Upload page:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
File Download page:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><%@ taglib uri=" Http://java.sun.com/jsp/jstl/core "prefix=" C "%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
File Controller class:
Package Com.hanqi.controller;import Java.io.file;import Java.io.ioexception;import java.util.date;import Javax.servlet.http.httpservletrequest;import Org.apache.commons.io.fileutils;import Org.springframework.http.httpheaders;import Org.springframework.http.httpstatus;import Org.springframework.http.mediatype;import Org.springframework.http.responseentity;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.multipart.MultipartFile, @Controller @requestmapping ("/file") public class Filecontroller {@ Requestmapping ("/upload") public String upload (multipartfile file,httpservletrequest request) {System.out.println ( File.getname ());//The value of the Name property in the input control System.out.println (File.getsize ()); File Size System.out.println (File.getoriginalfilename ());//file name//create a new Files folder under WebContent and get its physical path string path = Request.getservletcontext (). Getrealpath ("/files");//Create a new empty file, File.separator equivalent to "/", plus a timestamp to distinguish the file of the same name Orgfile =New file (path + file.separator + new Date (). GetTime () + "-" + file.getoriginalfilename ()); try {//Put the received file in an empty file// The file is actually stored in the D:\eclipse workspace\.metadata\.plugins\org.eclipse.wst.server.core//\tmp0\wtpwebapps\spring-file\ Filesfile.transferto (orgfile);} catch (IllegalStateException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Return "Success";} @RequestMapping ("/download") public responseentity<byte[]> testdownload (HttpServletRequest request,string FileName) throws Exception {//String orgfilename = new String (Filename.getbytes ("iso-8859-1"), "Utf-8"); String path = Request.getservletcontext (). Getrealpath ("/files");//File Orgfile = new file (path + "/" + orgfilename); File Orgfile = new file (path + "/" + filename);//Set Request header information Httpheaders hh = new Httpheaders ();//Tell the front desk to (attachement, download) Way to open the file Hh.setcontentdispositionformdata ("Attachment", New String (Filename.getbytes ("Utf-8"), "iso-8859-1"));// The file is transferred as a binary stream, which is the most common way to download Hh.setcontenttype (mediatype.application_octet_stream); return new responseentity<byte[]> (Fileutils.readfiletobytearray (orgfile), HH, httpstatus.created);}}
Page Jump Control class:
Package Com.hanqi.controller;import Java.io.file;import Java.util.arraylist;import java.util.list;import Javax.servlet.http.httpservletrequest;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import Org.springframework.web.bind.annotation.pathvariable;import Org.springframework.web.bind.annotation.RequestMapping, @Controllerpublic class Formcontroller {@RequestMapping ("/ {path} ') public string topage (@PathVariable ("path") string Page,model model,httpservletrequest request) {if ("download" . Equals (page)) {String path = Request.getservletcontext (). Getrealpath ("/files"); File Orgfiles = new file (path); file[] files = orgfiles.listfiles (); List fileList = new ArrayList (); for (File f:files) {Filelist.add (F.getname ());} Model.addattribute ("FileList", fileList);} return page;}}
JAVA Framework-springmvc-file upload and download