我想利用jspsmart组件,写一个sevelet,以实现对文件的下载?请大家帮忙

解决方案 »

  1.   

    public class up_downloadSevelet extends HttpServlet {

    private ServletConfig config;
    final public void init(ServletConfig config) throws ServletException {
    this.config = config;
    }

    final public ServletConfig getServletConfig() {
    return config;
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

    String filename=request.getParameter("filename");//需要处理一下文件路径
    response.addHeader("Content-Disposition","attachment;filename="+filename);

    //SmartUpload su=new SmartUpload(getConfig().getServletContext(),request,response);
    SmartUpload mySmartUpload=new SmartUpload();
    try
    {
    mySmartUpload.initialize(config,request,response);
    mySmartUpload.setContentDisposition(null);
    mySmartUpload.downloadFile("d:\\"+filename);
    }
    catch(Exception e)
    {}
    }


    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    this.doGet(req, resp);
    }
    }
    这是我写的,但是不能实现文件的下载
      

  2.   

    jspSmartUpload的例子:<%@ page language="java" import="com.jspsmart.upload.*"%><jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><%

    // Initialization
    mySmartUpload.initialize(pageContext);

    // Download file
    mySmartUpload.downloadFile("/upload/sample.zip");

    // With a physical path
    // mySmartUpload.downloadFile("c:\\temp\\sample.zip")

    // With options
    // mySmartUpload.downloadFile("/upload/sample.zip","application/x-zip-compressed","downloaded.zip")%>
      

  3.   

    <jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" /><%
    mySmartUpload.initialize(pageContext);
    // 设定contentDisposition为null以禁止浏览器自动打开文件,
    //保证点击链接后是下载文件。若不设定,则下载的文件扩展名为txt时,直接在浏览器打开。
    //文件类型为doc时,浏览器将自动用word打开它。扩展名为pdf时浏览器将用acrobat打开。
    mySmartUpload.setContentDisposition(null);
    // 下载文件
    mySmartUpload.downloadFile("/upload/test.txt");%>
    servlet就不知道了