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。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章