Clumsy birds have Java (i)--using the STRUTS2 framework for file upload and download

Source: Internet
Author: User

Whether it is. NET or Java, we most often contact is the file upload and download function, in Java to achieve these two commonly used features will have a lot of solutions, but STRUTS2 framework can give us a relatively simple way, let's look at it together:

File Upload:

First look at the form that implements the upload function, index.jsp:

<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" ><%@ page language= "java" contenttype= "text/html; charset=gb18030 "    pageencoding=" GB18030 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >We need a JSP to display information about the uploaded file when we upload it successfully, so we create a new JSP file with the following code:

<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" ><%@ page language= "java" contenttype= "text/html; charset=gb18030 "    pageencoding=" GB18030 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

In the framework of STRUTS2, with JSP, we also use the action to accept and process the data, it is very simple, but we have to abide by the corresponding rules:

<span style= "FONT-SIZE:18PX;" ><span style= "font-family:fangsong_gb2312;" >package Com.bjpowernode.struts2;import Java.io.bufferedinputstream;import Java.io.BufferedOutputStream;import Java.io.file;import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.InputStream;import Java.io.outputstream;import com.opensymphony.xwork2.action;/** * File uploaded Action * @author Chao * */public class uploadtestaction {private string title;//can get the name of the uploaded file//rule: Enter the name of the domain + fixed string filenameprivate string myfilefilename;//get the file data Rule: The name of the file input domain private file myfile;//Gets the content type//rule: Enter the name of the domain + fixed string contexttypeprivate string myfilecontexttype;public String Getmyfilefilename () {return myfilefilename;} public void Setmyfilefilename (String myfilefilename) {this.myfilefilename = Myfilefilename;} Public String GetTitle () {return title;} public void Settitle (String title) {this.title = title;} Public File Getmyfile () {return myFile;} public void Setmyfile (File myFile) {this.myfile = MyFile;} Public String GetmyfileContextType () {return myfilecontexttype;} public void Setmyfilecontexttype (String myfilecontexttype) {this.myfilecontexttype = Myfilecontexttype;} Public String Execute () throws exception{//initialize the input stream and output stream InputStream is =null;outputstream OS = null;try{//read in file is = new Buffer Edinputstream (New FileInputStream (MyFile));//read out the file to the established location OS = new Bufferedoutputstream (New FileOutputStream ("c:\\" + MyF Ilefilename));//Instantiate a byte array byte[] buffer = new Byte[1024];int len = 0;while ((len=is.read (buffer)) > 0) {os.write ( Buffer,0,len);}} Finally{if (is! = null) {Is.close ();} if (OS! = null) {Os.close ();}} return action.success;
</pre><pre name= "code" class= "Java" >

when both the JSP and the action are available, we also link them through the configuration file, that is, through the following code:

<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" ></span>
</pre><pre>
<! DOCTYPE struts public    "-//apache software foundation//dtd struts Configuration 2.0//en"    "/http Struts.apache.org/dtds/struts-2.0.dtd "><struts>    <!--when the Struts.xml configuration file is modified, it will be loaded immediately; it is best not to configure in the production environment-- >    <constant name= "Struts.configuration.xml.reload" value= "true"/>    <!--set this configuration to show a more friendly error-hinting interface    --<constant name= "Struts2.devmode" value= "true"/>    <!--need to inherit Struts-default package, This has the most basic function  --><package name= "upload-package" extends= "Struts-default" ><action name= "upload" class= "Com.bjpowernode.struts2.UploadTestAction" ><result>/success.jsp</result></action> </package></struts>
    

In this way, we can feed the data that the action takes over to the index.jsp, and when we've entered the information, click the "Upload" button, and the data will be transferred to the action, and if the upload succeeds, it will go to the seccess.jsp in the configuration file.

Therefore, it is not difficult to use the framework of STRUTS2 to implement the upload function of the file, mainly using IO stream to realize. See here, have you ever thought that if you want to implement multi-file upload, what? Here's a look:

in Struts2, multi-file uploads and single-file uploads are the same principle, just to use multiple <s:file/> tags to bind an array of action. The action code that is bound to the array should be:

     Private file[] uploads;     Private string[] Uploadfilenames;     Private string[] uploadcontenttypes;     Public file[] Getupload () {return this. uploads;}      public void Setupload (file[] upload) {this. uploads = upload;}       Public string[] Getuploadfilename () {return this. uploadfilenames;}      public void Setuploadfilename (string[] uploadfilename) {this. uploadfilenames = Uploadfilename;}      Type of upload file public     string[] Getuploadcontenttype () {return this. uploadcontenttypes;}      public void Setuploadcontenttype (string[] uploadcontenttype) {this. uploadcontenttypes = Uploadcontenttype;}
Note:

1, struts2 default to use the Apache Commons-fileupload

2, STRUTS2 support three types of upload components

3, need to introduce commons-fileupload related jar package
* Commons-io-1.3.2.jar
* Commnos-fileupload-1.2.1.jar
4, the form needs to use the Post submission method, the encoding type needs the Multipart/form-data
5. Struts2 Action
Get file name---->>> rule: Enter the name of the field + fixed string filename
Get file Data---->>> rules: Name of the input field
Get content Type---->>> Rules: Enter the name of the field + fixed string contexttype

6. Get input stream, write file with output stream


Using the STRUTS2 framework to implement the function of file upload first here, hope can help everyone! If you have any questions or better suggestions, you are welcome to correct me or contact me!





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Clumsy birds have Java (i)--using the STRUTS2 framework for 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.