从七牛服务下载PDF文件
Posted 阿若蜜意
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从七牛服务下载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(); } }
以上是关于从七牛服务下载PDF文件的主要内容,如果未能解决你的问题,请参考以下文章