Java_文件上传与下载(三)
2016-08-11 19:15
387 查看
Struts2实现
1.原理
a.表单中设置为post提交,encytype属性设置为multipart/form-dat,这样数据就以二进制流的方式传输 b.Struts2默认采用Jakarta和Common-FileUpload框架,导入相应jar包即可 c.通过Struts2的拦截器设置上传文件的类型及大小等 d.在Struts2的action中设置上传文件的内容、类型及名称
2.文件上传
(1)Action的写法:public class FileUploadAction extends ActionSupport { private File upload; //这里必须与表单中的input的Name一致 private String uploadContentType; private String uploadFileName; private String result; //省略set/get方法 。。。。。。 。。。。。。 //Struts2的action中excute方法的覆写 @Override public String execute() throws Exception { String filePath = ServletActionContext.getServletContext().getRealPath("/images"); File file = new File(filePath); if(!file.exists()){ file.mkdir(); } FileUtils.copyFile(upload, new File(file,uploadFileName)); result = "上传成功!"; return SUCCESS; } }
(2)Struts2的xml配置:*
<package name="default" namespace="/" extends="struts-default"> <action name="upload" class="com.mystudy.action.FileUploadAction"> <result>/jsps/struts2upload.jsp</result> <result name="input">/jsps/error.jsp</result> <!-- 拦截器配置 --> <interceptor-ref name="fileUpload"> <!-- 允许上传的文件类型 --> <param name="allowedTypes">image/jpeg,image/gif,image/png</param> <!-- 允许上传的最大文件 --> <param name="maximumSize">10M</param> </interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </action> </package>
(3)异常处理:
引入struts2的国际化的支持,在xml中添加 <constant name="struts.custom.i18n.resources" value="resource"/> 资源文件:假设定义了resouce_zh_CN.properties的文件
(4)批量上传:
a.jsp中多加几个input 上传文件1:<input type="file" name="upload"/><br> 上传文件2:<input type="file" name="upload"/><br> 上传文件3:<input type="file" name="upload"/><br> b.Action中的成员定义为List private List<File> upload; private List<String> uploadContentType; private List<String> uploadFileName; c.保存文件时循环逐个保存
(2)文件下载
(1)Struts2的xml配置:<!-- 文件下载的action配置 --> <action name="download" class="com.mystudy.action.FileDownloadAction"> <param name="inputPath">/images/image2.jpg</param> <result name="success" type="stream"> <param name="contentType">application/octet-stream</param> <param name="inputName">inputStream</param> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <param name="bufferSize">8192</param> </result> </action>
(2)Action的写法:
private String inputPath; public String filename; public String getInputPath() { return inputPath; } public void setInputPath(String inputPath) { this.inputPath = inputPath; } @Override public String execute() throws Exception { return SUCCESS; } public InputStream getInputStream() throws IOException{ String path = ServletActionContext.getServletContext().getRealPath("/images"); String filePath = path+"\\"+ filename; File file = new File(filePath); return FileUtils.openInputStream(file); //return ServletActionContext.getServletContext().getResourceAsStream(inputPath); } public String getDownloadFileName(){ String downloadFileName = ""; try { downloadFileName=URLEncoder.encode("下载文件.jpg","UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return downloadFileName; }
相关文章推荐
- java文件上传和下载(原创,根据RFC1867分析得到)
- 自己写的一个JSP上传文件和下载文件的JavaBean
- java上传下载文件的总结
- Java文件上传下载核心代码
- JAVA 文件上传与下载
- java文件上传下载
- jaVA使用FTP上传下载文件的问题
- java做的比较完善的FTP连接上传下载文件【转】
- java中的文件上传及下载组件介绍
- JAVA连接FTP服务器,并上传/下载文件代码
- java文件上传下载
- 文件上传下载:通过java方法把附件保存到clob字段中
- Java文件上传下载 源码
- JAVA实现FTP文件上传下载文件
- java做的比较完善的FTP连接上传下载文件
- SERVLET与JSP_文件上传下载(4)-java
- 自己写的一个JSP上传文件和下载文件的JavaBean
- java做的比较完善的FTP连接上传下载文件
- 用Java实现FTP批量大文件上传下载
- Java中如何使用组件实现文件上传下载