Struts2.0文件上传与下载_第1页
Struts2.0文件上传与下载_第2页
Struts2.0文件上传与下载_第3页
Struts2.0文件上传与下载_第4页
Struts2.0文件上传与下载_第5页
免费预览已结束,剩余3页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、文件上传与下载全解析struts的上传封装的已经非常完美了,首先我们来看一下页面<s:form action ="" method= "p ost" enctype="mult ip art/form-data"><td height ="32" class="heder">上传档案</ td><td alig n ="left" bgcolor ="#FFFFFF" class ="mai n2&quo

2、t;><s:filen ame="docume ntFile" /></ td><td alig n ="cen ter">vinput type ="submit" value ="保 存" class ="butt on"on click ="retur n n extsubmit();"/></ td></s:form >主要关注的就是enctype="mult ip art/form-d

3、ata"<s:filen ame="docume ntFile"/>在action 中,我们来看privateStri ngdocume ntFileC ontentType;privateStri ngdocume ntFileFileName;privateFiledocume ntFile;publicStri nggetDocume ntFileC ontentTyp e()returndocume ntFileC ontentType;docume ntFileC ontentTypp ublicvoidsetDocume ntFileC o

4、ntentTyp e(Stri nge) docume ntFileC ontentType;src);p ublicStri nggetDocume ntFileFileName()returndocume ntFileFileName;p ublicvoidsetDocume ntFileFileName(Stri ng=docume ntFileFileName;p ublicFilegetDocume ntFile()retur ndocume ntFile;p ublicdocume ntFileFileName)void setDocume ntFile(File=docume n

5、tFile;docume ntFile) p rivatevoidcopy( Filesrc, File dst) InputStream in =null ;Out putStreamouttry innull ;new Bufferedl np utStream(new FileInputStream(outnew BufferedOutputStream(new FileOutputStream(dst);byte whilebuffer = new byte 1024;(buffer) >0)(buffer);();(); catch(Exce ptio ne) ();p ubl

6、ic String save()if (!("")archives");String folder().getReal Path("/new File(folder);File rootDirStri ng fileEx();Stri ng fileRealName(0,(".")+ (new Date().getTime()+fileEx;Stri ng fileNamefolder +""+ fileRealName;copy(documentFile,new File(fileName);retur n&qu

7、ot;success"docume ntFile;上传后这三docume ntFileC ontentType;docume ntFileFileName;个东西会自动注入进来,根据要求对文件名更改下,保存下好了,接着我们要提供下载,看看struts是怎么做的,网上关于这方面资料很少,就一个家伙把官方的showcase翻译下,我再完整的走一遍流程在页面中<s:url id ="url" action ="dow nl oad"><s:paramname="inputPath"> /archives/

8、<s:property value ="loc" /></ s:param ></s:url > <s:a href ="%url"> 下载 </s:a>在action 中imp ortimp ort imp ort imp ortimp ortinputP athnew String("ISO-8859-1"),"UTF-8");p ublicclassFileDow nl oadActi onimp leme ntsActio n ().setHead

9、er("C onten t-Dis po siti on","attachme nt;file name="+ np ublicInpu tStreamgetI npu tStream()throws Exce pti on retur n().getResourceAsStream(i nputP ath);p ublicString execute() throwsExcep ti on("/")+1, ();Stri ngfileNameew String("gb2312"),"iso-8859-1

10、");retur nSUCCESS;可以看到inputPath我们已经写在了 jsp的URL中了,contentType 这个东西也是大家比相应的XML配置<act ionn ame="dow nl oad"class ="FileDow nl oadAct ion">vresultn ame="success"type ="stream"><param name="inputName"> inputStream </ param><p

11、aram n ame="bufferSize">4096</ p aram></ result ></ action >这里要注意,在action 中inputPath要转换下new String("ISO-8859-1"),"UTF-8");另外在"attachme nt;setHeader("C onten t-Dis po siti on", file name="+ new Strin g("gb2312"),"is

12、o-8859-1");这一步也是非常重要的。注意:第一个转换,"ISO-8859-1""UTF-8"UTF-8是根据你自己的编码来处理"iso-8859-1"你就不要改变了,不管你是什么编码,都第二个转换,"gb2312"这么处理就是了,只要你的客户用的是中文的操作系统,呵呵大家在官方例子showcase里看到的是这样的<act ionn ame="dow nl oad"class =""><param name="inputPath&q

13、uot;> /images/ </param>vresultn ame="success"type ="stream">p aram><p aram<p aram<p aramname="contentType"> image/gif </ param>name="inputName"> inputStream </ param>n ame="c onten tDis po siti on">file name="" </</ result ></ act

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论