Use the fileupload component to upload large files.
1. FileUploadServlet. java file for upload Processing
Import java. io. file; import java. io. IOException; import java. io. printWriter; import java. text. decimalFormat; import java. util. iterator; import java. util. list; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. commons. fileupload. fileItem; import org. apache. commons. fileupload. fileUploadException; import org. apache. commons. fileupload. fileUploadBase. sizeLimitExceededException; import org. apache. commons. fileupload. disk. diskFileItemFactory; import org. apache. commons. fileupload. servlet. servletFileUpload; public class FileUploadServlet extends HttpServlet {private static final long serialVersionUID = 1L; public FileUploadServlet () {super ();} protected void doGet (HttpServletRequest request, response) throws ServletException, IOException {this. doPost (request, response);} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {final long MAX_SIZE = 2048*1024*1024; // set the maximum size of the uploaded file to 2 GB. You can change it to a larger value. // list of file formats that can be uploaded. final String [] allowedExt = new String [] {"exe ", "jpg", "DT"}; response. setContentType ("text/html"); // set the character encoding to a UTF-8, unified encoding, handling garbled problems response. setCharacterEncoding ("UTF-8"); // instantiate a hard disk file factory to configure the Upload Component ServletFileUpload DiskFileItemFactory dfif = new DiskFileItemFactory (); // use the above factory to instantiate the Upload Component ServletFileUpload sfu = new ServletFileUpload (dfif); // set the maximum upload size sfu. setSizeMax (MAX_SIZE); PrintWriter out = response. getWriter (); // obtain the List of all upload domains from the request List fileList = null; try {fileList = sfu. parseRequest (request);} catch (FileUploadException e) {// handle abnormal file size if (e instanceof SizeLimitExceededException) {out. println ("the file size exceeds the specified size:" + MAX_SIZE + "bytes <p/>"); out. println ("<a href = \" FileUpload.html \ "target = \" _ top \ "> return </a>"); return;} e. printStackTrace ();} // No file to upload if (fileList = null | fileList. size () = 0) {out. println ("Select Upload File <p/>"); out. println ("<a href = \" FileUpload.html \ "target = \" _ top \ "> return </a>"); return ;} // The file size is DecimalFormat digit = new DecimalFormat ("0.00"); // obtain all uploaded files Iterator fileItr = fileList. iterator (); // process all objects cyclically while (fileItr. hasNext () {FileItem fileItem = null; String path = null; double size = 0; // obtain the current file fileItem = (FileItem) fileItr. next (); // ignore the simple form field instead of uploading the file field of the domain (such as <input type = "text"/>) if (fileItem = null | fileItem. isFormField () {continue;} // get the file size, K is the unit, and keep two decimal places size = (double) fileItem. getSize ()/1024; if ("". equals (path) | size = 0) {out. println ("
2.FileUpload.html file to upload the page.
<Html>
3. Configure the servlet and filter in the web. xml file. The filter resolves garbled characters when the uploaded file name is Chinese.
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet><display-name>FileUploadServlet</display-name><servlet-name>FileUploadServlet</servlet-name><servlet-class>FileUploadServlet</servlet-class></servlet><servlet-mapping><servlet-name>FileUploadServlet</servlet-name><url-pattern>/FileUploadServlet</url-pattern></servlet-mapping><filter><filter-name>setCharacterEncoding </filter-name><filter-class>com.xulu.EncodingChange</filter-class><init-param><param-name>ignore </param-name><param-value>true </param-value></init-param><init-param><param-name>encoding </param-name><param-value>UTF-8 </param-value></init-param></filter><filter-mapping><filter-name>setCharacterEncoding </filter-name><url-pattern>/* </url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><login-config><auth-method>BASIC</auth-method></login-config></web-app>
4. The filter file EncodingChange. java and setCharacterEncoding. java are as follows, and put the. class files compiled by them into the WEB-INF \ classes \ com \ xulu folder under the root directory.
Package com. xulu; import java. io. IOException; import javax. servlet. *; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class EncodingChange implements Filter {protected String encoding = null; protected FilterConfig filterconfig = null; protected boolean ignore = true; public void destroy () {this. encoding = null; this. filterconfig = null;} public void DoFilter (ServletRequest requests, responses, FilterChain chain) throws IOException, ServletException {// TODO Auto-generated method stub HttpServletRequest request = (HttpServletRequest) requests; response = (response) responses; if (ignore | request. getCharacterEncoding () = null) {String encoding = selectEncoding (request); if (encoding! = Null) {request. setCharacterEncoding (encoding); request. getSession (). getAttribute ("Login_Student"); request. getSession (). getAttribute ("Login_Teacher"); request. getSession (). getAttribute ("Login_Admin") ;}} chain. doFilter (request, response);} public void init (FilterConfig filterconfig) throws ServletException {// TODO Auto-generated method stub this. filterconfig = filterconfig; this. encoding = f Ilterconfig. getInitParameter ("encoding"); String value = filterconfig. getInitParameter ("ignore"); if (value = null) {this. ignore = true;} else if (value. equalsIgnoreCase ("true") {this. ignore = true;} else if (value. equalsIgnoreCase ("yes") {this. ignore = true;} else {this. ignore = false ;}} public String selectEncoding (ServletRequest request) {return this. encoding ;}and package com. xulu; I Mport java. io. IOException; import javax. servlet. *; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class setCharacterEncoding implements Filter {protected String encoding = null; protected FilterConfig filterconfig = null; protected boolean ignore = true; public void destroy () {this. encoding = null; this. filterconfig = null;} public void doFilter (ServletRequest requ Ests, ServletResponse responses, FilterChain chain) throws IOException, ServletException {// TODO Auto-generated method response request = (HttpServletRequest) requests; HttpServletResponse response = (HttpServletResponse) responses; if (ignore | request. getCharacterEncoding () = null) {String encoding = selectEncoding (request); if (encoding! = Null) {request. setCharacterEncoding (encoding); request. getSession (). getAttribute ("Login_Student"); request. getSession (). getAttribute ("Login_Teacher"); request. getSession (). getAttribute ("Login_Admin") ;}} chain. doFilter (request, response);} public void init (FilterConfig filterconfig) throws ServletException {// TODO Auto-generated method stubthis. filterconfig = filterconfig; this. encoding = filterconfig. getInitParameter ("encoding"); String value = filterconfig. getInitParameter ("ignore"); if (value = null) {this. ignore = true;} else if (value. equalsIgnoreCase ("true") {this. ignore = true;} else if (value. equalsIgnoreCase ("yes") {this. ignore = true;} else {this. ignore = false ;}} public String selectEncoding (ServletRequest request) {return this. encoding ;}}
5. You can upload pages in the browser. You can access http: // localhost: 8080/**/FileUpload.html.Specifically, ** the web Folders named after you, such as DoUpload, are all in this directory, and the DoUpload folder is placed in the webapp folder of Tomcat.
Add: Three. jar files need to be introduced prior to all of the above steps, which can be downloaded to the commons-fileupload-1.2.1.jar, commons-io-1.4.jar and servlet-api.jar Web
The above is all the content of the large file upload simple instance implemented by using the fileupload component provided by Alibaba Cloud. I hope it will help you and provide more support to the customer's home ~