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


}

 

转载于:https://www.cnblogs.com/xueyicanfei/p/10844668.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 实现文件在线签名需要结合数字签名和文件读写两个方面。 数字签名的实现可以参考我在之前问题中提供的示例代码,这里不再赘述。 文件读写方面,可以使用 Java文件操作类来实现,示例代码如下: ```java import java.io.*; import java.security.*; public class FileDigitalSignature { public static void main(String[] args) throws Exception { String fileName = "test.txt"; // 待签名的文件名 String signatureFileName = "test.txt.sig"; // 签名文件名 // 读取文件内容 File file = new File(fileName); FileInputStream fis = new FileInputStream(file); byte[] dataBytes = new byte[(int) file.length()]; fis.read(dataBytes); fis.close(); // 生成密钥对 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(1024); KeyPair keyPair = keyGen.generateKeyPair(); // 签名 Signature sig = Signature.getInstance("SHA1withRSA"); sig.initSign(keyPair.getPrivate()); sig.update(dataBytes); byte[] signatureBytes = sig.sign(); // 将签名写入文件 FileOutputStream fos = new FileOutputStream(signatureFileName); fos.write(signatureBytes); fos.close(); System.out.println("Digital signature generated and saved to file " + signatureFileName); } } ``` 以上代码中,我们使用了 Java文件操作类,首先读取待签名的文件内容,然后使用数字签名算法生成数字签名。接着将数字签名写入文件,完成文件签名操作。 需要注意的是,在实际应用中,还需要考虑文件的完整性和安全性等问题,例如文件摘要算法、证书管理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值