Jsp+servlet implementation of multiple file upload __js

Source: Internet
Author: User
JSP Code <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <%@ taglib "uri=" http:// Java.sun.com/jsp/jstl/core "prefix=" C "%> <%@ taglib uri=" http://java.sun.com/jsp/jstl/functions "prefix=" FN "% > <%@ taglib uri= "http://java.sun.com/jsp/jstl/fmt" prefix= "FMT"%> <fmt:setbundle basename= "Csdn"/> & lt;! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  





Java code, Web.xml is registered in the/uplodes.action package servlet;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;

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.UploadContext;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;

Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
	public class Uplodesservlet extends HttpServlet {private int maxfilesize = 1024 * 1024 * 10;

	Defines the path to the file private String path = "d:\\";

	Private String FileType = "";  public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//

		Resolve files in Chinese garbled request.setcharacterencoding ("UTF-8"); CreateDisk File factory Processing object Diskfileitemfactory diskfileitemfactory = new Diskfileitemfactory ();
		Create server File Upload Processing object servletfileupload servletfileupload = new Servletfileupload (diskfileitemfactory);
			* *//Set file maximum upload size Servletfileupload.setfilesizemax (maxfilesize);//10M/try {//Resolve fields and requests in request requests

			list<fileitem> Fileitems = servletfileupload.parserequest (request); Iterate through the parsed list for (Fileitem fileitem:fileitems) {//judge whether the parsed is a plain text field if (Fileitem.isformfield ()) {/
					/Get field name String Filedname = Fileitem.getname ();

					Gets the value of the field name String Filedvalue = fileitem.getstring ("UTF-8");
				Output text box content System.out.println (filedname + "==================" + filedvalue); else {//Determine if the resource file size is legitimate if (fileitem.getsize () <= maxfilesize) {//Get field name String fileName = File

						Item.getname ();
						Troubleshoot file path problems with different browsers/IE or the other is this path C:\Users\chenhongjun\Desktop\tag_1388040641790.txt//Firefox only filenameD:\ Video \day52\ Videos 12.avi if (!filename.equals ("")) {//Get upload file type System.out.println (fileitem.getconte
							Nttype ()); Determines whether the specified type file if (Fileitem.getcontenttype (). Equals ("Image/jpeg")) {//whether to include "\" int index = File
								Name.lastindexof ("\");
								if (index!=-1) {fileName = filename.substring (index + 1);

								//Specifies the path to the file store filename = new file (path, newfilename);

								Create output stream FileOutputStream fos = new FileOutputStream (file);

								Gets the input stream InputStream is = Fileitem.getinputstream () that is parsed in the request;
								Create buffer area byte[] buffer = new byte[1024];
								int len = 0;
								while (len = (is.read (buffer))!=-1) {//write out the contents of the buffer fos.write (buffer, 0, Len);
								} fos.flush ();
								Fos.close ();

								Is.close ();

								Delete temporary files fileitem.delete ();
							SYSTEM.OUT.PRINTLN ("Success");
		} else {						System.out.println ("Please upload the specified file type");
						} else {System.out.println ("Please select File to upload");
					} else {System.out.println ("file too large, exceeding" + maxfilesize);
		catch (Fileuploadexception e) {//TODO auto-generated catch block E.printstacktrace (); } public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcepti
	On {this.doget (request, response); The handling of//filename is public string newfilename (string oldfilename) {//Get Last "."
		The position that appears is subscript int index = Oldfilename.lastindexof ("."); Intercept the last "."
		Previous content String Frontstr = oldfilename.substring (0, index); Get the last "."
		After the content String behindstr = oldfilename.substring (index);

		Re-stitching file name String NewFileName = frontstr + system.currenttimemillis () + "_" + behindstr;
	return newfilename;
 }

}



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.