Java Doget, Dopost method and file upload instance code _java

Source: Internet
Author: User

Java Doget, Dopost method and file upload

Index.html

<! DOCTYPE html>  

Testservlet.java

Import Org.apache.commons.fileupload.FileItem;
Import org.apache.commons.fileupload.FileUploadException;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;

Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletInputStream;
Import Javax.servlet.ServletOutputStream;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import java.io.IOException;
Import Java.net.URLDecoder;
Import Java.net.URLEncoder;
Import java.util.Enumeration;

Import java.util.List; /** * Project:day15_web * created:lulu * date:2016/8/16/@WebServlet (name = "Testservlet") public class TESTSERVL
  ET extends HttpServlet {private File root_upload;
    Public Testservlet () {root_upload = new File ("C:/temp"); if (!root_upload.exists ()) {
      Root_upload.mkdirs (); }//Two links doPost doget protected void DoPost (HttpServletRequest request, httpservletresponse response) throws S
    Ervletexception, IOException {response.setcharacterencoding ("UTF-8");
    Request.setcharacterencoding ("UTF-8");
    Servletoutputstream OS = Response.getoutputstream ();

    StringBuilder builder = new StringBuilder ();
    Builder.append (" 

Xml

<?xml version= "1.0" encoding= "UTF-8"?> <web-app xmlns=
"Http://xmlns.jcp.org/xml/ns/javaee"
     xmlns : xsi= "http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee Http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "
     version=" 3.1 ">
  <servlet>
    < Servlet-name>testservlet</servlet-name>
    <servlet-class>org.lulu.learn.testservlet</ servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>testservlet</ servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
</ Web-app>

Thank you for reading, I hope to help you, thank you for your support for this site!

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.