struts2 implement file upload download (download support chinese file name) code

Source: Internet
Author: User

struts2 Implement File Upload:


Action Code:

Package Com.action;import Java.io.file;import Java.io.fileinputstream;import java.io.fileoutputstream;import Java.io.inputstream;import Java.io.outputstream;import Org.apache.struts2.servletactioncontext;import Com.opensymphony.xwork2.actionsupport;public class Fileuploadaction extends baseaction{/** * */private static final Long serialversionuid = 1l;/** * files stored in temporary folder after uploading */private file <span style= "color: #ff0000;" >file;</span>/** * File name */private String <span style= "color: #ff0000;" >filefilename</span>;/** * file MIME type */private string<span style= "color: #ff0000;" > filecontenttype</span>;/** * Saved path */private string Savepath;public string Getsavepath () {return savepath;} public void Setsavepath (String savepath) {this.savepath = Savepath;} Public file GetFile () {return file;} public void Setfile (file file) {this.file = file;} Public String Getfilefilename () {return filefilename;} public void Setfilefilename (String filefilename) {this.filefilename = Filefilename;} Public String Getfilecontenttype () {return filecontenttype;} public void Setfilecontenttype (String filecontenttype) {this.filecontenttype = Filecontenttype;} Public String Execute () throws exception{string root = Servletactioncontext.getservletcontext (). Getrealpath (Savepath)        ;        InputStream is = new FileInputStream (file);                        OutputStream OS = new FileOutputStream (new File (Root, Filefilename));        byte[] buffer = new BYTE[500];                @SuppressWarnings ("unused") int length = 0;        while ( -1! = (length = is.read (buffer, 0, buffer.length))) {os.write (buffer);        } os.close ();                        Is.close (); return SUCCESS;}}


Struts2.xml configuration information:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts public        "-//apache software foundation//dtd struts Configuration 2.0//en"        "/http Struts.apache.org/dtds/struts-2.0.dtd ">          <struts><package name=" FileUpload "extends=" Struts-default "namespace="/"><action name=" fileupload_* "class=" com.action.FileUploadAction "method=" {1} " >           <param name= "Savepath" >/upload</param>//Note Build a upload folder in the current project          <result>/login.jsp </result> </action></package></struts>

JSP page for file upload:

<form action= "Fileupload.action" method= "post" enctype= "Multipart/form-data" >        file: <input type= "File" Name= "File"/><br>        <input type= "Submit" value= "Submit"/>    </form>

struts2 implement file download source code:support Chinese file nameNote: The size of the download file setting problem, the default is 2M. can be set in Struts.properties struts.multipart.maxsize=10485760 (10M size according to your own situation)Code for Action:

Package Com.action;import Java.io.ioexception;import Java.io.inputstream;import Org.apache.struts2.servletactioncontext;public class Filedownloadaction extends baseaction{/** *  */private Static final Long Serialversionuid = 1l;/** * File name */private string Filename;public string GetFileName () throws Ioexceptio n {<span style= "color: #ff0000;" >return  New String (Filename.getbytes (), "iso8859-1");//transcoding to enable it to support Chinese, otherwise cannot display the file name of the middle character </span>}public void Setfilename (String fileName) {this.filename = FileName;;} public InputStream getInputStream () throws exception{return Se Rvletactioncontext.getservletcontext (). getResourceAsStream (FileName);     }     Public String Execute () throws Exception{return SUCCESS;}}

Download the Struts2.xml configuration information:

<action name= "Download" class= "com.action.FileDownloadAction" ><result type= "stream" Name= "Success" > <param name= "contentdisposition" >attachment;filename= "${filename}" </param><param name= " ContentType ">application/octet-stream;charset=iso8859-1</param><param name=" InputName "><span style= "COLOR: #ff0000;" >InputStream</span></param>//inputstream is an action in <span style= "font-family:arial; font-size:14px; line-height:26px; " >getinputstream () method remove get, to be consistent </span><param name= "buffersize" >10485760</param>//file size (10M) </result></action>

JSP page:

<a href= "download.action?filename=upload/remember 11er.mp3" > click Download </a>


struts2 implement file upload download (download support chinese file name) code

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.