The two issues that are summarized are that the struts2 is very strict when uploading and downloading the property name:
The first: when uploading
private file file;
Private String Filecontenttype;
Private String Filefilename;
The red part of the attribute must be free to play, but cannot be omitted.
Second: When downloading the property name can not be arbitrarily up.
Implement file Upload: You can find the web-inf\lib\struts2-core-2.3.16.3.jar under the struts-default.xml blocker tag name=fileupload entity class alt+shift+t go into the source code ( provided that the jar Package is associated well ), see
My upload Action:
Package cn.jbit.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 Uploadaction extends Actionsupport {
3 attributes are used in action to encapsulate file information
1:file type of XXX property: Consistent with the Name property of the file control in the form, used to encapsulate the contents of files corresponding to the document control
The Xxxfilename property of the 2:string type and the Xxxcontenttype property of the 3:string type, the invariant syntax for both properties is
Property name of file type + suffix filename| ContentType group Synthesis Final: Xxxfilename,xxxcontenttype
If you do not write in the fixed syntax, it may cause file name and file type can not be read, the final written file is a
The name is: null, and there is no file information in the type format. (although manually changed to. jpg will also show) private file file;
Private String Filecontenttype;
Private String Filefilename;
@Override
Public String Execute () throws Exception {
1024*1024=1m
Byte[] Buffer=new byte[1024*1024];
The file tag in the form
InputStream fis=new fileinputstream (file);
Create a folder with pictures in the WebRoot directory and get the real address
String Path=servletactioncontext.getservletcontext (). Getrealpath ("image");
path=path+ "\ \" +this.filefilename;
SYSTEM.OUT.PRINTLN (path);
eventually read the binary stream is written, the real address under the same name in the file
OutputStream os=new FileOutputStream (path);
OutputStream os=new FileOutputStream ("e:\\upload\\" +this.filefilename);
int len=0;
while ((Len=fis.read (buffer)) >0) {
Os.write (Buffer,0,len);
}
Fis.close ();
Os.close ();
return SUCCESS;
}
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;
}
}
When you're done uploading, remember that this is when youWebRootRight-click to open theImage→MyEclipse→Open in ExplorerI can't find the picture you uploaded,Because the real storage address of the picture is where the project is deployedWebAppsIn the case of:E:\Program Files (x86) \apache-tomcat-7.0.55\webapps\up\imageinstead of this path:E:\java2\upload2014years AMonth2Day\webroot\image.
Upload a file form:
<body>
<s:form action= "Upload" enctype= "Multipart/form-data" method= "POST" >
<s:file name= "File" label= "Select Files" ></s:file>
<s:submit value= "Upload" ></s:submit>
</s:form>
</body>
Page to jump after success:
The file you uploaded is: <s:property value= "Filefilename"/><br/>
File type: <s:property value= "Filecontenttype"/><br/>.
If you upload multiple files:
We're under the org.apache.struts2 under the Web-inf\lib\struts2-core-2.3.16.3.jar. Search max in Default.properties to see:
struts.multipart.maxsize=2097152
That is, struts2 the default upload size is 2M(upload multiple files total size), then we can set a Large point inside the Struts2.xml:
20M
<constant name= "struts.multipart.maxSize" value= "20971520"/>
My Upload form is:
<s:form action= "Upload" enctype= "Multipart/form-data" method= "POST" >
<s:file name= "Files" label= "Select File" ></s:file>
<s:file name= "Files" label= "Select File" ></s:file>
<s:file name= "Files" label= "Select File" ></s:file>
<s:file name= "Files" label= "Select File" ></s:file>
<s:file name= "Files" label= "Select File" ></s:file>
<s:file name= "Files" label= "Select File" ></s:file>
<s:submit value= "Upload" ></s:submit>
My upload action:
Package cn.jbit.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 Uploadaction extends Actionsupport {
Private static final long serialversionuid = 1L;
Private file[] files;
Private string[] Filescontenttype;
Private string[] Filesfilename;
@Override
Public String Execute () throws Exception {
Byte[] Buffer=new byte[1024*1024];
Files=getfiles ();
for (int i = 0; i < files.length; i++) {
The file tag in the form
InputStream fis=new FileInputStream (Files[i]);
Create a folder with pictures in the WebRoot directory and get the real address
String Path=servletactioncontext.getservletcontext (). Getrealpath ("image");
path=path+ "\ \" +this.filesfilename[i];
SYSTEM.OUT.PRINTLN (path);
eventually read the binary stream is written, the real address under the same name in the file
OutputStream os=new FileOutputStream (path);
int len=0;
while ((Len=fis.read (buffer)) >0) {
Os.write (Buffer,0,len);
}
Fis.close ();
Os.close ();
}
return SUCCESS;
}
Public file[] GetFiles () {
return files;
}
public void Setfiles (file[] files) {
This.files = files;
}
Public string[] Getfilescontenttype () {
return filescontenttype;
}
public void Setfilescontenttype (string[] filescontenttype) {
This.filescontenttype = Filescontenttype;
}
Public string[] Getfilesfilename () {
return filesfilename;
}
public void Setfilesfilename (string[] filesfilename) {
This.filesfilename = Filesfilename;
}
}
To implement a file download:
My Action:
Package cn.jbit.action;
Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.InputStream;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionSupport;
public class Filedownaction extends Actionsupport {
Struts2 the configuration of the property name is very strict, if not correct will be an error, I made a mistake here
the private inputstream inputstream; InputStream input was declared;
just give me an error saying can't find inputstream, so suggest //filename,contenttype,inputstream Attribute names do not have to be named by yourself.
For commonly used property names, for example,
Read the directory of downloaded files
Private String path;
filename of the download file
Private String FileName;
Types of download files
Private String ContentType;
Read the input stream of the downloaded file
Private InputStream InputStream;
@Override
Public String Execute () throws Exception {
return SUCCESS;
}
Public String GetFileName () {
return fileName;
}
public void Setfilename (String fileName) {
This.filename = FileName;
}
Public String getContentType () {
return contentType;
}
public void setContentType (String contentType) {
This.contenttype = ContentType;
}
Public InputStream getInputStream () throws FileNotFoundException {
String Paths=servletactioncontext.getservletcontext (). Getrealpath (path);
return new Bufferedinputstream (new FileInputStream (paths+ "\ \" +filename));
}
public void Setinputstream (InputStream inputstream) {
This.inputstream = InputStream;
}
Public String GetPath () {
return path;
}
public void SetPath (String path) {
This.path = path;
}
}
My struts.xml configuration :
<action name= "Download" class= "Cn.jbit.action.FileDownAction" >
<param name= "Path" >/image</param>
<result name= "Success" type= "Stream" >
<!--find a publisher Tomcat under →conf→web.xml There are various MIME types--
<!--MIME: Multi-purpose Internet Mail extension type, which is the type of file to be opened with an application that is set to an extension.
<param name= "ContentType" >image/jpeg</param>
<param name= "FileName" >input</param>
<param name= "Contentdisposition" >
<!--The contentdisposition parameter consists of two parts, the previous part identifies the form of the processing file, such as attachment indicates that the dialog box appears when downloading, prompting the user to save or open the file directly;
The latter part identifies the file name of the downloaded file, separated by a semicolon between the volume parts and
Attachment;filename= "${filename}"
</param>
<!--set the size of the buffer when downloading, because it is downloaded from the server, it is recommended not to set too large--
<param name= "BufferSize" >4096</param>
</result>
</action>
My down.jsp:
<a href= "Download.action?filename=hydrangeas.jpg" > click here to download the documentation </a>
Struts2 for file uploads, multiple file uploads and file downloads