struts2文件上传

    技术2022-05-20  35

     

    1. <span style="font-size: large;" mce_style="font-size: large;"><span style="font-size: large;" mce_style="font-size: large;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2. <%@ taglib uri="/struts-tags" prefix="s" %> 3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4. <html> 5. <head> 6. <title>Struts2文件上传示例</title> 7. <meta http-equiv="pragma" content="no-cache"> 8. <meta http-equiv="cache-control" content="no-cache"> 9. <meta http-equiv="expires" content="0"> 10. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 11. <meta http-equiv="description" content="This is my page"> 12. <!-- 13. <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> 14. --> 15. </head> 16. 17. <body> 18. <h3>Struts2文件上传示例</h3><hr/> 19. 20. <form action="fileUpload.action" method="post" enctype="multipart/form-data"> 21. <input type="file" name="up" style="width: 500px"/> <s:fielderror name="up"></s:fielderror><br/> 22. <input type="submit" value="开始上传"/> 23. <s:token/> 24. </form> 25. 26. <h3>Struts2批量文件上传示例</h3><hr/> 27. 28. <form action="batchFileUpload.action" method="post" enctype="multipart/form-data"> 29. <s:fielderror name="up"></s:fielderror><br/> 30. <input type="file" name="up" style="width: 500px"/> <br/> 31. <input type="file" name="up" style="width: 500px"/><br/> 32. <input type="file" name="up" style="width: 500px"/> <br/> 33. <input type="submit" value="开始上传"/> 34. <s:token/> 35. </form> 36. 37. </body> 38. </html></span></span>

     

     

     

    单个文件上传的Action

    # <span style="font-size: large;" mce_style="font-size: large;"><span style="font-size: large;" mce_style="font-size: large;">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; # } # # }</span></span>

     

    多个文件上传的Action

     

    # <span style="font-size: large;" mce_style="font-size: large;"><span style="font-size: large;" mce_style="font-size: large;">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; # } # } # }</span></span>

     

    针对上面两个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

     

     

    1. <span style="font-size: large;" mce_style="font-size: large;"><span style="font-size: large;" mce_style="font-size: large;"><?xml version="1.0" encoding="UTF-8" ?> 2. <!DOCTYPE struts PUBLIC 3. "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" 4. "http://struts.apache.org/dtds/struts-2.1.7.dtd"> 5. <struts> 6. <!-- 请求参数的编码方式 --> 7. <constant name="struts.i18n.encoding" value="UTF-8"/> 8. <!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 --> 9. <constant name="struts.action.extension" value="action,do,go,xkk"/> 10. <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 --> 11. <constant name="struts.configuration.xml.reload" value="true"/> 12. <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 --> 13. <constant name="struts.devMode" value="false"/> 14. <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 --> 15. <constant name="struts.serve.static.browserCache" value="false" /> 16. <!-- 是否允许在OGNL表达式中调用静态方法,默认值为false --> 17. <constant name="struts.ognl.allowStaticMethodAccess" value="true"/> 18. <!-- 指定由spring负责action对象的创建 19. <constant name="struts.objectFactory" value="spring" />--> 20. 21. <constant name="struts.enable.DynamicMethodInvocation" value="false"/> 22. <constant name="struts.multipart.maxSize" value="102400000000000" /> 23. 24. <constant name="struts.custom.i18n.resources" value="msg"/> 25. 26. <package name="my" extends="struts-default" namespace="/"> 27. <interceptors> 28. <interceptor-stack name="myStack"> 29. <interceptor-ref name="defaultStack"/> 30. <interceptor-ref name="token"/> 31. </interceptor-stack> 32. </interceptors> 33. <default-interceptor-ref name="myStack"/> 34. 35. <global-results> 36. <result name="invalid.token">/error.jsp</result> 37. </global-results> 38. 39. <action name="fileUpload" class="com.javacrazyer.action.FileUploadAction"> 40. <param name="destPath">/abc</param> 41. <result name="input">/index.jsp</result> 42. <result>/success.jsp</result> 43. </action> 44. <action name="batchFileUpload" class="com.javacrazyer.action.BatchFileUploadAction"> 45. <param name="destPath">/abc</param> 46. <result name="input">/index.jsp</result> 47. <result>/success.jsp</result> 48. </action> 49. 50. </package> 51. 52. </struts></span></span>

     

    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中的

    1. <span style="font-size: large;" mce_style="font-size: large;"><span style="font-size: large;" mce_style="font-size: large;"><interceptors> 2. <interceptor-stack name="myStack"> 3. <interceptor-ref name="defaultStack"/> 4. <interceptor-ref name="token"/> 5. </interceptor-stack> 6. </interceptors></span></span>

     

     替换成下面的

     

     

     

    1. <span style="font-size: large;" mce_style="font-size: large;"><span style="font-size: large;" mce_style="font-size: large;"> <interceptors> 2. struts2的defaultStack中已经含有fileupload拦截器,如果想加入allowedTypes参数, 3. 需要从新写一个defaultstack ,拷贝过来修改一下 4. <interceptor-stack name="myStack"> 5. <interceptor-ref name="exception"/> 6. <interceptor-ref name="alias"/> 7. <interceptor-ref name="servletConfig"/> 8. <interceptor-ref name="i18n"/> 9. <interceptor-ref name="prepare"/> 10. <interceptor-ref name="chain"/> 11. <interceptor-ref name="debugging"/> 12. <interceptor-ref name="profiling"/> 13. <interceptor-ref name="scopedModelDriven"/> 14. <interceptor-ref name="modelDriven"/> 15. 修改下面的这些文件类型值,就可以限制上传文件的类型了 16. <interceptor-ref name="fileUpload"> 17. <param name="allowedTypes"> 18. image/png,image/gif,image/jpeg,text/plain 19. </param> 20. </interceptor-ref> 21. 上面配置的是上传文件类型的限制,其实共有两个参数 22. maximumSize (可选) - 这个拦截器允许的上传到action中的文件最大长度(以byte为单位). 23. 注意这个参数和在webwork.properties中定义的属性没有关系,默认2MB 24. 25. allowedTypes (可选) - 以逗号分割的contentType类型列表(例如text/html), 26. 这些列表是这个拦截器允许的可以传到action中的contentType.如果没有指定就是允许任何上传类型. 27. 28. 29. 30. <interceptor-ref name="checkbox"/> 31. <interceptor-ref name="staticParams"/> 32. <interceptor-ref name="actionMappingParams"/> 33. <interceptor-ref name="params"> 34. <param name="excludeParams">dojo/..*,^struts/..*</param> 35. </interceptor-ref> 36. <interceptor-ref name="conversionError"/> 37. <interceptor-ref name="validation"> 38. <param name="excludeMethods">input,back,cancel,browse</param> 39. </interceptor-ref> 40. <interceptor-ref name="workflow"> 41. <param name="excludeMethods">input,back,cancel,browse</param> 42. </interceptor-ref> 43. Token,防止重复提交 44. <interceptor-ref name="token"/> 45. 46. </interceptor-stack> 47. </interceptors></span></span>

     

     

     

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

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

     

     

     

    1. <span style="font-size: large;" mce_style="font-size: large;">application/vnd.lotus-1-2-3 2. 3gp video/3gpp 3. aab application/x-authoware-bin 4. aam application/x-authoware-map 5. aas application/x-authoware-seg 6. ai application/postscript 7. aif audio/x-aiff 8. aifc audio/x-aiff 9. aiff audio/x-aiff 10. als audio/X-Alpha5 11. amc application/x-mpeg 12. ani application/octet-stream 13. asc text/plain 14. asd application/astound 15. asf video/x-ms-asf 16. asn application/astound 17. asp application/x-asap 18. asx video/x-ms-asf 19. au audio/basic 20. avb application/octet-stream 21. avi video/x-msvideo 22. awb audio/amr-wb 23. bcpio application/x-bcpio 24. bin application/octet-stream 25. bld application/bld 26. bld2 application/bld2 27. bmp application/x-MS-bmp 28. bpk application/octet-stream 29. bz2 application/x-bzip2 30. cal image/x-cals 31. ccn application/x-cnc 32. cco application/x-cocoa 33. cdf application/x-netcdf 34. cgi magnus-internal/cgi 35. chat application/x-chat 36. class application/octet-stream 37. clp application/x-msclip 38. cmx application/x-cmx 39. co application/x-cult3d-object 40. cod image/cis-cod 41. cpio application/x-cpio 42. cpt application/mac-compactpro 43. crd application/x-mscardfile 44. csh application/x-csh 45. csm chemical/x-csml 46. csml chemical/x-csml 47. css text/css 48. cur application/octet-stream 49. dcm x-lml/x-evm 50. dcr application/x-director 51. dcx image/x-dcx 52. dhtml text/html 53. dir application/x-director 54. dll application/octet-stream 55. dmg application/octet-stream 56. dms application/octet-stream 57. doc application/msword 58. dot application/x-dot 59. dvi application/x-dvi 60. dwf drawing/x-dwf 61. dwg application/x-autocad 62. dxf application/x-autocad 63. dxr application/x-director 64. ebk application/x-expandedbook 65. emb chemical/x-embl-dl-nucleotide 66. embl chemical/x-embl-dl-nucleotide 67. eps application/postscript 68. eri image/x-eri 69. es audio/echospeech 70. esl audio/echospeech 71. etc application/x-earthtime 72. etx text/x-setext 73. evm x-lml/x-evm 74. evy application/x-envoy 75. exe application/octet-stream 76. fh4 image/x-freehand 77. fh5 image/x-freehand 78. fhc image/x-freehand 79. fif image/fif 80. fm application/x-maker 81. fpx image/x-fpx 82. fvi video/isivideo 83. gau chemical/x-gaussian-input 84. gca application/x-gca-compressed 85. gdb x-lml/x-gdb 86. gif image/gif 87. gps application/x-gps 88. gtar application/x-gtar 89. gz application/x-gzip 90. hdf application/x-hdf 91. hdm text/x-hdml 92. hdml text/x-hdml 93. hlp application/winhlp 94. hqx application/mac-binhex40 95. htm text/html 96. html text/html 97. hts text/html 98. ice x-conference/x-cooltalk 99. ico application/octet-stream 100. ief image/ief 101. ifm image/gif 102. ifs image/ifs 103. imy audio/melody 104. ins application/x-NET-Install 105. ips application/x-ipscript 106. ipx application/x-ipix 107. it audio/x-mod 108. itz audio/x-mod 109. ivr i-world/i-vrml 110. j2k image/j2k 111. jad text/vnd.sun.j2me.app-descriptor 112. jam application/x-jam 113. jar application/java-archive 114. jnlp application/x-java-jnlp-file 115. jpe image/jpeg 116. jpeg image/jpeg 117. jpg image/jpeg 118. jpz image/jpeg 119. js application/x-javascript 120. jwc application/jwc 121. kjx application/x-kjx 122. lak x-lml/x-lak 123. latex application/x-latex 124. lcc application/fastman 125. lcl application/x-digitalloca 126. lcr application/x-digitalloca 127. lgh application/lgh 128. lha application/octet-stream 129. lml x-lml/x-lml 130. lmlpack x-lml/x-lmlpack 131. lsf video/x-ms-asf 132. lsx video/x-ms-asf 133. lzh application/x-lzh 134. m13 application/x-msmediaview 135. m14 application/x-msmediaview 136. m15 audio/x-mod 137. m3u audio/x-mpegurl 138. m3url audio/x-mpegurl 139. ma1 audio/ma1 140. ma2 audio/ma2 141. ma3 audio/ma3 142. ma5 audio/ma5 143. man application/x-troff-man 144. map magnus-internal/imagemap 145. mbd application/mbedlet 146. mct application/x-mascot 147. mdb application/x-msaccess 148. mdz audio/x-mod 149. me application/x-troff-me 150. mel text/x-vmel 151. mi application/x-mif 152. mid audio/midi 153. midi audio/midi 154. mif application/x-mif 155. mil image/x-cals 156. mio audio/x-mio 157. mmf application/x-skt-lbs 158. mng video/x-mng 159. mny application/x-msmoney 160. moc application/x-mocha 161. mocha application/x-mocha 162. mod audio/x-mod 163. mof application/x-yumekara 164. mol chemical/x-mdl-molfile 165. mop chemical/x-mopac-input 166. mov video/quicktime 167. movie video/x-sgi-movie 168. mp2 audio/x-mpeg 169. mp3 audio/x-mpeg 170. mp4 video/mp4 171. mpc application/vnd.mpohun.certificate 172. mpe video/mpeg 173. mpeg video/mpeg 174. mpg video/mpeg 175. mpg4 video/mp4 176. mpga audio/mpeg 177. mpn application/vnd.mophun.application 178. mpp application/vnd.ms-project 179. mps application/x-mapserver 180. mrl text/x-mrml 181. mrm application/x-mrm 182. ms application/x-troff-ms 183. mts application/metastream 184. mtx application/metastream 185. mtz application/metastream 186. mzv application/metastream 187. nar application/zip 188. nbmp image/nbmp 189. nc application/x-netcdf 190. ndb x-lml/x-ndb 191. ndwn application/ndwn 192. nif application/x-nif 193. nmz application/x-scream 194. nokia-op-logo image/vnd.nok-oplogo-color 195. npx application/x-netfpx 196. nsnd audio/nsnd 197. nva application/x-neva1 198. oda application/oda 199. oom application/x-AtlasMate-Plugin 200. pac audio/x-pac 201. pae audio/x-epac 202. pan application/x-pan 203. pbm image/x-portable-bitmap 204. pcx image/x-pcx 205. pda image/x-pda 206. pdb chemical/x-pdb 207. pdf application/pdf 208. pfr application/font-tdpfr 209. pgm image/x-portable-graymap 210. pict image/x-pict 211. pm application/x-perl 212. pmd application/x-pmd 213. png image/png 214. pnm image/x-portable-anymap 215. pnz image/png 216. pot application/vnd.ms-powerpoint 217. ppm image/x-portable-pixmap 218. pps application/vnd.ms-powerpoint 219. ppt application/vnd.ms-powerpoint 220. pqf application/x-cprplayer 221. pqi application/cprplayer 222. prc application/x-prc 223. proxy application/x-ns-proxy-autoconfig 224. ps application/postscript 225. ptlk application/listenup 226. pub application/x-mspublisher 227. pvx video/x-pv-pvx 228. qcp audio/vnd.qcelp 229. qt video/quicktime 230. qti image/x-quicktime 231. qtif image/x-quicktime 232. r3t text/vnd.rn-realtext3d 233. ra audio/x-pn-realaudio 234. ram audio/x-pn-realaudio 235. rar application/x-rar-compressed 236. ras image/x-cmu-raster 237. rdf application/rdf+xml 238. rf image/vnd.rn-realflash 239. rgb image/x-rgb 240. rlf application/x-richlink 241. rm audio/x-pn-realaudio 242. rmf audio/x-rmf 243. rmm audio/x-pn-realaudio 244. rmvb audio/x-pn-realaudio 245. rnx application/vnd.rn-realplayer 246. roff application/x-troff 247. rp image/vnd.rn-realpix 248. rpm audio/x-pn-realaudio-plugin 249. rt text/vnd.rn-realtext 250. rte x-lml/x-gps 251. rtf application/rtf 252. rtg application/metastream 253. rtx text/richtext 254. rv video/vnd.rn-realvideo 255. rwc application/x-rogerwilco 256. s3m audio/x-mod 257. s3z audio/x-mod 258. sca application/x-supercard 259. scd application/x-msschedule 260. sdf application/e-score 261. sea application/x-stuffit 262. sgm text/x-sgml 263. sgml text/x-sgml 264. sh application/x-sh 265. shar application/x-shar 266. shtml magnus-internal/parsed-html 267. shw application/presentations 268. si6 image/si6 269. si7 image/vnd.stiwap.sis 270. si9 image/vnd.lgtwap.sis 271. sis application/vnd.symbian.install 272. sit application/x-stuffit 273. skd application/x-Koan 274. skm application/x-Koan 275. skp application/x-Koan 276. skt application/x-Koan 277. slc application/x-salsa 278. smd audio/x-smd 279. smi application/smil 280. smil application/smil 281. smp application/studiom 282. smz audio/x-smd 283. snd audio/basic 284. spc text/x-speech 285. spl application/futuresplash 286. spr application/x-sprite 287. sprite application/x-sprite 288. spt application/x-spt 289. src application/x-wais-source 290. stk application/hyperstudio 291. stm audio/x-mod 292. sv4cpio application/x-sv4cpio 293. sv4crc application/x-sv4crc 294. svf image/vnd 295. svg image/svg-xml 296. svh image/svh 297. svr x-world/x-svr 298. swf application/x-shockwave-flash 299. swfl application/x-shockwave-flash 300. t application/x-troff 301. tad application/octet-stream 302. talk text/x-speech 303. tar application/x-tar 304. taz application/x-tar 305. tbp application/x-timbuktu 306. tbt application/x-timbuktu 307. tcl application/x-tcl 308. tex application/x-tex 309. texi application/x-texinfo 310. texinfo application/x-texinfo 311. tgz application/x-tar 312. thm application/vnd.eri.thm 313. tif image/tiff 314. tiff image/tiff 315. tki application/x-tkined 316. tkined application/x-tkined 317. toc application/toc 318. toy image/toy 319. tr application/x-troff 320. trk x-lml/x-gps 321. trm application/x-msterminal 322. tsi audio/tsplayer 323. tsp application/dsptype 324. tsv text/tab-separated-values 325. tsv text/tab-separated-values 326. ttf application/octet-stream 327. ttz application/t-time 328. txt text/plain 329. ult audio/x-mod 330. ustar application/x-ustar 331. uu application/x-uuencode 332. uue application/x-uuencode 333. vcd application/x-cdlink 334. vcf text/x-vcard 335. vdo video/vdo 336. vib audio/vib 337. viv video/vivo 338. vivo video/vivo 339. vmd application/vocaltec-media-desc 340. vmf application/vocaltec-media-file 341. vmi application/x-dreamcast-vms-info 342. vms application/x-dreamcast-vms 343. vox audio/voxware 344. vqe audio/x-twinvq-plugin 345. vqf audio/x-twinvq 346. vql audio/x-twinvq 347. vre x-world/x-vream 348. vrml x-world/x-vrml 349. vrt x-world/x-vrt 350. vrw x-world/x-vream 351. vts workbook/formulaone 352. wav audio/x-wav 353. wax audio/x-ms-wax 354. wbmp image/vnd.wap.wbmp 355. web application/vnd.xara 356. wi image/wavelet 357. wis application/x-InstallShield 358. wm video/x-ms-wm 359. wma audio/x-ms-wma 360. wmd application/x-ms-wmd 361. wmf application/x-msmetafile 362. wml text/vnd.wap.wml 363. wmlc application/vnd.wap.wmlc 364. wmls text/vnd.wap.wmlscript 365. wmlsc application/vnd.wap.wmlscriptc 366. wmlscript text/vnd.wap.wmlscript 367. wmv audio/x-ms-wmv 368. wmx video/x-ms-wmx 369. wmz application/x-ms-wmz 370. wpng image/x-up-wpng 371. wpt x-lml/x-gps 372. wri application/x-mswrite 373. wrl x-world/x-vrml 374. wrz x-world/x-vrml 375. ws text/vnd.wap.wmlscript 376. wsc application/vnd.wap.wmlscriptc 377. wv video/wavelet 378. wvx video/x-ms-wvx 379. wxl application/x-wxl 380. x-gzip application/x-gzip 381. xar application/vnd.xara 382. xbm image/x-xbitmap 383. xdm application/x-xdma 384. xdma application/x-xdma 385. xdw application/vnd.fujixerox.docuworks 386. xht application/xhtml+xml 387. xhtm application/xhtml+xml 388. xhtml application/xhtml+xml 389. xla application/vnd.ms-excel 390. xlc application/vnd.ms-excel 391. xll application/x-excel 392. xlm application/vnd.ms-excel 393. xls application/vnd.ms-excel 394. xlt application/vnd.ms-excel 395. xlw application/vnd.ms-excel 396. xm audio/x-mod 397. xml text/xml 398. xmz audio/x-mod 399. xpi application/x-xpinstall 400. xpm image/x-xpixmap 401. xsit text/xml 402. xsl text/xml 403. xul text/xul 404. xwd image/x-xwindowdump 405. xyz chemical/x-pdb 406. yz1 application/x-yz1 407. z application/x-compress 408. zac application/x-zaurus-zac 409. zip application/zip </span>

     

     

     

    关于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

     

     

     

     

     

     


    最新回复(0)