文件下载-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);
    }
}

实例

 /**
     * 导出word文件01
     * @param response
     * @throws IOException
     */
    @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<String, Object> datas = new HashMap<String, Object>() {
            {
                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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值