簡體   English   中英

我正在嘗試編寫一個程序,該程序將為用戶提供密碼保護 pdf。 用戶需要能夠上傳pdf並下載受保護的pdf

[英]I'm trying to write a program that'll password protect a pdf for a user. The user needs to be able to upload the pdf and download a protected one

在我的嘗試中,我會得到一個損壞的文件或一個不受保護的 pfd,這是我使用的代碼:

//上傳文件方法

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<List<String>> uploadFiles(@RequestParam("files")List<MultipartFile> multipartFiles) throws IOException, DocumentException {
    List<String> filenames = new ArrayList<>();
    for (MultipartFile file : multipartFiles){
        String filename = StringUtils.cleanPath(file.getOriginalFilename());
        Path fileStorage = get(DIRECTORY, filename).toAbsolutePath().normalize();
        copy(file.getInputStream(), fileStorage, REPLACE_EXISTING);

        File f = new File(String.valueOf(fileStorage));
        if(!f.exists() && !f.isDirectory()) {
            try {
                FileInputStream fis = new FileInputStream(f);
                PdfReader pdfReader = new PdfReader(fis);
                pdfReader.setUnethicalReading(true);
                WriterProperties writerProperties = new WriterProperties();
                writerProperties.setStandardEncryption(OWNER_PASSWORD.getBytes(),
                        USER_PASSWORD.getBytes(), EncryptionConstants.ALLOW_PRINTING,
                        EncryptionConstants.ENCRYPTION_AES_128);
                PdfWriter pdfWriter = new PdfWriter(new FileOutputStream("Protected_"+DIRECTORY), writerProperties);
                PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter);
                pdfDocument.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        filenames.add(filename);

    }
    return ResponseEntity.ok().body(filenames);
}

對這個問題的任何幫助將不勝感激。 因為我需要我的用戶上傳一個普通的 pdf 文件並且能夠下載具有密碼保護的相同 pdf 文件

感謝您建議將其分解,創建有助於生成受保護文件的加密方法。

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResponseEntity<List<String>> uploadFiles(@RequestParam("files")List<MultipartFile> multipartFiles) throws IOException, DocumentException {
        List<String> filenames = new ArrayList<>();
        for (MultipartFile file : multipartFiles){
            String filename = StringUtils.cleanPath(file.getOriginalFilename());
            Path fileStorage = get(DIRECTORY, filename).toAbsolutePath().normalize();
            copy(file.getInputStream(), fileStorage, REPLACE_EXISTING);
            encrypt(fileStorage, filename);
            filenames.add(filename);

        }
        return ResponseEntity.ok().body(filenames);
    }

    public static void encrypt(Path src, String filename){
            try {
                FileInputStream fis = new FileInputStream(String.valueOf(src));
                PdfReader pdfReader = new PdfReader(fis);
                pdfReader.setUnethicalReading(true);
                WriterProperties writerProperties = new WriterProperties();
                writerProperties.setStandardEncryption(OWNER_PASSWORD.getBytes(),
                        USER_PASSWORD.getBytes(), EncryptionConstants.ALLOW_PRINTING,
                        EncryptionConstants.ENCRYPTION_AES_128);
                PdfWriter pdfWriter = new PdfWriter(new FileOutputStream(filename), writerProperties);
                PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter);
                pdfDocument.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM