如何将服务器端指定的文件(d:\A\lwl.xls)下载到客户端(弹出对话框选择本地欲存放的路径以及文件名)?
谢谢!

解决方案 »

  1.   

    如果不是特殊的文件,完全可以通过javascript来实现:document.location="/root/x.xls",但是为了避免文件在浏览器直接打开(或者是处理不在服务器目录下的文件),您可能就绪要写一个专门的servlet来处理了,示例程序如下:import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.lang.reflect.*;
    import java.sql.*;/**
     * 类描述:${describe} 生成时间: 2005-10-26  16:46:25
     *
     * @author user
     * @version Revision
     */public class FileViewServlet extends HttpServlet { private String dsName = "OpenojSessionFactory"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String id = request.getParameter("id");
    String vo = request.getParameter("vo");
    String pkg = request.getParameter("pkg");
    String colum = request.getParameter("colum");
    String type = request.getParameter("download"); if (vo == null || id == null || colum == null)
    throw new ServletException("参数[vo,id,colum]必须全部指定,pkg默认为[com.gsbiz.vo],下载download默认为[true]~!");
    if (pkg == null || pkg.length() == 0)
    pkg = "com.gsbiz.vo";
    if (type == null || type.length() == 0)
    type = "true"; String clasz = pkg + "." + vo; try {
    Class voclass = Class.forName(clasz);
    Datastore ds = PersistenceFactory.create().getDatastore(dsName);
    Object rvo = ds.findByPK(voclass, id); String methodName = "get" + colum.substring(0, 1).toUpperCase() + colum.substring(1);
    Method method = voclass.getMethod(methodName, null);
    Object file = method.invoke(rvo, null); if (file instanceof Blob || file instanceof Clob) { InputStream in = null;
    if (file instanceof Blob) {
    Blob blob = (Blob) file;
    in = blob.getBinaryStream();
    }
    else if (file instanceof Clob) {
    Clob clob = (Clob) file;
    in = clob.getAsciiStream();
    } //设置页面不缓存
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0); if (type.equalsIgnoreCase("true")) {
    response.setContentType("application/x-download");
    String methodName1 = "get" + colum.substring(0, 1).toUpperCase() + colum.substring(1) + "FileName";
    Method method1 = voclass.getMethod(methodName1, null);
    Object fileName = method1.invoke(rvo, null);
    response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
    }
    else {
    response.setContentType("image/gif"); } ServletOutputStream outputStream = response.getOutputStream(); byte[] data = new byte[2048];
    int size;
    while ((size = in.read(data)) != -1) {
    outputStream.write(data, 0, size);
    }
    in.close();
    outputStream.flush();
    outputStream.close();
    }
    }
    catch (Exception e) {
    e.printStackTrace();
    } }
    }
      

  2.   

    真是不好意思,我还不会用servlet。
    我用jspsmart组建暂时实现了。
    先收藏!
    非常感谢(踏月无痕)!