Chinaunix首页 | 论坛 | 博客
  • 博客访问: 794586
  • 博文数量: 738
  • 博客积分: 7000
  • 博客等级: 少将
  • 技术积分: 5000
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-12 09:00
文章分类

全部博文(738)

文章存档

2011年(1)

2008年(737)

我的朋友

分类:

2008-09-12 09:06:53

    页面一开始进去action,action负责把file文件夹下的所有文件读入一个ArrayList中
Action代码如下:
ArrayList list = new ArrayList();
     String path=request.getRealPath("/")+"file";
     String FullPath;
       //System.out.println(path);
     myDir=new File(path);
     list.clear();
     contents=myDir.listFiles();
     for(int i=0;i      FullPath=contents[i].getName();
      list.add(FullPath);
      //System.out.println(FullPath);
     }
 request.setAttribute("list",list);
        ActionForward forward=new ActionForward("/download.jsp"); 
        return forward;
然后进入download.jsp中,这个页面主要负责把所有文件显示,并提供连接,代码如下:
<%@ page language="java" contentType="text/html;charset=GBK" import="java.util.ArrayList"%>





<%ArrayList list=(ArrayList)request.getAttribute("list");
  for(int i=0;i  {
  String a=java.net.URLEncoder.encode((String)list.get(i));

   out.print(""+list.get(i)+"
");
  }
  %>


注意,下划线画中的代码的作用,就是解决问题的所在。
接下来可以直接传入到loadedaction中,也可以通过一个form,我演示的是通过一个form
Form代码如下
package org.aeolus.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LoadForm extends ActionForm {
    /*
     * Generated Methods
     */
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
接下来就是action的代码
  LoadForm doc=(LoadForm)form;
String docName = new String(doc.getName().getBytes("8859_1"));
          File f;
          if(docName!=""){
          String docFullPath=request.getRealPath("/"); 
              f = new File(docFullPath+"file\\"+docName);
             response.reset();        
             response.setContentType("application/x-msdownload;charset=GBK"); 
             System.out.print(response.getContentType());
             response.setCharacterEncoding("UTF-8");
            docName=java.net.URLEncoder.encode(docName,"UTF-8");
             response.setHeader("Content-Disposition", "attachment; filename=" +new String(docName.getBytes("UTF-8"),"GBK")); 
             BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
             byte[] buf = new byte[1024];
             int len = 0;
              OutputStream out = response.getOutputStream();
              while((len = br.read(buf)) >0)
              out.write(buf,0,len);
              out.close();
              response.wait();
              ActionForward forward=new ActionForward("/download.jsp"); 
              
              return forward;
          }
          return null;  
注意,下划线画中的代码的作用,就是解决问题的所在。
说明一下
response.setCharacterEncoding("UTF-8");
            docName=java.net.URLEncoder.encode(docName,"UTF-8");
             response.setHeader("Content-Disposition", "attachment; filename=" +new String(docName.getBytes("UTF-8"),"GBK")); 
如果不这样做你将要的文件名是乱码。
说明就到这里,如果有问题或要eclipse下的工程代码可以留下你的地址,我会尽可能的给你发邮件。
我的邮件aeoluswqq@163.com
【责编:Peng】

--------------------next---------------------

阅读(350) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~