Upload and download files based on struts2;

Source: Internet
Author: User

Reception from the form;

<form action= "Dd.action" method= "post" enctype= "Multipart/form-data" >

</form>

The form submits data to the server in the context of the binary stream when the data is submitted;

Struts2 in the original upload parser inheritance to do a further encapsulation, a step closer to simplifying the file upload;

STRUTS2 uses the file upload framework for Jakarta and Common-fileupload by default, so if you need to Shiyong

Struts2 File Upload function, you need to import the relevant jar package in the Web application;

How to write the action in the background;

(1) The xxx attribute of type file encapsulates the file content corresponding to that domain; import java.io.File;

(2) The Xxxfilename attribute of type string encapsulates the file type corresponding to the file field of the case;

(3) The Xxxconttype property of type string encapsulates the file type of the file;

1. Open the downloaded struts frame;

Open this folder and copy the Lib jar under the empty project inside the Web-inf to the Web-inf file under your project.

Under , the operation to load the jar Budlidpath, add BuildPath is complete.

2. Also copy the Struts.xml under the empty project to the SRC directory under our project and delete the contents of the package.

and the contents of the following are also deleted.

The basic configuration is here;

Front page;

<%@ page contenttype= "Text/html;charset=utf-8" pageencoding= "UTF-8"%>

<form action= "dd.action" method= "post" enctype= "Multipart/form-data" >
<table id= "Imageupload" >
<tr height= "30px" >
<TD colspan= "2" >
"Upload Photos"
</td>
</tr>
<tr>
<TD align= "center" > select photos </td>
<td><input type= "" File "name=" Upload "size="/></td> "
</tr>
<tr>
<TD align= "center" > Photo name </td>
<td><input type= "text" name= "info" size= "/></td>"
</tr>
<tr height= ">"
<TD colspan= "2" align= "Center" >
<input type= "Submit" value= "Upload" class= "Btn_bg"/>
<input type= "reset" value= "reset" class= "BTN_BG"/>${result}
</td>
</tr>
</table>
</form>

Action for background processing;

In the project SRC, create a package that creates an action in the package

Package com.test.action;

Import Java.io.File;

Import Org.apache.commons.io.FileUtils;
Import Org.apache.struts2.ServletActionContext;

Import Com.opensymphony.xwork2.ActionSupport;

public class Fileaction extends Actionsupport {
Private File upload;
Private String Uploadconttype;
Private String Uploadfilename;
Public File Getupload () {
return upload;
}
public void Setupload (File upload) {
This.upload = upload;
}
Public String Getuploadconttype () {
return uploadconttype;
}
public void Setuploadconttype (String uploadconttype) {
This.uploadconttype = Uploadconttype;
}
Public String Getuploadfilename () {
return uploadfilename;
}
public void Setuploadfilename (String uploadfilename) {
This.uploadfilename = Uploadfilename;
}

Public String Execute () throws exception{
String path = Servletactioncontext.getservletcontext (). Getrealpath ("/userfiles/images");
File File = new file (path);
System.out.print (File.exists ());
if (!file.exists ()) {
File.mkdir ();
System.out.println ("Create folder");
}
Fileutils.copyfile (Upload, new File (File,uploadfilename));
System.out.print ("Upload acquisition or not");
return SUCCESS;
}
}

The last step left is to configure struts,

<action name= "dd" class= "com.test.action.Fileaction" >//This class path is the action path;
<result name= "Success" >/index.jsp</result>
<result name= "Input" >/error.jsp</result>
<interceptor-ref name= "FileUpload" >
<!--configuring interceptors to limit upload file types and sizes--
<param name= "Allowedtypes" >image/bmp,image/x-png,image/gif,image/jpeg</param>
<param name= "MaximumSize" >2M</param>
</interceptor-ref>
<interceptor-ref name= "Defaultstack" ></interceptor-ref>
</action>

Upload and download files based on struts2;

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.