struts2 上传文件在action里必须提供
private File url; //strunts2 自动将文件流--》临时储存成tmp文件
private String urlContentType; // 上传文件类型--与文件扩展名不同
private String urlFileName; // 上传文件的真实名字
三个属性,
且action必须实现一个ServletResponseAware
String fileEx = urlFileName.substring(urlFileName.indexOf("."),
urlFileName.length());
可以获取文件后缀
"文件后缀名" 与"ContentType类型"不是同一个意思
"文件后缀名" = "ContentType类型"
".*"="application/octet-stream"
".001"="application/x-001"
".301"="application/x-301"
".323"="text/h323"
".906"="application/x-906"
".907"="drawing/907"
".a11"="application/x-a11"
".acp"="audio/x-mei-aac"
".ai"="application/postscript"
".aif"="audio/aiff"
".aifc"="audio/aiff"
".aiff"="audio/aiff"
".anv"="application/x-anv"
".asa"="text/asa"
".asf"="video/x-ms-asf"
".asp"="text/asp"
".asx"="video/x-ms-asf"
".au"="audio/basic"
".avi"="video/avi"
".awf"="application/vnd.adobe.workflow"
".biz"="text/xml"
".bmp"="application/x-bmp"
".bot"="application/x-bot"
".c4t"="application/x-c4t"
".c90"="application/x-c90"
".cal"="application/x-cals"
".cat"="application/vnd.ms-pki.seccat"
".cdf"="application/x-netcdf"
".cdr"="application/x-cdr"
".cel"="application/x-cel"
".cer"="application/x-x509-ca-cert"
".cg4"="application/x-g4"
".cgm"="application/x-cgm"
".cit"="application/x-cit"
".class"="Java/*"
".cml"="text/xml"
".cmp"="application/x-cmp"
".cmx"="application/x-cmx"
".cot"="application/x-cot"
".crl"="application/pkix-crl"
".crt"="application/x-x509-ca-cert"
".csi"="application/x-csi"
".css"="text/css"
".cut"="application/x-cut"
".dbf"="application/x-dbf"
".dbm"="application/x-dbm"
".dbx"="application/x-dbx"
".dcd"="text/xml"
".dcx"="application/x-dcx"
".der"="application/x-x509-ca-cert"
".dgn"="application/x-dgn"
".dib"="application/x-dib"
".dll"="application/x-msdownload"
".doc"="application/msword"
".dot"="application/msword"
".drw"="application/x-drw"
".dtd"="text/xml"
".dwf"="Model/vnd.dwf"
".dwf"="application/x-dwf"
".dwg"="application/x-dwg"
".dxb"="application/x-dxb"
".dxf"="application/x-dxf"
".edn"="application/vnd.adobe.edn"
".emf"="application/x-emf"
".eml"="message/rfc822"
".ent"="text/xml"
".epi"="application/x-epi"
".eps"="application/x-ps"
".eps"="application/postscript"
".etd"="application/x-ebx"
".exe"="application/x-msdownload"
".fax"="image/fax"
".fdf"="application/vnd.fdf"
".fif"="application/fractals"
".fo"="text/xml"
".frm"="application/x-frm"
".g4"="application/x-g4"
".gbr"="application/x-gbr"
".gcd"="application/x-gcd"
".gif"="image/gif"
".gl2"="application/x-gl2"
".gp4"="application/x-gp4"
".hgl"="application/x-hgl"
".hmr"="application/x-hmr"
".hpg"="application/x-hpgl"
".hpl"="application/x-hpl"
".hqx"="application/mac-binhex40"
".hrf"="application/x-hrf"
".hta"="application/hta"
".htc"="text/x-component"
".htm"="text/html"
".html"="text/html"
".htt"="text/webviewhtml"
".htx"="text/html"
".icb"="application/x-icb"
".ico"="image/x-icon"
".ico"="application/x-ico"
".iff"="application/x-iff"
".ig4"="application/x-g4"
".igs"="application/x-igs"
".iii"="application/x-iphone"
".img"="application/x-img"
".ins"="application/x-internet-signup"
".isp"="application/x-internet-signup"
".IVF"="video/x-ivf"
".java"="java/*"
".jfif"="image/jpeg"
".jpe"="image/jpeg"
".jpe"="application/x-jpe"
".jpeg"="image/jpeg"
".jpg"="image/jpeg"
".jpg"="application/x-jpg"
".js"="application/x-JavaScript"
".jsp"="text/html"
".la1"="audio/x-liquid-file"
".lar"="application/x-laplayer-reg"
".latex"="application/x-latex"
".lavs"="audio/x-liquid-secure"
".lbm"="application/x-lbm"
".lmsff"="audio/x-la-lms"
".ls"="application/x-javascript"
".ltr"="application/x-ltr"
".m1v"="video/x-mpeg"
".m2v"="video/x-mpeg"
".m3u"="audio/mpegurl"
".m4e"="video/mpeg4"
".mac"="application/x-mac"
".man"="application/x-troff-man"
".math"="text/xml"
".mdb"="application/msaccess"
".mdb"="application/x-mdb"
".mfp"="application/x-shockwave-flash"
".mht"="message/rfc822"
".mhtml"="message/rfc822"
".mi"="application/x-mi"
".mid"="audio/mid"
".midi"="audio/mid"
".mil"="application/x-mil"
".mml"="text/xml"
".mnd"="audio/x-musicnet-download"
".mns"="audio/x-musicnet-stream"
".mocha"="application/x-javascript"
".movie"="video/x-sgi-movie"
".mp1"="audio/mp1"
".mp2"="audio/mp2"
".mp2v"="video/mpeg"
".mp3"="audio/mp3"
".mp4"="video/mpeg4"
".mpa"="video/x-mpg"
".mpd"="application/vnd.ms-project"
".mpe"="video/x-mpeg"
".mpeg"="video/mpg"
".mpg"="video/mpg"
".mpga"="audio/rn-mpeg"
".mpp"="application/vnd.ms-project"
".mps"="video/x-mpeg"
".mpt"="application/vnd.ms-project"
".mpv"="video/mpg"
".mpv2"="video/mpeg"
".mpw"="application/vnd.ms-project"
".mpx"="application/vnd.ms-project"
".mtx"="text/xml"
".mxp"="application/x-mmxp"
".net"="image/pnetvue"
".nrf"="application/x-nrf"
".nws"="message/rfc822"
".odc"="text/x-ms-odc"
".out"="application/x-out"
".p10"="application/pkcs10"
".p12"="application/x-pkcs12"
".p7b"="application/x-pkcs7-certificates"
".p7c"="application/pkcs7-mime"
".p7m"="application/pkcs7-mime"
".p7r"="application/x-pkcs7-certreqresp"
".p7s"="application/pkcs7-signature"
".pc5"="application/x-pc5"
".pci"="application/x-pci"
".pcl"="application/x-pcl"
".pcx"="application/x-pcx"
".pdf"="application/pdf"
".pdf"="application/pdf"
".pdx"="application/vnd.adobe.pdx"
".pfx"="application/x-pkcs12"
".pgl"="application/x-pgl"
".pic"="application/x-pic"
".pko"="application/vnd.ms-pki.pko"
".pl"="application/x-perl"
".plg"="text/html"
".pls"="audio/scpls"
".plt"="application/x-plt"
".png"="image/png"
".png"="application/x-png"
".pot"="application/vnd.ms-powerpoint"
".ppa"="application/vnd.ms-powerpoint"
".ppm"="application/x-ppm"
".pps"="application/vnd.ms-powerpoint"
".ppt"="application/vnd.ms-powerpoint"
".ppt"="application/x-ppt"
".pr"="application/x-pr"
".prf"="application/pics-rules"
".prn"="application/x-prn"
".prt"="application/x-prt"
".ps"="application/x-ps"
".ps"="application/postscript"
".ptn"="application/x-ptn"
".pwz"="application/vnd.ms-powerpoint"
".r3t"="text/vnd.rn-realtext3d"
".ra"="audio/vnd.rn-realaudio"
".ram"="audio/x-pn-realaudio"
".ras"="application/x-ras"
".rat"="application/rat-file"
".rdf"="text/xml"
".rec"="application/vnd.rn-recording"
".red"="application/x-red"
".rgb"="application/x-rgb"
".rjs"="application/vnd.rn-realsystem-rjs"
".rjt"="application/vnd.rn-realsystem-rjt"
".rlc"="application/x-rlc"
".rle"="application/x-rle"
".rm"="application/vnd.rn-realmedia"
".rmf"="application/vnd.adobe.rmf"
".rmi"="audio/mid"
".rmj"="application/vnd.rn-realsystem-rmj"
".rmm"="audio/x-pn-realaudio"
".rmp"="application/vnd.rn-rn_music_package"
".rms"="application/vnd.rn-realmedia-secure"
".rmvb"="application/vnd.rn-realmedia-vbr"
".rmx"="application/vnd.rn-realsystem-rmx"
".rnx"="application/vnd.rn-realplayer"
".rp"="image/vnd.rn-realpix"
".rpm"="audio/x-pn-realaudio-plugin"
".rsml"="application/vnd.rn-rsml"
".rt"="text/vnd.rn-realtext"
".rtf"="application/msword"
".rtf"="application/x-rtf"
".rv"="video/vnd.rn-realvideo"
".sam"="application/x-sam"
".sat"="application/x-sat"
".sdp"="application/sdp"
".sdw"="application/x-sdw"
".sit"="application/x-stuffit"
".slb"="application/x-slb"
".sld"="application/x-sld"
".slk"="drawing/x-slk"
".smi"="application/smil"
".smil"="application/smil"
".smk"="application/x-smk"
".snd"="audio/basic"
".sol"="text/plain"
".sor"="text/plain"
".spc"="application/x-pkcs7-certificates"
".spl"="application/futuresplash"
".spp"="text/xml"
".ssm"="application/streamingmedia"
".sst"="application/vnd.ms-pki.certstore"
".stl"="application/vnd.ms-pki.stl"
".stm"="text/html"
".sty"="application/x-sty"
".svg"="text/xml"
".swf"="application/x-shockwave-flash"
".tdf"="application/x-tdf"
".tg4"="application/x-tg4"
".tga"="application/x-tga"
".tif"="image/tiff"
".tif"="application/x-tif"
".tiff"="image/tiff"
".tld"="text/xml"
".top"="drawing/x-top"
".torrent"="application/x-bittorrent"
".tsd"="text/xml"
".txt"="text/plain"
".uin"="application/x-icq"
".uls"="text/iuls"
".vcf"="text/x-vcard"
".vda"="application/x-vda"
".vdx"="application/vnd.visio"
".vml"="text/xml"
".vpg"="application/x-vpeg005"
".vsd"="application/vnd.visio"
".vsd"="application/x-vsd"
".vss"="application/vnd.visio"
".vst"="application/vnd.visio"
".vst"="application/x-vst"
".vsw"="application/vnd.visio"
".vsx"="application/vnd.visio"
".vtx"="application/vnd.visio"
".vxml"="text/xml"
".wav"="audio/wav"
".wax"="audio/x-ms-wax"
".wb1"="application/x-wb1"
".wb2"="application/x-wb2"
".wb3"="application/x-wb3"
".wbmp"="image/vnd.wap.wbmp"
".wiz"="application/msword"
".wk3"="application/x-wk3"
".wk4"="application/x-wk4"
".wkq"="application/x-wkq"
".wks"="application/x-wks"
".wm"="video/x-ms-wm"
".wma"="audio/x-ms-wma"
".wmd"="application/x-ms-wmd"
".wmf"="application/x-wmf"
".wml"="text/vnd.wap.wml"
".wmv"="video/x-ms-wmv"
".wmx"="video/x-ms-wmx"
".wmz"="application/x-ms-wmz"
".wp6"="application/x-wp6"
".wpd"="application/x-wpd"
".wpg"="application/x-wpg"
".wpl"="application/vnd.ms-wpl"
".wq1"="application/x-wq1"
".wr1"="application/x-wr1"
".wri"="application/x-wri"
".wrk"="application/x-wrk"
".ws"="application/x-ws"
".ws2"="application/x-ws"
".wsc"="text/scriptlet"
".wsdl"="text/xml"
".wvx"="video/x-ms-wvx"
".xdp"="application/vnd.adobe.xdp"
".xdr"="text/xml"
".xfd"="application/vnd.adobe.xfd"
".xfdf"="application/vnd.adobe.xfdf"
".xhtml"="text/html"
".xls"="application/vnd.ms-excel"
".xls"="application/x-xls"
".xlw"="application/x-xlw"
".xml"="text/xml"
".xpl"="audio/scpls"
".xq"="text/xml"
".xql"="text/xml"
".xquery"="text/xml"
".xsd"="text/xml"
".xsl"="text/xml"
".xslt"="text/xml"
".xwd"="application/x-xwd"
".x_b"="application/x-x_b"
".x_t"="application/x-x_t
public class UploadImgAction extends BaseUploadAction { /** * 文件上传 */ public String execute() { Print.println("进入execute方法"); //重命名该图片 String old_name=getUploadFileName(); String file_name=DateUtil.dateToDateString(new Date(),"yyyyMMddHHmmssSSS")+old_name.substring(old_name.indexOf(".")); //设置保存文件位置 String savePath = super.getSavePath(); if ("1".equals(getNum())) { savePath = getSavePath1(); }else if ("2".equals(getNum())) { savePath = getSavePath2(); }else if ("3".equals(getNum())) { savePath = getSavePath3(); } String saveFile=FindProjectPath.getRootPath(savePath+"\\"+old_name); //文件类型限制 String allowedTypes = super.getAllowedTypes(); if ("1".equals(getNum())) { allowedTypes = getAllowedTypes1(); }else if ("2".equals(getNum())) { allowedTypes = getAllowedTypes2(); } else if ("3".equals(getNum())) { allowedTypes = getAllowedTypes3(); } //上传文件 String errorString=UploadFile.upload(getUpload(), saveFile, getUploadContentType(), getUpload().length(), allowedTypes, getMaximunSize()); //判断上传结果 if(!"".equals(errorString)) { Print.println(errorString); Param.setAttribute("tip", "no"); Param.setAttribute("errorString", errorString); return INPUT; } Param.setAttribute("tip", "ok"); Param.setAttribute("filename",file_name); Param.setAttribute("filenameGBK",old_name); Param.setAttribute("filelength",Math.round(getUpload().length()/1024.0)); return SUCCESS; } }
相关推荐
本篇文章将详细探讨如何在Struts2框架下实现文件的上传与下载。 首先,我们需要了解Struts2中的文件上传机制。Struts2提供了`FileUploadInterceptor`拦截器来处理文件上传请求。在处理文件上传时,开发者需要在...
在Struts2的动作类(Action)中,你需要创建一个List类型的属性来接收这些文件。例如: ```java private List<File> files; private List<String> fileNames; private List<String> fileTypes; // getters and ...
1. **配置Action**: 在Struts2的配置文件(struts.xml)中,我们需要为文件上传的Action添加特殊的配置。例如,启用`params`拦截器,确保文件参数能够正确解析: ```xml <package name="default" namespace="/" ...
在Struts2中实现文件上传并展示进度条是一项常见的需求,它能提升用户体验,尤其是在处理大文件时。以下将详细解释如何在Struts2中实现这一功能。 首先,我们需要在Struts2配置中开启文件上传的支持。在`struts.xml...
在Struts2中,你可能需要创建一个Action类来处理文件上传的逻辑。Action类通常会有`@RequestParam`注解的方法,如`@RequestParam("file") File file`,用于接收上传的文件。同时,你也需要一个` ...
1. **配置Struts2 Action**:在Struts2中,我们需要创建一个Action类来处理文件上传请求。这个类通常需要实现`ServletRequestAware`接口,以便可以直接访问HttpServletRequest对象,从中获取上传文件。 2. **添加...
`<input type="file" name="files[]" multiple>`允许用户选择多个文件,`multiple`属性表示可以上传多个文件。 3. **Action类处理**:在Struts2的Action类中,我们需要定义一个`List<File>`或`List<Part>`类型的...
首先,你需要在Struts2配置文件(通常为struts.xml)中启用文件上传支持,这通常意味着添加`<constant name="struts.multipart.parser" value="jakarta"/>`配置,以确保框架能够解析multipart/form-data类型的请求,...
在Struts2的Action类中,我们需要创建一个字段来接收上传的文件,并配置相应的注解以进行处理。 1. **配置Struts2的配置文件**: 在struts.xml或struts.properties中,需要启用`struts.multipart.parser`属性,...
Struts 2 的 fileUpload 拦截器则负责将这些文件绑定到 Action 对象的属性上,使得开发者可以像操作本地文件一样对上传文件进行处理。 在 Struts 2 中实现文件上传,首先需要在 JSP 页面创建一个支持多部分数据的...
Struts2提供了完善的文件上传支持,让我们来详细探讨如何在Struts2中实现多文件上传。 首先,我们需要在Struts2的配置文件(struts.xml)中启用文件上传的支持。这通常涉及到添加`<constant>`标签来设置`struts....
在这个“Struts2实现文件上传”的主题中,我们将深入探讨如何利用Struts2框架来实现在Web应用中的文件上传功能。 首先,我们注意到一个细节描述:“private String uploadContextType;应更正为private String ...
在Struts2中,实现文件上传功能是一项常见的需求,它允许用户通过Web界面上传文件到服务器。以下是对该主题的详细解释: 1. **Struts2文件上传原理** Struts2使用Apache的Commons FileUpload库来处理文件上传。这...
在Struts2中,文件上传是一个常见的需求,可以帮助用户从客户端上传文件到服务器。本文将详细讲解Struts2框架下三种不同的文件上传方式:copy模式、字节流上传和字符流上传。 1. Copy模式文件上传: Copy模式是...
文件上传在现代Web应用中十分常见,如用户头像、文档分享等场景。Struts2提供了两种主要的文件上传方式:单文件上传和多文件上传。下面将详细介绍这两种方法及其相关知识点。 一、单文件上传 1. **配置Struts2核心...
在Struts2中,文件上传是常见的功能之一,特别是批量上传,可以处理用户一次性上传多个文件的需求。下面将详细介绍Struts2实现文件批量上传的相关知识点。 1. **Struts2的ActionContext与FileUpload插件** Struts2...
2. **struts.xml配置**:在Struts2的配置文件中,我们需要为文件上传操作指定一个`<action>`元素,并配置`<interceptor-ref>`以包含`params`和`fileUpload`拦截器。这两个拦截器负责解析`multipart/form-data`请求并...
在本篇中,我们将深入探讨Struts2实现文件上传的原理、步骤以及需要注意的关键点。 首先,理解文件上传的基本流程: 1. 用户在表单中选择文件,并提交表单。 2. Struts2 框架接收到请求,解析请求体中的文件数据。...
在Struts2中,文件上传功能是常见的需求,比如用户可能需要上传个人照片、文档或者其他类型的文件。在这个"Struts2之struts2文件上传详解案例struts011"中,我们将深入探讨如何实现这一功能。 首先,我们需要了解...
在Struts2中,文件上传是常见的功能之一,尤其在处理用户提交的表单数据时,如上传图片、文档等。在本项目中,"struts2多文件的上传"实现了用户一次性上传多个文件的能力。 要理解这个功能,首先我们需要了解Struts...