血翼残飞

导航

java 实现文件在线查看

<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属性来绑定window.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;
    }


}

 

posted on 2019-05-10 15:25  血翼残飞  阅读(3127)  评论(0编辑  收藏  举报