使用java如何实现下载

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hexu_blog/article/details/74276665

1---导入包  

       jspsmartupload.jar


2---jsp页面

       <body>
        <a href="DoDownloadServlet?filename=1.jpg">点击下载</a>
     </body>


3---servlet

     package download;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class DoDownloadServlet extends HttpServlet {
    
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //得到下载文件的名称
        String fileName=new String(request.getParameter("filename").getBytes("ISO-8859-1"),"GB2312");
        //新建一个smarUpload对象
        SmartUpload su=new SmartUpload();
        PageContext pagecontext=JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8192, true);
        //初始化
        su.initialize(pagecontext);
        su.setContentDisposition(null);
        //下载文件
        try {
            su.downloadFile("e:\\share\\"+fileName);
        } catch (SmartUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}


猜你喜欢

转载自blog.csdn.net/hexu_blog/article/details/74276665
今日推荐