<input class="btn btn-primary" type="button" value="在线查看" onclick="showDocAttach(${li.extPrjDocAttachmentId})"/>
JS: 新窗口打开 ,url 参数隐藏;
- function showDocAttach(extPrjDocAttachmentId) {
- var url = 'showDocAttach.action';
- // 参数隐藏
- // 创建 form 表单
- var tempForm = document.createElement("form");
- tempForm.id="tempFormId";
- tempForm.method = 'post';
- tempForm.action = url;
- // 利用表单的 target 属性来绑定 Windows.open 的一些参数 (如设置窗体属性的参数等)
- tempForm.target = "_blank";
- var input = document.createElement("input");
- input.type = 'hidden';
- input.name = 'extPrjDocAttachmentId';
- input.value = extPrjDocAttachmentId;
- tempForm.appendChild(input);
- // 将此 form 表单添加到页面主体 body 中
- document.body.appendChild(tempForm);
- tempForm.submit();
- // 将此 form 表单移除 body 中
- document.body.removeChild(tempForm);
- }
后台 controller, 图片文件, PDF 文件直接输出到页面, docx 文档通过 aspose 转化为 PDF 文件;
- public String showDocAttach() {
- HttpServletRequest request = ServletActionContext.getRequest();
- HttpServletResponse response = ServletActionContext.getResponse();
- OutputStream outputStream = null;
- BufferedInputStream bufferedInputStream = null;
- try {
- extprjdocattachment = "数据库查询 java 对象";
- if (extprjdocattachment != null) {
- // 获取存储的文件路径
- String path = extprjdocattachment.getFilePath();
- // 获取存储的文件类型
- String fileType = extprjdocattachment.getFileType();
- File file = new File(path);
- if (!file.exists()) {
- return null;
- }
- response.setContentType("text/html; charset=UTF-8");
- //PDF jsp PNG 直接输出到页面
- if ("pdf".equalsIgnoreCase(fileType)) {
- response.setContentType("application/pdf");
- } else if ("docx".equalsIgnoreCase(fileType)) {
- //docx 使用 aspose 转化为 PDF 文件
- String tempPath = "tempPdf" + "/" ;
- Doc2PdfUtil.doc2Pdf(path, tempPath, extprjdocattachment.getFileName());
- path = tempPath + extprjdocattachment.getFileName() + ".pdf";
- response.setContentType("application/pdf;charset=UTF-8");
- } else if ("jpg".equalsIgnoreCase(fileType) || "png".equalsIgnoreCase(fileType)) {
- response.setContentType("image/" + fileType);
- }
- bufferedInputStream = new BufferedInputStream(new FileInputStream(path));
- outputStream = response.getOutputStream();
- int count = 0;
- byte[] buffer = new byte[1024 * 1024];
- while ((count = bufferedInputStream.read(buffer)) != -1) {
- outputStream.write(buffer, 0, count);
- }
- outputStream.flush();
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (outputStream != null) {
- try {
- outputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (bufferedInputStream != null) {
- try {
- bufferedInputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return null;
- }
aspose 转 PDF 工具类:
jar 下载:
链接: https://pan.baidu.com/s/1xgIhMV5C8sGy8cYQ01kHhg
提取码: m90t
- package *********;
- import com.aspose.words.Document;
- import com.aspose.words.License;
- import com.aspose.words.SaveFormat;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- /**
- * Created by Administrator on 2019/5/10.
- */
- public class Doc2PdfUtil {
- /**
- * doc 转 PDF
- * @param docPath doc 文件路径, 包含. doc
- * @param pdfPath PDF 文件路径
- * @return
- */
- public static File doc2Pdf(String docPath, String pdfPath,String fileName){
- File tempPath = new File(pdfPath);
- File pdfFile = new File(pdfPath+fileName+".pdf");
- if (!tempPath.exists()){
- tempPath.mkdir();
- }
- try {
- String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";
- ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
- License license = new License();
- license.setLicense(is);
- Document document = new Document(docPath);
- document.save(new FileOutputStream(pdfFile), SaveFormat.PDF);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return pdfFile;
- }
- }
来源: http://www.bubuko.com/infodetail-3053692.html