代碼: 兵馬未動,糧草先行
作者: 傳說中的汽水槍
如有錯誤,請留言指正,歡迎一起探討.
轉載請注明出處.
公司要求從阿里雲OSS下載pdf文件並且需要添加水印.
因此這里總結一下.
首先添加了一個FileUploadUtil.java文件:
/**OSSClient*/ private static OSSClient CLIENT; public static InputStream getInputStreamFromOSS(String ossFileName) { String parentDirectory = ossFileName.substring(0, 8) + "/"; String fileId = "你自己的fileId"; CLIENT = new OSSClient(ENDPOINT, ACCESSKEYID, ACCESSKEYSECRET); OSSObject ossObject = CLIENT.getObject(BUCKETNAME, fileId + ossFileName); InputStream inputStream = ossObject.getObjectContent(); return inputStream; } public static void clientShutdown() { if (CLIENT != null) { CLIENT.shutdown(); } }
在controller層先實現下載文件功能:
@ResponseBody @RequestMapping(value = "/downloadFromOSS", method = RequestMethod.GET) public void downloadFromOSS(String fileName, HttpServletResponse response, HttpServletRequest request) { try { String dataString = DateUtil.date2Str(new Date(), "yyyy-MM-dd-HH-mm-ss-SSS"); String destFileName = dataString + ".pdf"; response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + destFileName); // 獲取outputStream OutputStream outputStream = response.getOutputStream(); // 獲取inputStream InputStream inputStream = FileUploadUtil.getInputStreamFromOSS(fileName); // 下載文件 byte[] bytes = new byte[2048]; int length; while ((length = inputStream.read(bytes)) > 0) { outputStream.write(bytes, 0, length); } outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { FileUploadUtil.clientShutdown(); } }
實現下載並添加水印:
@ResponseBody @RequestMapping(value = "/downloadWatermarkFile", method = RequestMethod.GET) public void downloadWatermarkFile(String fileName, HttpServletResponse response, HttpServletRequest request) { try { String dataString = DateUtil.date2Str(new Date(), "yyyy-MM-dd-HH-mm-ss-SSS"); String destFileName = dataString + ".pdf"; response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); response.setHeader("Content-Disposition", "attachment;fileName=" + destFileName); // 獲取outputStream OutputStream outputStream = response.getOutputStream(); // 獲取inputStream InputStream inputStream = FileUploadUtil.getInputStreamFromOSS(fileName); // 添加水印的時候,就已經在outputStream寫入了 PdfReader reader = new PdfReader(inputStream); PdfStamper stamper = new PdfStamper(reader, outputStream); int total = reader.getNumberOfPages() + 1; PdfContentByte content; BaseFont base = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); PdfGState gs = new PdfGState(); for (int i = 1; i < total; i++) { content = stamper.getOverContent(i);// 在內容上方加水印 //content = stamper.getUnderContent(i);//在內容下方加水印 gs.setFillOpacity(0.2f); content.setGState(gs); content.beginText(); content.setColorFill(com.itextpdf.text.BaseColor.LIGHT_GRAY); content.setFontAndSize(base, 50); content.setTextMatrix(70, 200); //將文字顯示在pdf頁面中 // content.showTextAligned(Element.ALIGN_CENTER, "國際財富管理協會(中國)!", 300,350, 55); //設置文字顏色 content.setColorFill(com.itextpdf.text.BaseColor.BLACK); //設置文字大小 content.setFontAndSize(base, 8); //將內容顯示在pdf底部 String waterMarkName = "111111"; content.showTextAligned(Element.ALIGN_CENTER, "下載時間:" + waterMarkName + "", 300, 10, 0); content.endText(); } stamper.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { FileUploadUtil.clientShutdown(); } }
OK 解決問題.