1、Struts2下多文件的上传与下载Struts2下多文件的上传与下载 Googel 标签: 多文件 ;上传 ;下载 ;随意文件 ;java ;Struts2 ;单例 ;配置 ;动态读取 ;李顺利Struts2下多文件的上传与下载目录关键词 1写在前面 1需求 1环境 2目录结构 3重要文件的看点 3web.xml 3struts.xml 4UploadAction 5DownloadAction 8UploadConfigurationRead 10工程结果截图 13提供原代码下载 14关键词多文件 上传 下载 随意文件 java Struts2 单例 配置 动态读取 李顺利 写在前面 在网络
2、上,对于Java处理文件上传和下载的技术比较多,而Struts作为一款优秀的框架也提供了非常方便的文件上传和下载,而网上的一些例程都非常的不全面,概括来:1) 文件上传比较多,多文件上传少一点2) 文件下载很少的,看似简单,实则不然3) 文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子最近也在研究一下文件的上传和下载,在整合网上、浪曦风中叶老师和自己的学习的思想,写了这个工程,提供给大家参考,所以的代码终在IE、FireFox、Chrome测试通过。需求1.能够对多个文件进行上传(可以选择上传文件个数,也即上传文件个数不定)2.能够对上传路径进行配置文件(uploa
3、d.properties)指定,使用了一些类似单例模式的静态代码块3.对配置文件中的路径可以进行动态读取(不重启服务器)4.Struts2进行下载处理,能对上传的所有文件进行下载(多个)5.文件保存的名称UUID生成,不过显示并下载的名称都是原文件名称(人性化,通过UploadFiles处理)环境最新的Struts2:struts-2.1.8、MyEclipse、Tomcat、IE、FireFox、Chrome目录结构重要文件的看点web.xml struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFil
4、ter 在web.xml中使用了最新的Struts2的中央处理器类org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter,而不再是以前的org.apache.struts2.dispatcher.FilterDispatcher,FilterDispatcher在新版本Struts2中已经标注为过时了,请大家尽量使用最新的filter。struts.xml /upload.jsp /upload.jsp!- /upload_success.jsp- /download.jsp !- 2097152单位是字节 2
5、M (2097152B) -!- image/bmp,image/x-png,image/png,image/gif,image/jpeg,image/jpg,image/pjpeg- attachment;filename=$fileName downloadFile 就是文件上传和下载的一些Struts2配置,注意上传的时候,请引入,下载的配置中注意:downloadFile其他的配置解释网上很多,不懂的可以先google学习一下。UploadAction在此文件中请注意文件数组的使用,因为是多文件package org.usc.file;import java.io.File;impor
6、t java.util.ArrayList;import java.util.List;import java.util.UUID;import mons.io.FileUtils;import org.apache.struts2.ServletActionContext;import org.usc.utils.UploadConfigurationRead;import com.opensymphony.xwork2.ActionSupport;/* * 多文件上传类 * * author MZ * * Time 2009-11-24下午09:26:44 */public class U
7、ploadAction extends ActionSupport /* * */ private static final long serialVersionUID = 1L; private File upload;/ 实际上传文件 private String uploadContentType; / 文件的内容类型 private String uploadFileName; / 上传文件名 private List uploadFiles = new ArrayList(); public String execute() throws Exception try String t
8、argetDirectory = ServletActionContext.getServletContext() .getRealPath(/+ UploadConfigurationRead.getInstance().getConfigItem(uploadFilePath).trim();/ 获得路径 for (int i = 0; i upload.length; i+) String fileName = uploadFileNamei;/ 上传的文件名 String type = uploadContentTypei;/ 文件类型 String realName = UUID.r
9、andomUUID().toString()+ getExt(fileName);/ 保存的文件名称,使用UUID+后缀进行保存 File target = new File(targetDirectory, realName); FileUtils.copyFile(uploadi, target);/ 上传至服务器的目录,一般都这样操作, / 在把路径写入数据库即可 UploadFiles uf = new UploadFiles();/ 创建文件 uf.setUploadContentType(type); uf.setUploadFileName(fileName); uf.setUp
10、loadRealName(realName); uploadFiles.add(uf);/ 添加到需要下载文件的List集合中 ServletActionContext.getRequest().setAttribute(uploadFiles, uploadFiles); catch (Exception e) e.printStackTrace(); addActionError(e.getMessage(); return INPUT; return SUCCESS; public File getUpload() return upload; public void setUpload
11、(File upload) this.upload = upload; public String getUploadContentType() return uploadContentType; public void setUploadContentType(String uploadContentType) this.uploadContentType = uploadContentType; public String getUploadFileName() return uploadFileName; public void setUploadFileName(String uplo
12、adFileName) this.uploadFileName = uploadFileName; public static String getExt(String fileName) return fileName.substring(fileName.lastIndexOf(.); DownloadAction在此文件中要注意public InputStream getDownloadFile()的名称在Struts2配置文件配置,返回的是一个InputStream类型的对象。package org.usc.file;import java.io.InputStream;import
13、java.io.UnsupportedEncodingException;import com.opensymphony.xwork2.ActionSupport;import org.apache.struts2.ServletActionContext;import org.usc.utils.UploadConfigurationRead;public class DownloadAction extends ActionSupport private static final long serialVersionUID = 6329383258366253255L; private S
14、tring fileName; private String fileRealName; public void setFileName() / 得到请求下载的文件名 String fname = ServletActionContext.getRequest().getParameter(name); String frealname = ServletActionContext.getRequest().getParameter(realname); try /* * 对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码,本机为GBK。 * 这里使用reques
15、t.setCharacterEncoding解码无效. * 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件 */ fname = new String(fname.getBytes(ISO-8859-1), UTF-8); frealname= new String(frealname.getBytes(ISO-8859-1), UTF-8); catch (Exception e) e.printStackTrace(); this.fileName = fname; this.fileRealName = frealname;/ System.out.p
16、rintln(fileName);/ System.out.println(fileRealName); /* * getFileName 此方法对应的是struts.xml文件中的: attachment;filename=$fileName * 这个属性设置的是下载工具下载文件时显示的文件名, 要想正确的显示中文文件名,我们需要对fileName再次编码 * 否则中文名文件将出现乱码,或无法下载的情况 */ public String getFileName() throws UnsupportedEncodingException fileRealName = new String(fi
17、leRealName.getBytes(), ISO-8859-1); return fileRealName; /* * getDownloadFile 此方法对应的是struts.xml文件中的: downloadFile 返回下载文件的流,可以参看struts2的源码 */ public InputStream getDownloadFile() this.setFileName(); return ServletActionContext.getServletContext().getResourceAsStream(/+UploadConfigurationRead.getInsta
18、nce().getConfigItem(uploadFilePath).trim()+/ + fileName); Override public String execute() throws Exception return SUCCESS; UploadConfigurationRead动态读取配置文件,借鉴网上的代码package org.usc.utils;import java.io.File;import java.io.FileInputStream;import .URI;import .URISyntaxException;import java.util.Properti
19、es;/* * 动态读取配置文件类 * * author MZ * * Time 2009-11-24下午08:25:22 */public class UploadConfigurationRead /* * 属性文件全名,需要的时候请重新配置PFILE */ private static String PFILE = upload.properties; /* * 配置文件路径 */ private URI uri = null; /* * 属性文件所对应的属性对象变量 */ private long m_lastModifiedTime = 0; /* * 对应于属性文件的文件对象变量
20、*/ private File m_file = null; /* * 属性文件所对应的属性对象变量 */ private Properties m_props = null; /* * 唯一实例 */ private static UploadConfigurationRead m_instance = new UploadConfigurationRead(); /* * 私有构造函数 * * throws URISyntaxException */ private UploadConfigurationRead() try m_lastModifiedTime = getFile().l
21、astModified(); if (m_lastModifiedTime = 0) System.err.println(PFILE + file does not exist!); m_props = new Properties(); m_props.load(new FileInputStream(getFile(); catch (URISyntaxException e) System.err.println(PFILE+文件路径不正确); e.printStackTrace(); catch (Exception e) System.err.println(PFILE+文件读取异
22、常); e.printStackTrace(); /* * 查找ClassPath路径获取文件 * * return File对象 * throws URISyntaxException */ private File getFile() throws URISyntaxException URI fileUri = this.getClass().getClassLoader().getResource(PFILE).toURI(); m_file = new File(fileUri); return m_file; /* * 静态工厂方法 * * return 返回Configurati
23、onRead的单一实例 */ public synchronized static UploadConfigurationRead getInstance() return m_instance; /* * 读取一特定的属性项 */ public String getConfigItem(String name, String defaultVal) long newTime = m_file.lastModified(); / 检查属性文件是否被修改 if (newTime = 0) / 属性文件不存在 if (m_lastModifiedTime = 0) System.err.print
24、ln(PFILE + file does not exist!); else System.err.println(PFILE + file was deleted!); return defaultVal; else if (newTime m_lastModifiedTime) m_props.clear(); try m_props.load(new FileInputStream(getFile(); catch (Exception e) System.err.println(文件重新读取异常); e.printStackTrace(); m_lastModifiedTime = newTime; String val = m_props.getProperty(name); if (val = null) return defaultVal; else return val; /* * 读取一特定的属性项 *
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1