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

文件下载-java

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

文件下载-java

工具类
public class FileDownloadUtil {

    public void downloadLocal(HttpServletResponse response) throws FileNotFoundException, UnsupportedEncodingException {
        // 下载本地文件
        //String fileName = "图片.gif".toString(); // 文件的默认保存名
       // String fileName = new String("图片.gif".getBytes(),"ISO-8859-1"); // 文件的默认保存名
        String fileName = URLEncoder.encode("图片.gif","UTF-8");
        // 读到流中
        InputStream inStream = new FileInputStream("f:/abc.gif");// 文件的存放路径
        // 设置输出的格式
        // 支持中文名称文件,需要对header进行单独设置,不然下载的文件名会出现乱码或者无法显示的情况
        // String downloadFileName = new String(fileName .getBytes(), "ISO-8859-1");
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition", "attachment; filename="" + fileName + """);
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void downloadNet(HttpServletResponse response) throws IOException {
        // 下载网络文件
        int bytesum = 0;
        int byteread = 0;

        URL url = new URL("https://img1.baidu.com/it/u=3246628741,3439955235&fm=26&fmt=auto");

        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            FileOutputStream fs = new FileOutputStream("f:/abc.gif");
            byte[] buffer = new byte[1204];
            int length;
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                System.out.println(bytesum);
                fs.write(buffer, 0, byteread);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        FileDownloadUtil fileDownloadUtil = new FileDownloadUtil();
        HttpServletResponse httpServletResponse = null;
        fileDownloadUtil.downloadNet(httpServletResponse);
    }
}

实例
 
    @RequestMapping(value = "/download_word", method = RequestMethod.GET)
    public void  download_word(HttpServletResponse response) throws IOException {
        byte[] base64 = base64.decodebase64("iVBOJUBgEwEBYosQIECgfIH1fi+YH0BT/rqOX4EA6dd6uhoCBAgQIECAAAECSy0gQJZ6eQmCC");
        ByteArrayInputStream in = new ByteArrayInputStream(base64);
        BufferedImage image = ImageIO.read(in);
        Map datas = new HashMap() {
            {
                put("Name", new TextRenderData("33ff33", "Alex Bin001"));
                put("Age", "24");
                //   put("Sources", new HyperlinkTextRenderData("baidu", "http://www.baidu.com"));
                // 本地图片 可以绝对路径但不推荐
                put("Image", new PictureRenderData(120, 120, new ClassPathResource("static/1.png").getFile().getPath()));
                //java图片/base64等格式图片
                put("JavaImage", new PictureRenderData(600, 350, ".png", BytePictureUtils.getBufferByteArray(image)));
            }
        };
        //String fileName = new String("导出out_template_word.docx".getBytes(),"ISO-8859-1");
        String fileName = URLEncoder.encode("导出out_template_word.docx","UTF-8");
        ClassPathResource oldDoc = new ClassPathResource("doc/TL.docx");
        String filePath = oldDoc.getFile().getPath();
        XWPFTemplate template = XWPFTemplate.compile(filePath)
                .render(datas);
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=""+fileName+""");
        response.flushBuffer();
        template.write(response.getOutputStream());
    }

    @RequestMapping(value = "/download_test", method = RequestMethod.GET)
    public void  download_test(HttpServletResponse response) throws IOException {
        FileDownloadUtil fileDownloadUtil = new FileDownloadUtil();
        fileDownloadUtil.downloadLocal(response);
    }

https://www.cnblogs.com/xiaoyue1606bj/p/10985764.html

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

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

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