现在的位置: 首页 > 综合 > 正文

STRUTS2多文件上传以及下载

2013年08月11日 ⁄ 综合 ⁄ 共 1596字 ⁄ 字号 评论关闭

    上传文件先要导入两个包

 

  在JSP页面当中切记FORM后面的enctype="multipart/form-data"一定要写上

 

 

  在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就是下载的入境以及下载的入境

      下面就是在页面中下载文件的时候

抱歉!评论已关闭.