java如何实现文件下载

本博客是写给自己玩的(总结),学习中 :)

**1.问题标题(标题中我通常会包含tag方便查询)
2.问题描述(包括软硬环境人的操作等)
<1>不会从数据库取值,取出来是空值
<2>不会写数据库语句

3.问题影响(影响范围产生的危害)
基础不好导致bug满天飞
4.分析过程(详细记录分析的过程)
<1>建一个类,写查询语句,通过sql找到实体,然后return给调用它的方法

public String getType(String fid) {
        String sql = "select f_type from TBTKY_DATA_SYB where f_id=" + fid;
        Map map = serMetaDataDao.getEntityBySql(sql);
        Object obj = map.get("f_type");
        String s = obj.toString();
        if (s.contains("Exel")) {
            s="xls";
        }
        return s;
    }

针对这次空值,是因为我的jdk版本与项目不一致。

从pl/sql数据库中取出文件路径,用File IO流操作
5.问题原因(分析后得出的结论)
6.解决方法(写出核心的知识点及操作步骤)

public void downFileZip() throws Exception {
        String fileFloderPath = "";
        File file = null;
        HttpServletRequest request = ServletActionContext.getRequest();
        String fid = request.getParameter("fid");

        String fpath = request.getParameter("fforderpath");

        String fname = request.getParameter("fname");

//      String ftype = request.getParameter("ftype");

        try {
            if (fpath != null) {
                // 获取生成的文件路径   通过fid获取ftype
                fileFloderPath = fpath+"/"+fname+"."+this.statisticalyearbookService.getType(fid);
                file = new File(fileFloderPath);
                download(file, "zip");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
public String getType(String fid) {
        String sql = "select f_type from TBTKY_DATA_SYB where f_id=" + fid;
        Map map = serMetaDataDao.getEntityBySql(sql);
        Object obj = map.get("f_type");
        String s = obj.toString();
        if (s.contains("Exel")) {
            s="xls";
        }
        return s;
    }

public void download(File file, String fileType) {
        HttpServletResponse res = ServletActionContext.getResponse();
        try {
            if (!file.exists()) {
                String url = this.getRequest().getScheme() + "://"
                        + this.getRequest().getServerName() + ":"
                        + this.getRequest().getServerPort()
                        + this.getRequest().getContextPath() + "/"
                        + "fileNotFound.jsp";
                this.getResponse().sendRedirect(url);
                return;
            }
            InputStream brSource = new BufferedInputStream(new FileInputStream(
                    file));
            // 清空输出流
            res.reset();
            // 设定输出文件头
            res.setContentType("application/x-" + fileType
                    + "-compressed;charset=utf-8");
            res.setHeader("Content-Disposition", "attachment;filename="
                    + new String(file.getName().getBytes("GBK"), "ISO-8859-1"));
            res.addHeader("Content-Length", "" + file.length());
            OutputStream out = new BufferedOutputStream(res.getOutputStream());
            int i = 0;
            byte[] buffer = new byte[800];
            while (true) {
                if (brSource.available() < 800) {
                    while (i != -1) {
                        i = brSource.read();
                        out.write(i);
                    }
                    break;
                } else {
                    brSource.read(buffer);
                    out.write(buffer);

                }
            }
            brSource.close();
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

7.参考资料(分析过程中的各种文章资料等)
8.心得体会**

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值