栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

以id为参数进行文件下载

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

以id为参数进行文件下载

controller

    @RequestMapping("downloadFile")
    public void downloadData(Long id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        DataRadarFarStation dataRadarFarStation = dataRadarFarStationService.selectDataRadarFarStationById(id);
        //要下载的文件名 从前台传来
        String fileNameNeedDown = dataRadarFarStation.getFileName();
        //这里的路径是要下载的文件所在路径
        String realPath = dataRadarFarStation.getFilePath();
        File file = null;
        file = new File(realPath);

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            String aFileName = null;
            request.setCharacterEncoding("UTF-8");
            String agent = request.getHeader("User-Agent").toUpperCase();
            if ((agent.indexOf("MSIE") > 0)
                    || ((agent.indexOf("RV") != -1) && (agent
                    .indexOf("FIREFOX") == -1))) {
                aFileName = URLEncoder.encode(fileNameNeedDown, "UTF-8");
            } else {
                aFileName = new String(fileNameNeedDown.getBytes("UTF-8"), "ISO8859-1");
            }
            response.setContentType("application/octet-stream");
            response.setHeader("Content-disposition", "attachment; filename="
                    + aFileName);
            response.setHeader("Content-Length", String.valueOf(file.length()));
            bis = new BufferedInputStream(new FileInputStream(new File(realPath)));
            bos = new BufferedOutputStream(response.getOutputStream());
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
            System.out.println("success");
            bos.flush();
        } catch (Exception e) {
            System.out.println("失败!");
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (Exception e) {
            }
        }
    }

service

public  DataRadarFarStation   selectDataRadarFarStationById(String id)


impl

    @Override
    public DataRadarFarStation selectDataRadarFarStationById(Long dataId)
    {
        return DataRadarFarStation .selectDataRadarFarStationById(dataId);
    }

mapper
 
    public DataGts2Query selectDataRadarFarStationById(Long dataId);

sql

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322598.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号