Use of form upload field (type = "file") ---- Upload file, form ----
1. Upload of a single file
1. html/jsp 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">
2. Filters solve Chinese garbled characters
package com.ser;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Filter01 implements Filter{ @Override public void destroy() { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { HttpServletRequest request= (HttpServletRequest)arg0; HttpServletResponse response=(HttpServletResponse)arg1; request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html charset=utf-8"); arg2.doFilter(request, response); } @Override public void init(FilterConfig arg0) throws ServletException { }}
Filter01.java
3. Process html/jsp
Package com. ser; import java. io. file; import java. io. IOException; import javax. servlet. servletConfig; import javax. servlet. servletException; import javax. servlet. annotation. multipartConfig; import javax. servlet. annotation. webServlet; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import javax. servlet. http. part;/*** Servlet Implementation class FileSer */@ WebServlet ("/FileSer") @ MultipartConfig (location = "e: // uploadFile", fileSizeThreshold = 1024*1024*1000, maxFileSize = 1024*1024*1000, maxRequestSize = 1024*1024*1024) public class FileSer extends HttpServlet {private static final long serialVersionUID = 1L; private static final String location = "e: // uploadFile ";/*** @ see HttpServlet # HttpServlet () */public FileSer (){}@ Override public void init (ServletConfig config) throws ServletException {File file = new File ("e: // uploadFile"); if (! File. exists ()&&! File. isDirectory () {file. mkdir () ;}/ *** @ see HttpServlet # doGet (HttpServletRequest request, HttpServletResponse response) */protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}/*** @ see HttpServlet # doPost (HttpServletRequest request, HttpServletResponse response) */protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Part part = request. getPart ("fileN"); String fileType = part. getContentType (); String fileHeader = part. getHeader ("content-disposition"); // String fileName = part. getName (); long size = part. getSize (); System. out. println (part); System. out. println (fileType); System. out. println (fileHeader); // System. out. println (fileName); System. out. println (size); String filename = fileHeader. substring (fileHeader. indexOf ("filename =") + 10, fileHeader. lastIndexOf ("\" "); part. write (filename); response. getWriter (). print (filename + "uploaded successfully ");}}
FileSer. java