附件点击即下载的实现

客户需求:点击附件后不打开附件,而是直接下载

实现思想:将附件读入InputStream,并写入OutputStream返回给Response

实现代码

download.jsp:

<a href="<c:url value="/front/download.do?documentId=${document.id}"/>">${document.name}</a>

downloadController.java:

    @RequestMapping("/download.do")
    public void download(@RequestParam(value = "documentId",required = true)Long documentId,
                         ModelMap modelMap,HttpServletRequest request ,HttpServletResponse response){
        //保存数据
        if(documentId > 0){
            //取得附件信息
            List<LmsResourceEntity> lmsResourceList = lmsResourceDao.findById(documentId);

            for(LmsResourceEntity lmsResource : lmsResourceList){
            	
                //取得附件在服务器上的路径(D:/Tomcat7/webapp/...)
                String savePath = request.getSession().getServletContext().getRealPath("/") + lmsResource.getUrl();

                System.out.println("store path is :" + savePath);

                //Response编码设为UTF-8
                response.setContentType("text/html;charset=utf-8");

                try {
                    request.setCharacterEncoding("UTF-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException("设置错误",e);
                }

                BufferedInputStream bis = null;
                BufferedOutputStream bos = null;

                try {
                    long fileLength = new File(savePath).length();

                    response.setContentType("application/x-msdownload;");

                    //注意!filename需要转码,否则中文名会显示不了
                    response.setHeader("Content-disposition", "attachment; filename=" + new String( lmsResource.getName().getBytes("gb2312"), "ISO8859-1" ) );
                    response.setHeader("Content-Length", String.valueOf(fileLength));

                    //读取文件
                    bis = new BufferedInputStream(new FileInputStream(savePath));

                    //bos设置为Response的OutputStream
                    bos = new BufferedOutputStream(response.getOutputStream());

                    //将文件写入bos
                    byte[] buff = new byte[2048];
                    int bytesRead;
                    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                        bos.write(buff, 0, bytesRead);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null)
                        try {
                            bis.close();
                        } catch (IOException e) {
                            throw new RuntimeException("下载失败",e);
                        }
                    if (bos != null)
                        try {
                            bos.close();
                        } catch (IOException e) {
                            throw new RuntimeException("下载失败",e);
                        }
                }
            }
        }
        else{
        	System.out.println("文件下载无效documentId");
        }
    }


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值