struts2上传文件
java 2011-03-11 23:00:50 阅读18 评论0 字号:大中小 订阅
Java代码
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts2 File Upload</title>
</head>
<body>
<s:text name=""></s:text>
<form action="fileUpload.action" method="POST" enctype="multipart/form-data">
文件标题:<input type="text" name="title" size="50"/><br/>
选择文件:<input type="file" name="upload" size="50"/><br/>
选择文件:<input type="file" name="upload" size="50"/><br/>
选择文件:<input type="file" name="upload" size="50"/><br/>
<input type="submit" value=" 上传 "/>
</form>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts2 File Upload</title> </head> <body> <s:text name=""></s:text> <form action="fileUpload.action" method="POST" enctype="multipart/form-data"> 文件标题:<input type="text" name="title" size="50"/><br/> 选择文件:<input type="file" name="upload" size="50"/><br/> 选择文件:<input type="file" name="upload" size="50"/><br/> 选择文件:<input type="file" name="upload" size="50"/><br/> <input type="submit" value=" 上传 "/> </form> </body> </html>
baseAction文件
Java代码
//pizza
package com.trendcom.upload.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport {
public HttpServletRequest getRequest(){
return ServletActionContext.getRequest();
}
public HttpServletResponse getResponse(){
return ServletActionContext.getResponse();
}
public HttpSession getSession(){
return getRequest().getSession();
}
public ServletContext getServletContext(){
return ServletActionContext.getServletContext();
}
public String getRealyPath(String path){
return getServletContext().getRealPath(path);
}
}
//pizza
package com.trendcom.upload.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport {
public HttpServletRequest getRequest(){
return ServletActionContext.getRequest();
}
public HttpServletResponse getResponse(){
return ServletActionContext.getResponse();
}
public HttpSession getSession(){
return getRequest().getSession();
}
public ServletContext getServletContext(){
return ServletActionContext.getServletContext();
}
public String getRealyPath(String path){
return getServletContext().getRealPath(path);
}
}
action文件
Java代码
package com.trendcom.upload.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class UploadFileAction extends BaseAction {
private static final int BUFFER_SIZE=16*1024;
// 文件标题
private String title;
// 用File数组来封装多个上传文件域对象
private File[] upload;
// 用String数组来封装多个上传文件名
private String[] uploadFileName;
// 用String数组来封装多个上传文件类型
private String[] uploadContentType;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
//以下为所有属性的getter和setter。省略。。。
// 自己封装的一个把源文件对象复制成目标文件对象
private static boolean copy(File src, File dst) {
boolean result=false;
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
result=true;
} catch (Exception e) {
e.printStackTrace();
result=false;
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
@Override
public String execute() throws Exception {
File[] srcFiles = this.getUpload();
List<String> successFileList=new ArrayList<String>();
// 处理每个要上传的文件
for (int i = 0; i < srcFiles.length; i++) {
// 根据服务器的文件保存地址和原文件名创建目录文件全路径
String dstPath = getRealyPath(getSavePath())
+ "//" + this.getUploadFileName()[i];
File dstFile = new File(dstPath);
if(copy(srcFiles[i], dstFile)){
successFileList.add(getUploadFileName()[i]);
}
}
getRequest().setAttribute("successFileList", successFileList);
return SUCCESS;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
package com.trendcom.upload.action; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; public class UploadFileAction extends BaseAction { private static final int BUFFER_SIZE=16*1024; // 文件标题 private String title; // 用File数组来封装多个上传文件域对象 private File[] upload; // 用String数组来封装多个上传文件名 private String[] uploadFileName; // 用String数组来封装多个上传文件类型 private String[] uploadContentType; // 保存文件的目录路径(通过依赖注入) private String savePath; //以下为所有属性的getter和setter。省略。。。 // 自己封装的一个把源文件对象复制成目标文件对象 private static boolean copy(File src, File dst) { boolean result=false; InputStream in = null; OutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; int len = 0; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } result=true; } catch (Exception e) { e.printStackTrace(); result=false; } finally { if (null != in) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != out) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } @Override public String execute() throws Exception { File[] srcFiles = this.getUpload(); List<String> successFileList=new ArrayList<String>(); // 处理每个要上传的文件 for (int i = 0; i < srcFiles.length; i++) { // 根据服务器的文件保存地址和原文件名创建目录文件全路径 String dstPath = getRealyPath(getSavePath()) + "//" + this.getUploadFileName()[i]; File dstFile = new File(dstPath); if(copy(srcFiles[i], dstFile)){ successFileList.add(getUploadFileName()[i]); } } getRequest().setAttribute("successFileList", successFileList); return SUCCESS; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public File[] getUpload() { return upload; } public void setUpload(File[] upload) { this.upload = upload; } public String[] getUploadFileName() { return uploadFileName; } public void setUploadFileName(String[] uploadFileName) { this.uploadFileName = uploadFileName; } public String[] getUploadContentType() { return uploadContentType; } public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; } public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } }
配置文件
Java代码
<?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="com.trendcom.upload.action" extends="struts-default">
<action name="fileUpload" class="com.trendcom.upload.action.UploadFileAction">
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型,多个用","分隔 -->
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg
</param>
<!-- 配置允许上传的文件大小,单位字节 -->
<param name="maximumSize">102400</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<!-- 动态设置Action中的savePath属性的值 -->
<param name="savePath">/upload</param>
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
<?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="com.trendcom.upload.action" extends="struts-default"> <action name="fileUpload" class="com.trendcom.upload.action.UploadFileAction"> <interceptor-ref name="fileUpload"> <!-- 配置允许上传的文件类型,多个用","分隔 --> <param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg </param> <!-- 配置允许上传的文件大小,单位字节 --> <param name="maximumSize">102400</param> </interceptor-ref> <interceptor-ref name="defaultStack" /> <!-- 动态设置Action中的savePath属性的值 --> <param name="savePath">/upload</param> <result name="input">/index.jsp</result> <result name="success">/success.jsp</result> </action> </package> </struts>
所需lib包括(struts2基本包就不在这里列出来)
Java代码
commons-fileupload-1[1].2.jar
commons-io-1.4.jar
commons-fileupload-1[1].2.jar commons-io-1.4.jar
上传类型.当中可能有些是旧的如果想查看struts2中的上传类型可以下载本文的附件.
FileUploadInterceptor
把FileUploadInterceptor.class代替struts2包中类后,就会在控制台中输出所上传的文件是什么类型
Java代码
Description of Data Content Typical Filename Extensions
MIME type/subtype
Text and Text-Related Types
HTML text data (RFC 1866) html htm text/html
Plain text: documents; program listings txt c c++ pl cc h text/plain
Richtext (obsolete - replaced by text/enriched) text/richtext
Structure enhanced text (etx?) text/x-setext
Enriched text markup (RFC 1896) text/enriched
Tab-separated values (tabular) (tsv?) text/tab-separated-values
SGML documents (RFC 1874) text/sgml
Speech synthesis data (MVP Solutions) talk text/x-speech
Document Stylesheet Types
Cascading Stylesheets css text/css
DSSSL-online stylesheets application/dsssl (proposed)
Image Types
GIF gif image/gif
X-Windows bitmap (b/w) xbm image/x-xbitmap
X-Windows pixelmap (8-bit color) xpm image/x-xpixmap
Portable Network Graphics png image/x-png
Image Exchange Format (RFC 1314) ief image/ief
JPEG jpeg jpg jpe image/jpeg
TIFF tiff tif image/tiff
RGB rgb image/rgb
image/x-rgb
Group III Fax (RFC 1494) g3f image/g3fax
X Windowdump format xwd image/x-xwindowdump
Macintosh PICT format pict image/x-pict
PPM (UNIX PPM package) ppm image/x-portable-pixmap
PGM (UNIX PPM package) pgm image/x-portable-graymap
PBM (UNIX PPM package) pbm image/x-portable-bitmap
PNM (UNIX PPM package) pnm image/x-portable-anymap
Microsoft Windows bitmap bmp image/x-ms-bmp
CMU raster ras image/x-cmu-raster
Kodak Photo-CD pcd image/x-photo-cd
Computer Graphics Metafile cgm image/cgm
North Am. Presentation Layer Protocol image/naplps
CALS Type 1 or 2 mil cal image/x-cals
Fractal Image Format (Iterated Systems) fif image/fif
QuickSilver active image (Micrografx) dsf image/x-mgx-dsf
CMX vector image (Corel) cmx image/x-cmx
Wavelet-compressed (Summus) wi image/wavelet
AutoCad Drawing (SoftSource) dwg image/vnd.dwg
image/x-dwg
AutoCad DXF file (SoftSource) dxf image/vnd.dxf
image/x-dxf
Simple Vector Format (SoftSource) svf image/vnd.svf
also vector/x-svf
Audio/Voice/Music Related Types
"basic"audio - 8-bit u-law PCM au snd audio/basic
Macintosh audio format (AIpple) aif aiff aifc audio/x-aiff
Microsoft audio wav audio/x-wav
MPEG audio mpa abs mpega audio/x-mpeg
MPEG-2 audio mp2a mpa2 audio/x-mpeg-2
compressed speech (Echo Speech Corp.) es audio/echospeech
Toolvox speech audio (Voxware) vox audio/voxware
RapidTransit compressed audio (Fast Man) lcc application/fastman
Realaudio (Progressive Networks) ra ram application/x-pn-realaudio
NIFF music notation data format application/vnd.music-niff
MIDI music data mmid x-music/x-midi
Koan music data (SSeyo) skp application/vnd.koan
application/x-koan
Speech synthesis data (MVP Solutions) talk text/x-speech
Video Types
MPEG video mpeg mpg mpe video/mpeg
MPEG-2 video mpv2 mp2v video/mpeg-2
Macintosh Quicktime qt mov video/quicktime
Microsoft video avi video/x-msvideo
SGI Movie format movie video/x-sgi-movie
VDOlive streaming video (VDOnet) vdo video/vdo
Vivo streaming video (Vivo software) viv video/vnd.vivo
video/vivo
Special HTTP/Web Application Types
Proxy autoconfiguration (Netscape browsers) pac application/x-ns-proxy-autoconfig
See Chapter 6 application/x-www-form-urlencoded
See Chapter 9 application/x-www-local-exec
See Chapter 9 (Netscape extension) multipart/x-mixed-replace
See Chapter 9 and Appendix B multipart/form-data
Netscape Cooltalk chat data (Netscape) ice x-conference/x-cooltalk
Interactive chat (Ichat) application/x-chat
Application Types
Text-Related
PostScript ai eps ps application/postscript
Microsoft Rich Text Format rtf application/rtf
Adobe Acrobat PDF pdf application/pdf
application/x-pdf
Maker Interchange Format (FrameMaker) mif application/vnd.mif
application/x-mif
Troff document t tr roff application/x-troff
Troff document with MAN macros man application/x-troff-man
Troff document with ME macros me application/x-troff-me
Troff document with MS macros ms application/x-troff-ms
LaTeX document latex application/x-latex
Tex/LateX document tex application/x-tex
GNU TexInfo document texinfo texi application/x-texinfo
TeX dvi format dvi application/x-dvi
MacWrite document ?? application/macwriteii
MS word document ?? application/msword
WordPerfect 5.1 document ?? application/wordperfect5.1
SGML application (RFC 1874) application/sgml
Office Document Architecture oda application/oda
Envoy Document evy application/envoy
Wang Info. Tranfer Format (Wang) application/wita
DEC Document Transfer Format (DEC) application/dec-dx
IBM Document Content Architecture (IBM) application/dca-rft
CommonGround Digital Paper (No Hands Software) application/commonground
FrameMaker Documents (Frame) doc fm frm frame application/vnd.framemaker
application/x-framemaker
Remote printing at arbitrary printers (RFC 1486) application/remote-printing
Archive/Compressed Archives
Gnu tar format gtar application/x-gtar
4.3BSD tar format tar application/x-tar
POSIX tar format ustar application/x-ustar
Old CPIO format bcpio application/x-bcpio
POSIX CPIO format cpio application/x-cpio
UNIX sh shell archive shar application/x-shar
DOS/PC - Pkzipped archive zip application/zip
Macintosh Binhexed archive hqx application/mac-binhex40
Macintosh Stuffit Archive sit sea application/x-stuffit
Fractal Image Format fif application/fractals
Binary, UUencoded bin uu application/octet-stream
PC executable exe application/octet-stream
WAIS "sources" src wsrc application/x-wais-source
NCSA HDF data format hdf application/hdf
Downloadable Program/Scripts
Javascript program js ls mocha text/javascript
application/x-javascript
VBScript program text/vbscript
UNIX bourne shell program sh application/x-sh
UNIX c-shell program csh application/x-csh
Perl program pl application/x-perl
Tcl (Tool Control Language) program tcl application/x-tcl
Atomicmail program scripts (obsolete) application/atomicmail
Slate documents - executable enclosures (BBN) application/slate
Undefined binary data (often executable progs) application/octet-stream
RISC OS Executable programs (ANT Limited) application/riscos
Animation/Multimedia
Andrew Toolkit inset application/andrew-inset
FutureSplash vector animation (FutureWave) spl application/futuresplash
mBED multimedia data (mBED) mbd application/mbedlet
Macromedia Shockwave (Macromedia) application/x-director
Sizzler real-time video/animation application/x-sprite
PowerMedia multimedia (RadMedia) rad application/x-rad-powermedia
Presentation
PowerPoint presentation (Microsoft) ppz application/mspowerpoint
PointPlus presentation data (Net Scene) css application/x-pointplus
ASAP WordPower (Software Publishing Corp.) asp application/x-asap
Astound Web Player multimedia data (GoldDisk) asn application/astound
Special Embedded Object
OLE script e.g. Visual Basic (Ncompass) axs application/x-olescript
OLE Object (Microsoft/NCompass) ods application/x-oleobject
OpenScape OLE/OCX objects (Business@Web) opp x-form/x-openscape
Visual Basic objects (Amara) wba application/x-webbasic
Specialized data entry forms (Alpha Software) frm application/x-alpha-form
client-server objects (Wayfarer Communications) wfx x-script/x-wfxclient
General Applications
Undefined binary data (often executable progs) application/octet-stream
CALS (U.S. D.O.D data format - RFC 1895) application/cals-1840
Pointcast news data (Pointcast) pcn application/x-pcn
Excel spreadsheet (Microsoft) application/vnd.ms-excel
application/x-msexcel
application/ms-excel
PowerPoint (Microsoft) ppt application/vnd.ms-powerpoint
application/ms-powerpoint
Microsoft Project (Microsoft) application/vnd.ms-project
Works data (Microsoft) application/vnd.ms-works
MAPI data (Microsoft) application/vnd.ms-tnef
Artgallery data (Microsoft) application/vnd.artgalry
SourceView document (Dataware Electronics) svd application/vnd.svd
Truedoc (Bitstream) application/vnd.truedoc
Net Install - software install (20/20 Software) ins application/x-net-install
Carbon Copy - remote control/access (Microcom) ccv application/ccv
Spreadsheets (Visual Components) vts workbook/formulaone
Cybercash digital money (Cybercash) application/cybercash
Format for sending generic Macintosh files application/applefile
Active message -- connect to active mail app. application/activemessage
X.400 mail message body part (RFC 1494) application/x400-bp
USENET news message id (RFC 1036) application/news-message-id
USENET news message (RFC 1036) application/news-transmission
Multipart Types (mostly email)
Messages with multiple parts multipart/mixed
Messages with multiple, alternative parts multipart/alternative
Message with multiple, related parts multipart/related
Multiple parts are digests multipart/digest
For reporting of email status (admin.) multipart/report
Order of parts does not matter multipart/parallel
Macintosh file data multipart/appledouble
Aggregate messages; descriptor as header multipart/header-set
Container for voice-mail multipart/voice-message
HTML FORM data (see Ch. 9 and App. B) multipart/form-data
Infinite multiparts - See Chapter 9 (Netscape) multipart/x-mixed-replace
Message Types (mostly email)
MIME message message/rfc822
Partial message message/partial
Message containing external references message/external-body
Message containing USENET news message/news
HTTP message message/http
2D/3D Data/Virtual Reality Types
VRML data file wrl vrml x-world/x-vrml
(changing to model/vrml)
WIRL - VRML data (VREAM) vrw x-world/x-vream
Play3D 3d scene data (Play3D) p3d application/x-p3d
Viscape Interactive 3d world data (Superscape) svr x-world/x-svr
WebActive 3d data (Plastic Thought) wvr x-world/x-wvr
QuickDraw3D scene data (Apple) 3dmf x-world/x-3dmf
Scientific/Math/CAD Types
Chemical types -- to communicate information about chemical models chemical/* (several subtypes)
Mathematica notebook ma application/mathematica
Computational meshes for numerical simulations msh x-model/x-mesh
(evolving to model/mesh)
Vis5D 5-dimensional data v5d application/vis5d
IGES models -- CAD/CAM (CGM) data igs application/iges
(evolving to model/iges?)
Autocad WHIP vector drawings dwf drawing/x-dwf
Largely Platform-Specific Types
Silicon Graphics Specific Types
Showcase Presentations showcase slides sc sho show application/x-showcase
Insight Manual pages ins insight application/x-insight
Iris Annotator data ano application/x-annotator
Directory Viewer dir application/x-dirview
Software License lic application/x-enterlicense
Fax manager file faxmgr application/x-fax-manager
Fax job data file faxmgrjob application/x-fax-manager-job
IconBook data icnbk application/x-iconbook
? wb application/x-inpview
Installable software in 'inst' format inst application/x-install
Mail folder mail application/x-mailfolder
? pp ppages application/x-ppages
Data for printer (via lpr) sgi-lpr application/x-sgi-lpr
Software in 'tardist' format tardist application/x-tardist
Software in compressed 'tardist' format ztardist application/x-ztardist
WingZ spreadsheet wkz application/x-wingz
Open Inventor 3-D scenes iv graphics/x-inventor