STRUTS2多文件上传以及下载
2011-09-07 18:45
585 查看
上传文件先要导入两个包
![](http://hi.csdn.net/attachment/201109/7/0_1315391670hgG7.gif)
在JSP页面当中切记FORM后面的enctype="multipart/form-data"一定要写上
![](http://hi.csdn.net/attachment/201109/7/0_1315391933J8q5.gif)
在Action当中分别写入
private File[] imgpath;// 实际的上传文件
private String[] imgpathFileName; // 上传文件名
private String[] imgpathContentType; // 上传文件内容类型
private ServletContext context;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
在封装GET和SET方法省略
// 上传的文件名修改成部门+当前时间
private String getfileName(String fileFileName) {
HttpServletRequest request = ServletActionContext.getRequest();
int position = fileFileName.lastIndexOf(".");
String extension = fileFileName.substring(position);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String dept = (String) request.getSession().getAttribute("userdept");
String curDate = sdf.format(new Date());
extension = retuDeptId(dept) + curDate+(int)(Math.random()*100+1) + extension;
return extension;
}
if (imgpath!=null) {
String targetDirectory = context
.getRealPath(this.getSavePath());
for (int i = 0; i < imgpath.length; i++) {
String str=getfileName(imgpathFileName[i]);
if(i<imgpathFileName.length-1){
System.out.println("=========="+i+"次数"+getfileName(imgpathFileName[i]));
targetFileName += str + ",";
}else{
targetFileName+=str;
}
File target = new File(targetDirectory,
str);
FileCopyUtils.copy(imgpath[i], target);
}
}
下载文件因为我的文件名称是保存在数据当中这时候我拿出来添加的LIST并且做了一些处理当中然后在页面中遍历
String temp = adversariatable.getAdveAccessories();// 文件名称
String[] str = temp.split(",");
strList=new ArrayList();
for (int i = 0; i < str.length; i++) {
strList.add(str[i]);
}
下载需要在STRUTS.XML文件当中配置一下存入文件的入境,存入的入境就是下载的入境,你的ACTION在那里就在那个ACTION下面配置
savePath就是下载的入境以及下载的入境
![](http://hi.csdn.net/attachment/201109/7/0_1315392120M6VA.gif)
下面就是在页面中下载文件的时候
![](http://hi.csdn.net/attachment/201109/7/0_1315391670hgG7.gif)
在JSP页面当中切记FORM后面的enctype="multipart/form-data"一定要写上
![](http://hi.csdn.net/attachment/201109/7/0_1315391933J8q5.gif)
在Action当中分别写入
private File[] imgpath;// 实际的上传文件
private String[] imgpathFileName; // 上传文件名
private String[] imgpathContentType; // 上传文件内容类型
private ServletContext context;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
在封装GET和SET方法省略
// 上传的文件名修改成部门+当前时间
private String getfileName(String fileFileName) {
HttpServletRequest request = ServletActionContext.getRequest();
int position = fileFileName.lastIndexOf(".");
String extension = fileFileName.substring(position);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String dept = (String) request.getSession().getAttribute("userdept");
String curDate = sdf.format(new Date());
extension = retuDeptId(dept) + curDate+(int)(Math.random()*100+1) + extension;
return extension;
}
if (imgpath!=null) {
String targetDirectory = context
.getRealPath(this.getSavePath());
for (int i = 0; i < imgpath.length; i++) {
String str=getfileName(imgpathFileName[i]);
if(i<imgpathFileName.length-1){
System.out.println("=========="+i+"次数"+getfileName(imgpathFileName[i]));
targetFileName += str + ",";
}else{
targetFileName+=str;
}
File target = new File(targetDirectory,
str);
FileCopyUtils.copy(imgpath[i], target);
}
}
下载文件因为我的文件名称是保存在数据当中这时候我拿出来添加的LIST并且做了一些处理当中然后在页面中遍历
String temp = adversariatable.getAdveAccessories();// 文件名称
String[] str = temp.split(",");
strList=new ArrayList();
for (int i = 0; i < str.length; i++) {
strList.add(str[i]);
}
下载需要在STRUTS.XML文件当中配置一下存入文件的入境,存入的入境就是下载的入境,你的ACTION在那里就在那个ACTION下面配置
savePath就是下载的入境以及下载的入境
![](http://hi.csdn.net/attachment/201109/7/0_1315392120M6VA.gif)
下面就是在页面中下载文件的时候
![](http://hi.csdn.net/attachment/201109/7/0_13153922149SR8.gif)
相关文章推荐
- J2EE系列之Struts2学习笔记(二十二)--Struts2多文件上传和文件下载
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- struts1.3.8多文件上传以及文件下载源码
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- Struts2————学习笔记6 文件的上传与下载 以及AJAX
- struts文件的上传与下载
- [置顶] struts文件上传与下载
- struts 实现文件上传与下载
- Struts2多文件上传
- Apache、struts1、struts2文件上传下载的三种方式
- struts 学习总结 ---- 文件上传与下载
- Android 关于带有中文字样以及空格文件名的文件上传及下载
- Struts 1.3中实现文件的上传与下载
- struts2多文件动态下载及中文解决方案
- struts2.3.15之文件上传与下载
- Struts2.x上传和下载文件
- struts文件上传以及中文乱码问题
- 文件的上传和下载以及如何防止网站被入侵(web开发中很有用的知识)