web端java实现url下载接口

其实没那么糟糕
只是你等的人还没到
内心的山川河流
你都已经备好
只等大雁回归
在屋檐下筑个巢

  • 方式一,非Spring环境,纯JAVA类
public void download(String href, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileName = href.substring(href.lastIndexOf('/') + 1);
        //url
        String notesUrl = href;
        //下载文件名称
        String notesName =fileName;
        ServletOutputStream out = null;
        InputStream inputStream = null;

        try {
            //文件名
            String pdfName = notesName ;
            //路径
            String path = notesUrl ;
            // 获取外部文件流
            //logger.info("下载中------invPdfUrl=" +path);
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(3 * 1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            inputStream = conn.getInputStream();
            /**
             * 输出文件到浏览器
             */
            int len = 0;
            // 输出 下载的响应头,如果下载的文件是中文名,文件名需要经过url编码
            response.setContentType("text/html;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(pdfName, "UTF-8"));
            response.setHeader("Cache-Control", "no-cache");
            out = response.getOutputStream();
            byte[] buffer = new byte[1024];
            while ((len = inputStream.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (Exception e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                }
            }
        }
    }
  • 方式二,Spring环境,使用Spring 封装的工具类
public void download(String url, HttpServletResponse response){
        InputStream is=null;
        ServletOutputStream outputStream=null;
        try {
            URL httpUrl = new URL(url);
            URLConnection con = httpUrl.openConnection();
            is = con.getInputStream();
            outputStream = response.getOutputStream();
            //生成文件名
            String suffix = url.substring(url.lastIndexOf("."));
            String fileName = UUID.randomUUID()+suffix;
            System.out.println(fileName);
            //设置响应头,attachment表示以附件的形式下载,inline表示在线打开
            response.setHeader("content-disposition","attachment;fileName="+fileName);//下载时浏览器显示的名称
            FileCopyUtils.copy(is,outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

文章持续更新,可以微信搜索「 绅堂Style 」第一时间阅读,回复【资料】有我准备的面试题笔记。
GitHub https://github.com/dtt11111/Nodes 有总结面试完整考点、资料以及我的系列文章。欢迎Star。
在这里插入图片描述

你可能感兴趣的:(JAVA)