Javaweb Upload and download
Upload:
Upload method:
Jspsmartupload: File upload and download components applied on JSP.
FileUpload: Using the upload function in the Jaava environment
Servlet3.0: Provides file upload function
STRUTS2: Provides the ability to upload files
The main story here is fileupload upload and download
Upload three elements:
1. The way to submit must be post (because the content size of the Get commit is limited)
2. The form needs to have <input type= "file" > element, need to have name attribute and value.
3. Form enctype= "Multipart/form-data".
Test page:
<form action= "Submit Address" method= "POST" enctype= "Multipart/form-data" > <input type= "text" name= "info"/></br > <input type= "file" name= "filename"/> <input type= "Submit" value= "Upload"/></form>
Use a servlet to receive uploaded file entries
1. Guide Package
Commons-fileupload-1.2.1.jar
Commons-io-1.4.jar
2. Write a servlet to receive
Package Com.baidu.upload;import Java.io.file;import Java.io.ioexception;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.disk.diskfileitemfactory;import Org.apache.commons.fileupload.servlet.servletfileupload;public class Fileuploadservlet extends HttpServlet { protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { try {//Get disk factory diskfileitemfactory dfif=new diskfileitemfactory ();//Get Core class Servletfileupload Sfu=new Servletfileupload ( DFIF);//encode the core class object to solve the garbled sfu.setheaderencoding ("UTF-8") in the name of the file;//Get the Commit parameter collection (each table item Fileitem) list<fileitem> List = Sfu.parserequest (Request), for (Fileitem fileitem:list) {//To determine if it is a normal item if (Fileitem.isformfield ()) {//Common item string name = Fileitem.getname (); String value = Fileitem.getstrinG ("UTF-8"); System.out.println (name+ "" +value); else {///File entry//Get file name string fieldName = Fileitem.getname ();//Set server storage path string dir= "C:\\users\\administrator\\desktop\ \develop ";//Use the file entry stream to stream the docking fileitem.write (new file (Dir,fieldname)); Fileitem.delete ();}} catch (Exception e) {e.printstacktrace ();}} protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}
Download the file
How to download: Automatic download and manual download
Automatic Download: The use of hyperlinks to achieve file download
In <a href= "path to File" > Hyperlinks </a>
Note: hyperlinks are the way that if the browser does not recognize files in this format, prompt to download, if supporting the format of the file, open directly.
Manual download: set two-class
Context-type: File type MIME.
Context-disposition: The browser supports the format file download, prompting the download.
output stream: sets the input stream that represents the file ( the output stream is a fixed response.getoutputstream ())
Package Com.baidu.controller;import Java.io.fileinputstream;import Java.io.ioexception;import java.io.InputStream; Import Java.net.urlencoder;import Javax.servlet.servletexception;import Javax.servlet.servletoutputstream;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.baidu.utils.downloadutils;public class FileDownloadServlet Extends HttpServlet {protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//Receive parameter string FilePath = Request.getparameter ("path");
Re-encode the received data string fileName = new String (Request.getparameter ("name"). GetBytes ("Iso-8859-1"), "Utf-8"); String file=filepath+ "\ \" +filename;//sets two-class Content-type content-disposition file output stream String type = Getservletcontext (). GetMimeType (file);
Sets the type of the response header Response.setcontenttype (type); Response.setheader ("Content-disposition", "Attachment;filename=" +file) ;//Gets the type of browser//string header = Request.getheader ("user-agent");//if ("Firefox". Equalsignorecase (header)) {//file = Downloadutils.base64encodefilename (file),//} else {//file = Urlencoder.encode (file, "UTF-8");/}
Gets the input stream object takes the file to be downloaded as the input stream object parameter InputStream ips=new fileinputstream (file);
Gets the response output stream object response for page Servletoutputstream outputstream = Response.getoutputstream (); int len=0;byte[] Bt=new byte[1024*1024 *3];while ((Len=ips.read (BT))!=-1) {outputstream.write (BT, 0, Len);} Ips.close (); Outputstream.close ();} protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}
Uploading using Struts2 files
The private file file;//the Name property of the file that corresponds to the foreground, and the private string filecontenttype;//corresponds to file+contexttypeprivate string filefilename;/ /corresponds to File+filename;public file GetFile () {return file;} public void Setfile (file file) {this.file = file;} Public String Getfilecontenttype () {return filecontenttype;} public void Setfilecontenttype (String filecontenttype) {this.filecontenttype = Filecontenttype;} Public String Getfilefilename () {return filefilename;} public void Setfilefilename (String filefilename) {this.filefilename = Filefilename;} Public String Execute () {try {fileutils.copyfile (file, new file ("E:/server", Filefilename));} catch (IOException E) { E.printstacktrace ();} return SUCCESS;}
Configuration file
<action name= "Upload" class= "com.baidu.web.action.UploadAction" method= "execute" ><result name= "Success" >/success.jsp</result><!--Configure upload file listener--><interceptor-ref name= "fileUpload" ><!--Configure the size of the uploaded file- -><param name= "MaximumSize" ></param><!--upload file extension--><param name= "Allowedextensions" >bmp ,txt,xml,doc,java</param> <!--set the allowed suffix name of the upload file, multiple-->< with commas separated by!--configuration of the uploaded file--><param name= " Allowedtypesset ">application/java</param></interceptor-ref><interceptor-ref name=" Defaultstack "/></action>
Day24 (javaweb upload and download)