【问题标题】:To set the password OutputStream from the response to Excel sheet using Java Servlet使用 Java Servlet 从对 Excel 表的响应中设置密码 OutputStream
【发布时间】:2018-04-14 16:34:42
【问题描述】:

如果我从响应中获得了 OutputStream,你可以将 OutputStream 的内容写入响应中,然后再发送回浏览器吗? 在我的场景中,我想在下载工作表时为 Excel 文件设置密码。 所以就这样写了代码。

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=TestFile.xls");
XSSFWorkbook wb = new XSSFWorkbook();
Sheet s = wb.createSheet("Demo");
Row row1 = s.createRow(0);
Row row2 = s.createRow(1);
// create cells in the row
Cell row1col1 = row1.createCell(0);
Cell row1col2 = row1.createCell(1);
Cell row1col3 = row2.createCell(0);
Cell row1col4 = row2.createCell(1);
// add data to the cells
row1col1.setCellValue("City Name");
row1col2.setCellValue("University");
row1col3.setCellValue("Hyderabad");
row1col4.setCellValue("JNTU");
// Add password protection and encrypt the file
POIFSFileSystem fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
// set the password
enc.confirmPassword("123");
// encrypt the file
OPCPackage opc = wb.getPackage();
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
fs.writeFilesystem(response.getOutputStream());

此处密码设置为 Excel 工作表,但出现以下错误时无法打开数据。 请检查并提供您为什么没有获得数据的答案。提前致谢。

【问题讨论】:

    标签: java servlets encryption passwords outputstream


    【解决方案1】:

    Apache POI 使用 HSSFWorkbook 对象来创建 xls 文件和 XSSFWorkbook 对象来创建 xlsx 文件。请检查您用于创建 xls 的对象。

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setHeader("Content-Disposition", "attachment; filename="TestFile.xlsx");
    
                            File fileVal = new File("TestFile.xlsx");
    
                            XSSFWorkbook wb = new XSSFWorkbook();
                            Sheet s = wb.createSheet("Demo");
    
                                  //Add data to your sheet
    
                               // write the excel to a file
                                try {
                                    FileOutputStream fileOut = new FileOutputStream(fileVal);
                                    wb.write(fileOut);
                                    fileOut.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
    
                                // Add password protection and encrypt the file
                                POIFSFileSystem fs = new POIFSFileSystem();
                                EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
                                Encryptor enc = info.getEncryptor();
    
                                // set the password
                                enc.confirmPassword("123");
    
                                // encrypt the file
                                OPCPackage opc = OPCPackage.open(fileVal, PackageAccess.READ_WRITE);
                                OutputStream os = enc.getDataStream(fs);
                                opc.save(os);
                                opc.close();
    
                                // save the file back to the filesystem
                                FileOutputStream fos = new FileOutputStream(fileVal);
                                fs.writeFilesystem(response.getOutputStream());
                                fos.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 2013-11-06
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 2018-03-13
      相关资源
      最近更新 更多