`
JavaCrazyer
  • 浏览: 3012202 次
  • 性别: Icon_minigender_1
  • 来自: 河南
社区版块
存档分类

Struts2温习(10)--文件上传

阅读更多

自接触这么多种技术的上传来看,还是Struts2的上传最好用,虽然之前有篇文章已经总结了几乎我接触到的所有类型的上传,但Struts2方面感觉讲的还是不够细致。

本文就单文件上传和批量文件上传来进行讲解

具体示例

首页上传页面


<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Struts2文件上传示例</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <h3>Struts2文件上传示例</h3><hr/>
    
    <form action="fileUpload.action" method="post" enctype="multipart/form-data">
    	<input type="file" name="up" style="width: 500px"/> <s:fielderror name="up"></s:fielderror><br/>
    	<input type="submit" value="开始上传"/>
    	<s:token/>
    </form>
    
    <h3>Struts2批量文件上传示例</h3><hr/>
    
    <form action="batchFileUpload.action" method="post" enctype="multipart/form-data">
    <s:fielderror name="up"></s:fielderror><br/>
    	<input type="file" name="up" style="width: 500px"/> <br/>
    	<input type="file" name="up" style="width: 500px"/><br/>
    	<input type="file" name="up" style="width: 500px"/> <br/>
    	<input type="submit" value="开始上传"/>
    	<s:token/>
    </form>
    
  </body>
</html>


 单文件上传的Action


package com.javacrazyer.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 org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;


public class FileUploadAction extends ActionSupport {
	
	private static final long serialVersionUID = -7001482935770262132L;
    private File up; 
    private String upContentType;
    private String upFileName;
    private String destPath;

	public String execute() throws Exception{
		
		System.out.println("contentType==" + this.upContentType);
		System.out.println("fileName==" + this.upFileName);
		
		File basePath = new File(ServletActionContext.getServletContext().getRealPath(destPath));
		
		if(!basePath.exists()){
			basePath.mkdirs();
		}
		
		copy(up, new File(basePath.getCanonicalPath()+"/" + upFileName));
		
		return SUCCESS;
	}

	
	public void copy(File srcFile, File destFile) throws IOException{
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		try{
			bis = new BufferedInputStream(new FileInputStream(srcFile));
			bos = new BufferedOutputStream(new FileOutputStream(destFile));
			byte[] buf = new byte[8192];
			
			for(int count = -1; (count = bis.read(buf))!= -1; ){
				bos.write(buf, 0, count);
			}
			bos.flush();
		}catch(IOException ie){
			throw ie;
		}finally{
			if(bis != null){
				bis.close();
			}
			if(bos != null){
				bos.close();
			}
		}
	}
	
	

	public File getUp() {
		return up;
	}


	public void setUp(File up) {
		this.up = up;
	}


	public String getUpContentType() {
		return upContentType;
	}


	public void setUpContentType(String upContentType) {
		this.upContentType = upContentType;
	}


	public String getUpFileName() {
		return upFileName;
	}


	public void setUpFileName(String upFileName) {
		this.upFileName = upFileName;
	}


	public String getDestPath() {
		return destPath;
	}

	public void setDestPath(String destPath) {
		this.destPath = destPath;
	}
	
}


 多文件上传的Action


package com.javacrazyer.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.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 批量文件上传
 * 
 * 这里有一点非常重要:就是凡是页面上的file文本域的name=xxx的,
 * 那么Action的三个属性必须为xxx,xxxContentType,xxxFileName
 * 
 * 
 */
public class BatchFileUploadAction extends ActionSupport {
	
	private static final long serialVersionUID = -7001482935770262132L;
	
    private List<File> up; 
    private List<String> upContentType;
    private List<String> upFileName;
    private String destPath;

	public String execute() throws Exception{
		
		//System.out.println("contentType==" + this.upContentType);
		//System.out.println("fileName==" + this.upFileName);
		
		File basePath = new File(ServletActionContext.getServletContext().getRealPath(destPath));
		
		if(!basePath.exists()){
			basePath.mkdirs();
		}
		
		int size = up == null ? 0 : up.size();
		for(int i = 0; i < size; i++){
			/*
			 * 如果需要限制上传文件的类型,那么可以解开此注释
			 * if(isAllowType(upContentType.get(i))){
				copy(up.get(i), new File(basePath.getCanonicalPath()+"/" + upFileName.get(i)));
			}else{
				upFileName.remove(i);
			}*/
			copy(up.get(i), new File(basePath.getCanonicalPath()+"/" + upFileName.get(i)));
		}
		return SUCCESS;
	}

	
	public void copy(File srcFile, File destFile) throws IOException{
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		try{
			bis = new BufferedInputStream(new FileInputStream(srcFile));
			bos = new BufferedOutputStream(new FileOutputStream(destFile));
			byte[] buf = new byte[8192];
			
			for(int count = -1; (count = bis.read(buf))!= -1; ){
				bos.write(buf, 0, count);
			}
			bos.flush();
		}catch(IOException ie){
			throw ie;
		}finally{
			if(bis != null){
				bis.close();
			}
			if(bos != null){
				bos.close();
			}
		}
	}


	public List<File> getUp() {
		return up;
	}


	public void setUp(List<File> up) {
		this.up = up;
	}


	public List<String> getUpContentType() {
		return upContentType;
	}


	public void setUpContentType(List<String> upContentType) {
		this.upContentType = upContentType;
	}


	public List<String> getUpFileName() {
		return upFileName;
	}


	public void setUpFileName(List<String> upFileName) {
		this.upFileName = upFileName;
	}


	public String getDestPath() {
		return destPath;
	}

	public void setDestPath(String destPath) {
		this.destPath = destPath;
	}

	public static boolean isAllowType(String type){
		List<String> types = new ArrayList<String>();
		types.add("image/pjpeg");
		types.add("text/plain");
		types.add("image/gif");
		types.add("image/x-png");
		
		if(types.contains(type)){
			return true;
		}else{
			return false;
		}
	}
}

针对上面两个Action,都得有那么三个属性【上传的文件,文件类型,文件名】,并且开头必须与表单file的name值一样



在Action中添加一个List<File>类型的与页面所有file域同名的属性。private List<File> up;

 添加一个以file域名开头,后面跟ContentType的字符串列表属性,这个由Struts2的文件上传拦截器赋文件类型值。如:private List<String> upContentTyp;

        添加一个以file域名开头,后面跟FileName的字符串列表属性,这个由Struts2的文件上传拦截器赋文件名的值。如:private List<String> upFileName;

       通过IO流循环操作,完成文件的读写。


记住:在struts2的Action中,对于无论是单个文件上传还是批量上传,就是凡是页面上的file文本域的name=xxx的, 那么Action的三个属性必须为xxx,xxxContentType,xxxFileName



 存放上传类型错误信息的资源文件

 msg_zh_CN.properties



struts.messages.error.content.type.not.allowed=\u4E0A\u4F20\u7684\u6587\u4EF6\u7C7B\u578B\u662F\u975E\u6CD5\u7684
#struts.messages.error.uploading=文件不能上传的通用错误信息
#struts.messages.error.file.too.large=上传文件长度过大的错误信息
#struts.messages.error.content.type.not.allowed= 当上传文件不符合指定的contentType

 




 

msg_en_US.properties


struts.messages.error.content.type.not.allowed=upload file contenttype is invalidate


src/struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
	<!-- 请求参数的编码方式 -->
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->
	<constant name="struts.action.extension" value="action,do,go,xkk"/>
	<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开  -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开  -->
	<constant name="struts.devMode" value="false"/>
	<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭  -->
	<constant name="struts.serve.static.browserCache" value="false" />
	<!-- 是否允许在OGNL表达式中调用静态方法,默认值为false -->
	<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
	<!-- 指定由spring负责action对象的创建 
	<constant name="struts.objectFactory" value="spring" />-->
	
	<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
	<constant name="struts.multipart.maxSize" value="102400000000000" /> 
	
	<constant name="struts.custom.i18n.resources" value="msg"/>
	 
	<package name="my" extends="struts-default" namespace="/">
	<interceptors>
			<interceptor-stack name="myStack">
				<interceptor-ref name="defaultStack"/>
				<interceptor-ref name="token"/>
			</interceptor-stack>
		</interceptors>
		<default-interceptor-ref name="myStack"/>
		
		<global-results>
			<result name="invalid.token">/error.jsp</result>
		</global-results>
		
		<action name="fileUpload" class="com.javacrazyer.action.FileUploadAction">
			<param name="destPath">/abc</param>
			<result name="input">/index.jsp</result>
			<result>/success.jsp</result>
		</action>
		<action name="batchFileUpload" class="com.javacrazyer.action.BatchFileUploadAction">
			<param name="destPath">/abc</param>
			<result name="input">/index.jsp</result>
			<result>/success.jsp</result>
		</action>
		
	</package>
	
</struts>

 



token防止重复提交表单【主要是防止上传过后点击浏览器的后退按钮,再次提交这个问题】

  1) 在页面的表单中添加<s:token/>

  2) 在要使用token的Action配置中添加token或tokenSession拦截器。


  

看到没有,我们在配置中还配置了<result name="input">/index.jsp</result>,这样的话,如果上传失败就会跳到index.jsp页面,不过现在

我们还没涉到类型限制的问题,接下来,将的就是如何为上传添加文件类型

struts2中为文件上传添加类型限制有两种方式:

第一种方式在Action纯写代码方式

看BatchFileUploadAction中,copy(up.get(i), new File(basePath.getCanonicalPath()+"/" + upFileName.get(i)));这句话,如果将其上边的被注释的段落注释去掉,将本行注释掉,那么就可以起到文件限制,具体可以在isAllowType这个方法中配置

第二种方式在struts.xml中配置

将上边struts.xml中的


<interceptors>
	<interceptor-stack name="myStack">
		<interceptor-ref name="defaultStack"/>
		<interceptor-ref name="token"/>
	</interceptor-stack>
</interceptors>

 替换成下面的


     <interceptors>
	struts2的defaultStack中已经含有fileupload拦截器,如果想加入allowedTypes参数,
需要从新写一个defaultstack ,拷贝过来修改一下
			<interceptor-stack name="myStack">
                <interceptor-ref name="exception"/>
                <interceptor-ref name="alias"/>
                <interceptor-ref name="servletConfig"/>
                <interceptor-ref name="i18n"/>
                <interceptor-ref name="prepare"/>
                <interceptor-ref name="chain"/>
                <interceptor-ref name="debugging"/>
                <interceptor-ref name="profiling"/>
                <interceptor-ref name="scopedModelDriven"/>
                <interceptor-ref name="modelDriven"/>
                   修改下面的这些文件类型值,就可以限制上传文件的类型了
                <interceptor-ref name="fileUpload">
                  <param name="allowedTypes">
                     image/png,image/gif,image/jpeg,text/plain
                  </param>
                </interceptor-ref>
  上面配置的是上传文件类型的限制,其实共有两个参数
maximumSize (可选) - 这个拦截器允许的上传到action中的文件最大长度(以byte为单位). 
                                                     注意这个参数和在webwork.properties中定义的属性没有关系,默认2MB

allowedTypes (可选) - 以逗号分割的contentType类型列表(例如text/html),
这些列表是这个拦截器允许的可以传到action中的contentType.如果没有指定就是允许任何上传类型.   
           
                
                
                <interceptor-ref name="checkbox"/>
                <interceptor-ref name="staticParams"/>
                <interceptor-ref name="actionMappingParams"/>
                <interceptor-ref name="params">
                  <param name="excludeParams">dojo\..*,^struts\..*</param>
                </interceptor-ref>
                <interceptor-ref name="conversionError"/>
                <interceptor-ref name="validation">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
                <interceptor-ref name="workflow">
                    <param name="excludeMethods">input,back,cancel,browse</param>
                </interceptor-ref>
                  Token,防止重复提交	
                <interceptor-ref name="token"/>

            </interceptor-stack>
</interceptors>

 那么具体想添加什么类型就可以在这个配置文件中 <interceptor-ref name="fileUpload">的内容里进行自定义了

 现在为止,开发中可能用到的类型大致有如下这么多种,也就是mime类型

 

application/vnd.lotus-1-2-3
3gp video/3gpp
aab application/x-authoware-bin
aam application/x-authoware-map
aas application/x-authoware-seg
ai application/postscript
aif audio/x-aiff
aifc audio/x-aiff
aiff audio/x-aiff
als audio/X-Alpha5
amc application/x-mpeg
ani application/octet-stream
asc text/plain
asd application/astound
asf video/x-ms-asf
asn application/astound
asp application/x-asap
asx video/x-ms-asf
au audio/basic
avb application/octet-stream
avi video/x-msvideo
awb audio/amr-wb
bcpio application/x-bcpio
bin application/octet-stream
bld application/bld
bld2 application/bld2
bmp application/x-MS-bmp
bpk application/octet-stream
bz2 application/x-bzip2
cal image/x-cals
ccn application/x-cnc
cco application/x-cocoa
cdf application/x-netcdf
cgi magnus-internal/cgi
chat application/x-chat
class application/octet-stream
clp application/x-msclip
cmx application/x-cmx
co application/x-cult3d-object
cod image/cis-cod
cpio application/x-cpio
cpt application/mac-compactpro
crd application/x-mscardfile
csh application/x-csh
csm chemical/x-csml
csml chemical/x-csml
css text/css
cur application/octet-stream
dcm x-lml/x-evm
dcr application/x-director
dcx image/x-dcx
dhtml text/html
dir application/x-director
dll application/octet-stream
dmg application/octet-stream
dms application/octet-stream
doc application/msword
dot application/x-dot
dvi application/x-dvi
dwf drawing/x-dwf
dwg application/x-autocad
dxf application/x-autocad
dxr application/x-director
ebk application/x-expandedbook
emb chemical/x-embl-dl-nucleotide
embl chemical/x-embl-dl-nucleotide
eps application/postscript
eri image/x-eri
es audio/echospeech
esl audio/echospeech
etc application/x-earthtime
etx text/x-setext
evm x-lml/x-evm
evy application/x-envoy
exe application/octet-stream
fh4 image/x-freehand
fh5 image/x-freehand
fhc image/x-freehand
fif image/fif
fm application/x-maker
fpx image/x-fpx
fvi video/isivideo
gau chemical/x-gaussian-input
gca application/x-gca-compressed
gdb x-lml/x-gdb
gif image/gif
gps application/x-gps
gtar application/x-gtar
gz application/x-gzip
hdf application/x-hdf
hdm text/x-hdml
hdml text/x-hdml
hlp application/winhlp
hqx application/mac-binhex40
htm text/html
html text/html
hts text/html
ice x-conference/x-cooltalk
ico application/octet-stream
ief image/ief
ifm image/gif
ifs image/ifs
imy audio/melody
ins application/x-NET-Install
ips application/x-ipscript
ipx application/x-ipix
it audio/x-mod
itz audio/x-mod
ivr i-world/i-vrml
j2k image/j2k
jad text/vnd.sun.j2me.app-descriptor
jam application/x-jam
jar application/java-archive
jnlp application/x-java-jnlp-file
jpe image/jpeg
jpeg image/jpeg
jpg image/jpeg
jpz image/jpeg
js application/x-javascript
jwc application/jwc
kjx application/x-kjx
lak x-lml/x-lak
latex application/x-latex
lcc application/fastman
lcl application/x-digitalloca
lcr application/x-digitalloca
lgh application/lgh
lha application/octet-stream
lml x-lml/x-lml
lmlpack x-lml/x-lmlpack
lsf video/x-ms-asf
lsx video/x-ms-asf
lzh application/x-lzh
m13 application/x-msmediaview
m14 application/x-msmediaview
m15 audio/x-mod
m3u audio/x-mpegurl
m3url audio/x-mpegurl
ma1 audio/ma1
ma2 audio/ma2
ma3 audio/ma3
ma5 audio/ma5
man application/x-troff-man
map magnus-internal/imagemap
mbd application/mbedlet
mct application/x-mascot
mdb application/x-msaccess
mdz audio/x-mod
me application/x-troff-me
mel text/x-vmel
mi application/x-mif
mid audio/midi
midi audio/midi
mif application/x-mif
mil image/x-cals
mio audio/x-mio
mmf application/x-skt-lbs
mng video/x-mng
mny application/x-msmoney
moc application/x-mocha
mocha application/x-mocha
mod audio/x-mod
mof application/x-yumekara
mol chemical/x-mdl-molfile
mop chemical/x-mopac-input
mov video/quicktime
movie video/x-sgi-movie
mp2 audio/x-mpeg
mp3 audio/x-mpeg
mp4 video/mp4
mpc application/vnd.mpohun.certificate
mpe video/mpeg
mpeg video/mpeg
mpg video/mpeg
mpg4 video/mp4
mpga audio/mpeg
mpn application/vnd.mophun.application
mpp application/vnd.ms-project
mps application/x-mapserver
mrl text/x-mrml
mrm application/x-mrm
ms application/x-troff-ms
mts application/metastream
mtx application/metastream
mtz application/metastream
mzv application/metastream
nar application/zip
nbmp image/nbmp
nc application/x-netcdf
ndb x-lml/x-ndb
ndwn application/ndwn
nif application/x-nif
nmz application/x-scream
nokia-op-logo image/vnd.nok-oplogo-color
npx application/x-netfpx
nsnd audio/nsnd
nva application/x-neva1
oda application/oda
oom application/x-AtlasMate-Plugin
pac audio/x-pac
pae audio/x-epac
pan application/x-pan
pbm image/x-portable-bitmap
pcx image/x-pcx
pda image/x-pda
pdb chemical/x-pdb
pdf application/pdf
pfr application/font-tdpfr
pgm image/x-portable-graymap
pict image/x-pict
pm application/x-perl
pmd application/x-pmd
png image/png
pnm image/x-portable-anymap
pnz image/png
pot application/vnd.ms-powerpoint
ppm image/x-portable-pixmap
pps application/vnd.ms-powerpoint
ppt application/vnd.ms-powerpoint
pqf application/x-cprplayer
pqi application/cprplayer
prc application/x-prc
proxy application/x-ns-proxy-autoconfig
ps application/postscript
ptlk application/listenup
pub application/x-mspublisher
pvx video/x-pv-pvx
qcp audio/vnd.qcelp
qt video/quicktime
qti image/x-quicktime
qtif image/x-quicktime
r3t text/vnd.rn-realtext3d
ra audio/x-pn-realaudio
ram audio/x-pn-realaudio
rar application/x-rar-compressed
ras image/x-cmu-raster
rdf application/rdf+xml
rf image/vnd.rn-realflash
rgb image/x-rgb
rlf application/x-richlink
rm audio/x-pn-realaudio
rmf audio/x-rmf
rmm audio/x-pn-realaudio
rmvb audio/x-pn-realaudio
rnx application/vnd.rn-realplayer
roff application/x-troff
rp image/vnd.rn-realpix
rpm audio/x-pn-realaudio-plugin
rt text/vnd.rn-realtext
rte x-lml/x-gps
rtf application/rtf
rtg application/metastream
rtx text/richtext
rv video/vnd.rn-realvideo
rwc application/x-rogerwilco
s3m audio/x-mod
s3z audio/x-mod
sca application/x-supercard
scd application/x-msschedule
sdf application/e-score
sea application/x-stuffit
sgm text/x-sgml
sgml text/x-sgml
sh application/x-sh
shar application/x-shar
shtml magnus-internal/parsed-html
shw application/presentations
si6 image/si6
si7 image/vnd.stiwap.sis
si9 image/vnd.lgtwap.sis
sis application/vnd.symbian.install
sit application/x-stuffit
skd application/x-Koan
skm application/x-Koan
skp application/x-Koan
skt application/x-Koan
slc application/x-salsa
smd audio/x-smd
smi application/smil
smil application/smil
smp application/studiom
smz audio/x-smd
snd audio/basic
spc text/x-speech
spl application/futuresplash
spr application/x-sprite
sprite application/x-sprite
spt application/x-spt
src application/x-wais-source
stk application/hyperstudio
stm audio/x-mod
sv4cpio application/x-sv4cpio
sv4crc application/x-sv4crc
svf image/vnd
svg image/svg-xml
svh image/svh
svr x-world/x-svr
swf application/x-shockwave-flash
swfl application/x-shockwave-flash
t application/x-troff
tad application/octet-stream
talk text/x-speech
tar application/x-tar
taz application/x-tar
tbp application/x-timbuktu
tbt application/x-timbuktu
tcl application/x-tcl
tex application/x-tex
texi application/x-texinfo
texinfo application/x-texinfo
tgz application/x-tar
thm application/vnd.eri.thm
tif image/tiff
tiff image/tiff
tki application/x-tkined
tkined application/x-tkined
toc application/toc
toy image/toy
tr application/x-troff
trk x-lml/x-gps
trm application/x-msterminal
tsi audio/tsplayer
tsp application/dsptype
tsv text/tab-separated-values
tsv text/tab-separated-values
ttf application/octet-stream
ttz application/t-time
txt text/plain
ult audio/x-mod
ustar application/x-ustar
uu application/x-uuencode
uue application/x-uuencode
vcd application/x-cdlink
vcf text/x-vcard
vdo video/vdo
vib audio/vib
viv video/vivo
vivo video/vivo
vmd application/vocaltec-media-desc
vmf application/vocaltec-media-file
vmi application/x-dreamcast-vms-info
vms application/x-dreamcast-vms
vox audio/voxware
vqe audio/x-twinvq-plugin
vqf audio/x-twinvq
vql audio/x-twinvq
vre x-world/x-vream
vrml x-world/x-vrml
vrt x-world/x-vrt
vrw x-world/x-vream
vts workbook/formulaone
wav audio/x-wav
wax audio/x-ms-wax
wbmp image/vnd.wap.wbmp
web application/vnd.xara
wi image/wavelet
wis application/x-InstallShield
wm video/x-ms-wm
wma audio/x-ms-wma
wmd application/x-ms-wmd
wmf application/x-msmetafile
wml text/vnd.wap.wml
wmlc application/vnd.wap.wmlc
wmls text/vnd.wap.wmlscript
wmlsc application/vnd.wap.wmlscriptc
wmlscript text/vnd.wap.wmlscript
wmv audio/x-ms-wmv
wmx video/x-ms-wmx
wmz application/x-ms-wmz
wpng image/x-up-wpng
wpt x-lml/x-gps
wri application/x-mswrite
wrl x-world/x-vrml
wrz x-world/x-vrml
ws text/vnd.wap.wmlscript
wsc application/vnd.wap.wmlscriptc
wv video/wavelet
wvx video/x-ms-wvx
wxl application/x-wxl
x-gzip application/x-gzip
xar application/vnd.xara
xbm image/x-xbitmap
xdm application/x-xdma
xdma application/x-xdma
xdw application/vnd.fujixerox.docuworks
xht application/xhtml+xml
xhtm application/xhtml+xml
xhtml application/xhtml+xml
xla application/vnd.ms-excel
xlc application/vnd.ms-excel
xll application/x-excel
xlm application/vnd.ms-excel
xls application/vnd.ms-excel
xlt application/vnd.ms-excel
xlw application/vnd.ms-excel
xm audio/x-mod
xml text/xml
xmz audio/x-mod
xpi application/x-xpinstall
xpm image/x-xpixmap
xsit text/xml
xsl text/xml
xul text/xul
xwd image/x-xwindowdump
xyz chemical/x-pdb
yz1 application/x-yz1
z application/x-compress
zac application/x-zaurus-zac
zip application/zip 

 关于office2007的几个常用的mime类型为

 

.dotx,application/vnd.openxmlformats-officedocument.wordprocessingml.template

.docx,application/vnd.openxmlformats-officedocument.wordprocessingml.document

.xlsx,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

.pptx,application/vnd.openxmlformats-officedocument.presentationml.presentation

 

到此,文件类型被限制住了,那么当类型不对时,<result name="input">/index.jsp</result>这个就起作用了,意思

就是凡是上传遇到的类型不是规定类型的文件时,都会跳到index.jsp就是还是上传页面并显示错误信息,错误信息呢是利用了国际化配置在资源文件中的,页面上用 <s:fielderror name="up"></s:fielderror>来显示错误信息


假如我们限制类型为: image/png,image/gif,image/jpeg,text/plain,application/pdf,text/html,application/msword


那么上传的效果为

单独上传




 上传后




 如果我们不点击返回继续上传,而是点击浏览器上的后退按钮,那么这时TOKEN将起作用,提示下面的页面


如果我们上传一个不在限定类型内的文件的话,也会报错



 上传后的结果



 对于批量上传



 上传后





 
 


 
 


 
 





 

  • 大小: 2 KB
  • 大小: 9.2 KB
  • 大小: 2 KB
  • 大小: 587 Bytes
  • 大小: 10.6 KB
  • 大小: 4.7 KB
  • 大小: 9.6 KB
  • 大小: 10.2 KB
分享到:
评论
3 楼 伽马裕 2013-01-15  
求源代码,liu157689735@qq.com,感激不尽...
2 楼 datouren 2012-08-04  
好了 我已经调试成功了,没有问题 的 谢谢
1 楼 datouren 2012-08-03  
您好 我想要 这个上传下载的 这个项目的源代码,,在您不忙的时候 发给我 好吗?我的qq:907003340@qq.com 很感谢你 写了这么好的文章

相关推荐

Global site tag (gtag.js) - Google Analytics