Struts2.0文件上传与下载_第1页
Struts2.0文件上传与下载_第2页
Struts2.0文件上传与下载_第3页
Struts2.0文件上传与下载_第4页
Struts2.0文件上传与下载_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

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

文档简介

Struts2.0 文件上传与下载全解析struts的上传封装的已经非常完美了,首先我们来看一下页面上传档案:主要关注的就是 enctype=multipart/form-data在action中,我们来看privateStringdocumentFileContentType;privateStringdocumentFileFileName;privateFiledocumentFile;publicStringgetDocumentFileContentType().returndocumentFileContentType;publicvoidsetDocumentFileContentType(StringdocumentFileContentType).this.documentFileContentType=documentFileContentType;publicStringgetDocumentFileFileName().returndocumentFileFileName;publicvoidsetDocumentFileFileName(StringdocumentFileFileName).this.documentFileFileName=documentFileFileName;publicFilegetDocumentFile().returndocumentFile;publicvoidsetDocumentFile(FiledocumentFile).this.documentFile=documentFile;privatevoidcopy(Filesrc,Filedst).InputStreamin=null;OutputStreamout=null;try.in=newBufferedInputStream(newFileInputStream(src);out=newBufferedOutputStream(newFileOutputStream(dst);bytebuffer=newbyte1024;while(in.read(buffer)0)out.write(buffer);in.close();out.close();catch(Exceptione).e.printStackTrace();publicStringsave().if(!documentFileFileName.equals().Stringfolder=ServletActionContext.getServletContext().getRealPath(/archives);FilerootDir=newFile(folder);if(!rootDir.exists()rootDir.mkdirs();StringfileEx=documentFileFileName.substring(documentFileFileName.indexOf(.),documentFileFileName.length();StringfileRealName=documentFileFileName.substring(0,documentFileFileName.indexOf(.)+String.valueOf(newDate().getTime()+fileEx;StringfileName=folder+fileRealName;copy(documentFile,newFile(fileName);returnsuccess;documentFileContentType; documentFileFileName; documentFile; 上传后这三个东西会自动注入进来,根据要求对文件名更改下,保存下好了,接着我们要提供下载,看看struts是怎么做的,网上关于这方面资料很少,就一个家伙把官方的showcase翻译下,我再完整的走一遍流程在页面中 /archives/下载在action中importjava.io.InputStream;importjava.io.UnsupportedEncodingException;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.Action;importcom.opensymphony.xwork2.ActionContext;publicclassFileDownloadActionimplementsAction.privateStringinputPath;publicvoidsetInputPath(Stringvalue)throwsUnsupportedEncodingException.inputPath=newString(value.getBytes(ISO-8859-1),UTF-8);System.out.println();publicInputStreamgetInputStream()throwsException.returnServletActionContext.getServletContext().getResourceAsStream(inputPath);publicStringexecute()throwsException.StringfileName=inputPath.substring(inputPath.lastIndexOf(/)+1,inputPath.length();ServletActionContext.getResponse().setHeader(Content-Disposition,attachment;filename=+newString(fileName.getBytes(gb2312),iso-8859-1);returnSUCCESS;相应的XML配置inputStream4096这里要注意,在action中 inputPath=newString(value.getBytes(ISO-8859-1),UTF-8); 需要转换下另外在setHeader(Content-Disposition,attachment;filename=+newString(fileName.getBytes(gb2312),iso-8859-1);这一步也是非常重要的。注意:第一个转换,ISO-8859-1UTF-8 UTF-8是根据你自己的编码来处理第二个转换,gb2312iso-8859-1 你就不要改变了,不管你是什么编码,都这么处理就是了,只要你的客户用的是中文的操作系统,呵呵大家在官方例子showcase里看到的是这样的/images/struts.gifimage/gifinputStreamfilename=struts.gif4096可以看到 inputPath 我们已经写在了jsp的URL中了,contentType 这个东西也是大家比较恼火的,因为对于图片、zip、rar、doc、word、txt

温馨提示

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

评论

0/150

提交评论