从七牛服务下载PDF文件

/**
     * 从七牛下载PDF文件
     * @param request
     * @param response
     * @param exhiId
     * @throws MalformedURLException
     */
    @RequestMapping("/loadCanZhanHan")
    public void downloadNet(HttpServletRequest request, HttpServletResponse response,String exhiId) throws MalformedURLException{
        ExhiMain exhiMain = exhiMainService.get(exhiId);
        String canZhanHanUrl = exhiMain.getCanZhanHanUrl();
        try {
            if(canZhanHanUrl !=null && !"".equals(canZhanHanUrl)){
                PDFUtil.downloadZhaoZhanHan(response, canZhanHanUrl);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
       }
/**
      * 下载七牛上面的PDF文件
      * @param response 
      * @param canZhanHanUrl 七牛上的文件地址
      * @throws Exception
      */
    public static void downloadZhaoZhanHan(HttpServletResponse response,String canZhanHanUrl) throws Exception{
        int byteread=0;
        URL url = new URL(canZhanHanUrl);
        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            ServletOutputStream outputStream = response.getOutputStream();
            
            response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("招展函.pdf", "UTF-8"));
            response.setContentType("application/force-download");
             byte[] buffer = new byte[1024];
             while((byteread=inStream.read(buffer)) !=-1) {
                 outputStream.write(buffer, 0, byteread);
             }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

 js:

var e = $('input[name="exhiId"]').val();
                    window.location.href = pageConfig.ctx + "/exhi/loadCanZhanHan?exhiId=" + e
原文地址:https://www.cnblogs.com/SHMILYHP/p/5275161.html