Java Basics-File upload and download

Source: Internet
Author: User

/index.jsp

<%@ 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">"Content-type"Content="text/html; Charset=utf-8"><title>insert title Here</title><script type="Text/javascript"Src="/web-ajax/js/jquery.min.js"></script>"${pagecontext.request.contextpath}/fileservlet"Enctype="Multipart/form-data"Method="Post">User:<input type="text"Name="username"/><br> <input type="file"Name="Photo"/><br> <input type="file"Name="photo1"/><br> <input type="file"Name="Photo2"/><br> <input type="Submit"Value="Takeoff"/> </form></body>
View Code

/downlode.jsp

<%@ 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">"Content-type"Content="text/html; Charset=utf-8"><title>insert title here</title>"${pagecontext.request.contextpath}/downservlet"> Download Bar </a></body>
View Code

/class Fileservlet

Package Com.neuedu.servlet;import Java.io.file;import Java.io.ioexception;import java.util.list;import Java.util.uuid;import Javax.servlet.servletcontext;import Javax.servlet.servletexception;import Javax.servlet.annotation.webservlet;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.fileuploadbase.filesizelimitexceededexception;import Org.apache.commons.fileupload.fileuploadbase.sizelimitexceededexception;import Org.apache.commons.fileupload.disk.diskfileitemfactory;import Org.apache.commons.fileupload.servlet.ServletFileUpload, @WebServlet ("/fileservlet") public class Fileservlet          Extends HttpServlet {private static final long serialversionuid = 1L; protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {} protected void DoPost (HttpServletRequest request, HttpServletresponse response) throws Servletexception, IOException {//factory class, used to build a parser instance. Diskfileitemfactory factory=new diskfileitemfactory ();//Create an object, complete the upload servletfileupload fileupload=new Servletfileupload (Factory); Fileupload.setsizemax (1024*1500); ServletContext ServletContext = Request.getservletcontext (); String Realpath = Servletcontext.getrealpath ("/upload"); File File=new file (Realpath), if (!file.exists ()) {File.mkdir ();} try {list<fileitem>filelist=fileupload.parserequest (request); for (Fileitem fileitem:filelist) {if ( Fileitem.isformfield ()) {String name = Fileitem.getfieldname (); String value = fileitem.getstring ("UTF8"); System.out.println (name+ ":" +value);} else {Long size = fileitem.getsize (); if (size ==0) {continue;} String ContentType = Fileitem.getcontenttype (); String name = Fileitem.getname (), if (name.contains ("\ \")) {name = Name.substring (name.lastindexof ("\ \") +1);} uuid_name.jpgstring prefix = Uuid.randomuuid (). toString ();p Refix = Prefix.replace ("-", ""); String FileName = prefix+ "_"+name;//string fieldName = Fileitem.getfieldname (); System.out.println (size+ ":" +contenttype+ ":--------" +name+ ":" +filename ") Fileitem.write (New File (realpath+" \ \ "+ FileName));}}} catch (Filesizelimitexceededexception e) {System.out.println ("file size can not exceed 500K");}  catch (Sizelimitexceededexception e) {System.out.println ("total size can not exceed 1500k");} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

/class Downservlet

Package Com.neuedu.servlet;import Java.io.file;import Java.io.fileinputstream;import java.io.ioexception;import Java.io.inputstream;import Java.io.printwriter;import Javax.servlet.servletcontext;import Javax.servlet.servletexception;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 Org.apache.commons.io.IOUtils, @WebServlet ("/downservlet") public class Downservlet extends HttpServlet {private    Static final Long serialversionuid = 1L; protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { ServletContext ServletContext = Request.getservletcontext (); String filename= "Wind Blows Marlon mai. mp3"; String Path = Servletcontext.getrealpath ("/wind-blown Marlon mai. mp3"); File File=new file (path); String type = servletcontext.getmimetype (path); InputStream in=new fileinputstream (file); Response.setcontenttyThe PE (type); fileName = new String (filename.getbytes ("GBK"), "iso8859-1"), Response.setheader ("Content-disposition", " Attachment Filename= "+filename); Servletoutputstream Out=response.getoutputstream (); Ioutils.copy (in, out); In.close ();} protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}

  

Java Basics-File upload and download

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.