Clumsy birds have Java (i)--using the STRUTS2 framework for file uploads

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 frequently used features there will be a lot of solutions, but the STRUTS2 framework can give us a simpler way, let's look at the following:

File Upload:

First look at the form that implements the upload feature. 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" >When we upload successful, we need a JSP to display the information about the uploaded file, so we create a new JSP file, such as 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 struts2 this framework, with JSP, we also use action to accept and process the data, but also very easy, just 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, by following these codes:

</pre><pre name= "code" class= "java" ><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 struts.xml configuration file changes. will be loaded immediately. In production environments it is best not to configure--    <constant name= "Struts.configuration.xml.reload" value= "true"/>    <!-- Set this configuration to show a friendlier error interface--    <constant name= "Struts2.devmode" value= "true"/>    <!-- Need to inherit the Struts-default package. This has the most important 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 are able to feed the data that the action takes over to index.jsp, and when we've entered the information, click on the "Upload" button. The data is then transferred to the action, assuming the upload is successful, it will be shifted to the seccess.jsp in the configuration file.

so. It is not difficult to use the framework of STRUTS2 to implement the upload function of the file, mainly using IO stream. See here, have you ever thought of assuming that you want to implement a multi-file upload, and what? See below:

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 by default using 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 adopt the Post submission method, the encoding type needs to use Multipart/form-data
5. Struts2 Action
Get filename---->>> 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


Use the STRUTS2 framework to implement the function of file upload first here. Hope to be able to help everyone! Suppose you have any questions or better suggestions. You are welcome to correct me or contact me!





Clumsy birds have Java (i)--using the STRUTS2 framework for file uploads

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.