Jsp+servlet implementation of File upload

Source: Internet
Author: User

File uploads need to use the Cos.jar package. File upload the Display page index.jsp file is as follows:

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <!

DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

The form must be submitted using post, and the phrase "Multipart/form-data" must be written enctype=. The corresponding Uploadservlet.java files are as follows:

Import Java.io.File;
Import java.io.IOException;

Import java.util.Enumeration;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;
Import Com.hh.excel.ExcelData;

Import Com.oreilly.servlet.MultipartRequest; public class Uploadservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Respo
	NSE) throws Servletexception, IOException {doPost (request, response); 

		public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
		Upload files//files uploaded, save in the C-Packing directory request.setcharacterencoding ("Utf-8");
		String savedirectory = new File ("c:/"). GetAbsolutePath ();
		String FilePath = "";
		Maximum 500m int maxpostsize = 1 * 500 * 1024 * 1024 per file; Response is encoded as "Utf-8", and the default file name Conflict resolution strategy is implemented to upload multipartrequest multi = new Multipartrequest (Request, Savedirectory, Maxpostsize, "Utf-8 ");
		Output feedback Information Enumeration files = Multi.getfilenames ();
			while (Files.hasmoreelements ()) {String name = (String) files.nextelement ();

			File f = multi.getfile (name);
				if (f!= null) {String fileName = multi.getfilesystemname (name);
				FilePath = savedirectory + "\" + fileName;
			System.out.println (FilePath);
 }
		}
	}

}

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.