




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Struts2 0Struts2 0 文件上传与下载全解析文件上传与下载全解析 struts 的上传封装的已经非常完美了 首先我们来看一下页面 上传档案 主要关注的就是 enctype multipart form data 在 action 中 我们来看 private String documentFileContentType private String documentFileFileName private File documentFile public String getDocumentFileContentType return documentFileContentType public void setDocumentFileContentType String documentFileContentType this documentFileContentType documentFileContentType public String getDocumentFileFileName return documentFileFileName public void setDocumentFileFileName String documentFileFileName this documentFileFileName documentFileFileName public File getDocumentFile return documentFile public void setDocumentFile File documentFile this documentFile documentFile private void copy File src File dst InputStream in null OutputStream out null try in new BufferedInputStream new FileInputStream src out new BufferedOutputStream new FileOutputStream dst byte buffer new byte 1024 while in read buffer 0 out write buffer in close out close catch Exception e e printStackTrace public String save if documentFileFileName equals String folder ServletActionContext getServletContext getRealPath archives File rootDir new File folder if rootDir exists rootDir mkdirs String fileEx documentFileFileName substring documentFileFileName indexOf documentFileFileName length String fileRealName documentFileFileName substring 0 documentF ileFileName indexOf String valueOf new Date getTime fileEx String fileName folder fileRealName copy documentFile new File fileName return success documentFileContentType documentFileFileName documentFile 上传后这三个东 西会自动注入进来 根据要求对文件名更改下 保存下 好了 接着我们要提供下载 看看 struts 是怎么做的 网上关于这方面资料很少 就一个 家伙把官方的 showcase 翻译下 我再完整的走一遍流程 在页面中 archives 下载 在 action 中 import java io InputStream import java io UnsupportedEncodingException import org apache struts2 ServletActionContext import com opensymphony xwork2 Action import com opensymphony xwork2 ActionContext public class FileDownloadAction implements Action private String inputPath public void setInputPath String value throws UnsupportedEncodingExcept ion inputPath new String value getBytes ISO 8859 1 UTF 8 System out println public InputStream getInputStream throws Exception return ServletActionContext getServletContext getResourceAsStream i nputPath public String execute throws Exception String fileName inputPath substring inputPath lastIndexOf 1 i nputPath length ServletActionContext getResponse setHeader Content Disposition attachment filename new String fileName getBytes gb2312 i so 8859 1 return SUCCESS 相应的 XML 配置 inputStream 4096 这里要注意 在 action 中 inputPath new String value getBytes ISO 8859 1 UTF 8 需要转换下 另外在 setHeader Content Disposition attachment filename new String fileName getBytes gb2312 i so 8859 1 这一步也是非常重要的 注意 第一个转换 ISO 8859 1 UTF 8 UTF 8 是根据你自己的编码来处理 第二个转换 gb2312 iso 8859 1 你就不要改变了 不管你是什么编码 都 这么处理就是了 只要你的客户用的是中文的操作系统 呵呵 大家在官方例子 showcase 里看到的是这样的 images struts gif image gif inputStream filename struts gif 4096 可以看到 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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 彭阳消防考试题库及答案
- 2025年贵州公务员考试行测真题及答案
- 2025年广西壮族自治区中央遴选真题及参考答案(b类)
- 淮安清中开学考试卷及答案
- 母婴护理师考试试卷题库及答案
- 信息技术考试真题分类及答案
- 医学生化考试试题及答案
- 广东春季高考考试卷子及答案
- 九江编制考试题库及答案
- 2025年医疗器械法规与管理考试试题及答案
- 2025-2030年中国反无人机行业市场深度调研及前景趋势与投资研究报告
- 如何提升科室医疗安全
- 2025年医保知识考试题库及答案:基础政策解读与医保报销比例调整试题
- 《中医基础理论之阴阳五行:中医学专业教案》
- 中级货币鉴定师资格考试题(附答案)
- DB32T 5018-2025专业戒毒大队建设与工作规范
- 基于用户行为的精准推送策略
- 2025年时事政治考试116题及参考答案
- 2025年全国职业院校技能大赛导游业务知识竞赛题库及答案(共60题)
- 老人骨折术前护理
- 压裂返排液的深度处理及再利用技术研究进展
评论
0/150
提交评论