이번에 Apache poi 사용으로 엑셀 다운로드 암호화 방법이다.
암호화 방법으로 검색하면 나오는 EncryptionInfo 활용하면 된다.
파일손상 또는 엑셀파일
이 잘 만들어지지 않아서 zip파일 이나 다른방법을 검색을 해봤었다.ㅠㅠ
https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
poi 버전 4이상이면 문제가 생겨서.. 3.17로 버전을 낮춰서 테스트하니 잘 만들어졌다!
# version 변경 전
api('org.apache.poi:poi-ooxml:5.0.0')
# version 변경 후
api('org.apache.poi:poi-ooxml:3.17')
EncryptionInfo encryptionInfo = new EncryptionInfo(EncryptionMode.agile);
Encryptor encryptor = encryptionInfo.getEncryptor();
encryptor.confirmPassword(password);
// Write the workbook data to a file
try (FileOutputStream fos = new FileOutputStream(xlsxFile)) {
workbook.write(fos);
fos.close();
workbook.close();
}
// Protecting the excel file
try (POIFSFileSystem fs = new POIFSFileSystem()) {
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
Encryptor encryptor = info.getEncryptor();
// Setting the password 'javacodepoint'
encryptor.confirmPassword("javacodepoint");
/* Read in an existing OOXML file and write to encrypted output stream
* don't forget to close the output stream otherwise the padding bytes
* aren't added
*/
try (OPCPackage opc = OPCPackage.open(xlsxFile, PackageAccess.READ_WRITE);
OutputStream os = encryptor.getDataStream(fs)) {
opc.save(os);
}
// Write out the encrypted version
try (FileOutputStream fos = new FileOutputStream(xlsxFile, false)) {
fs.writeFilesystem(fos);
}
}
//write the excel to a file
try {
ByteArrayOutputStream fileOut = new ByteArrayOutputStream();
wb.write(fileOut);
InputStream filein = new ByteArrayInputStream(fileOut.toByteArray());
POIFSFileSystem fs = new POIFSFileSystem();
//EncryptionInfo info = new EncryptionInfo(fs, EncryptionMode.agile);
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword("AA");
OPCPackage opc = OPCPackage.open(filein);
OutputStream os = enc.getDataStream(fs);
opc.save(os);
opc.close();
OutputStream fileOut2 = null;
//out.clear();
//out = pageContext.pushBody();
fileOut2 = response.getOutputStream();
fs.writeFilesystem(fileOut2);
fileOut2.close();
fileOut.close();
}catch(Exception e){
e.printStackTrace();
}