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

文件下载到本地

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

文件下载到本地

打开本地文件的属性查看路径 

 

Java代码

    @ApiOperation(value = "文件下载", notes = "导出excel表格")
    @RequestMapping(value = "/clientDownload", method = RequestMethod.GET)
    public void orderFile(ModelMap model, HttpServletRequest request, HttpServletResponse response, @RequestParam String exeName) {
        InputStream inputStream = null;
        //设置下载文件的名字
        exeName=exeName+".xls";
        try{
            //String contextRealPath = FilePropertyUtil.getFilePathProperty().getProperty("temPath");//request.getSession().getServletContext().getRealPath("/");
            //这里是本地文件的路径
            inputStream = new FileInputStream(new File("文件的路径记得加后缀C:Users\antong.xls"));

            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(exeName.getBytes("gb2312")));
            byte[] b = new byte[1024];
            int len = -1;
            OutputStream outputStream = response.getOutputStream();
            while ((len = inputStream.read(b)) !=-1) {
                
                outputStream.write(b, 0, len);  //读取字节,从0到len长度
            }
            inputStream.close();
            //setSuccess(model);
        }catch(Exception e){
            e.printStackTrace();
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e1) {
                }
            }
            //setFailure(model);
        }
    }

然后浏览器访问

 然后就可以下载了

 

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

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

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