已阅读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年高铁旅客服务满意度测评报告撰写考核试卷
- 2025年互联网行业AI技术应用与未来发展研究报告及未来发展趋势预测
- 2025年服装鞋帽行业可持续时尚发展策略探讨报告
- 环境保护的重要性与挑战-公众演讲
- 2025湖北襄高城市更新投资有限公司面向社会招聘初试笔试考试参考试题及答案解析
- 2025广东广州市荔湾区卫生健康系统事业单位招聘工作人员98人考试笔试模拟试题及答案解析
- 2025年建平县面向社会公开招聘城市社区工作者21人考试笔试备考题库及答案解析
- 2025福建厦门市集美区康城小学教师招聘2人笔试考试备考题库及答案解析
- 2025甘肃省民航机场集团招聘38人笔试考试备考题库及答案解析
- 2025年广东省税务系统遴选面试真题带答案详解
- DB52∕T 1837-2024 党政机关办公区物业管理服务规范
- 工程劳务合作管理办法
- 控制区通行证培训课件
- 水利执法实物培训课件
- 给小学生讲建筑的课件
- 2025年广西中考生物试卷真题 (含答案解析)
- 旅行社(公司)投诉档案制度
- 档案培训课件模板
- 呼吸睡眠监测判读
- 检察机关微信管理制度
评论
0/150
提交评论