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;
    }


}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM