Use of Form form Upload file (type = "file"), Form form

Source: Internet
Author: User

Use of Form form Upload file (type = "file"), Form 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  {  }}

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 ");}}

Summary

The above section describes how to use Form upload fields (type = "file") (upload files). I hope it will be helpful to you, if you have any questions, please leave a message and the editor will reply to you in time. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.