You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.8 KiB

package com.eco.plugin.xx.xdfileencrypt.filter;
import com.eco.plugin.xx.xdfileencrypt.config.xxSimpleConfig;
import com.eco.plugin.xx.xdfileencrypt.utils.FileUtils;
import com.eco.plugin.xx.xdfileencrypt.utils.HttpUtils;
import com.eco.plugin.xx.xdfileencrypt.utils.Utils;
import com.fr.json.JSONObject;
import com.fr.record.analyzer.EnableMetrics;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
@EnableMetrics
public class EncryptOutputStream extends ServletOutputStream {
// private static final String directory = "E:\\temp\\";
// private static final String directory = "/opt/tomcat/temp/";
private static final String directory = "/usr/fine/temp";
private OutputStream original = null;
private boolean closed = false;
private ByteArrayOutputStream crypt = new ByteArrayOutputStream();
public EncryptOutputStream(OutputStream original){
this.original = original;
}
/**
* 实际加密的代码就在这里实现了
* @param bytes
* @return
*/
private byte[] encrypt(byte[] bytes) throws IOException {
String filename = UUID.randomUUID().toString()+Utils.getFileSuffix(bytes);
String originFilePath =directory+filename;
FileUtils.writeBytesToFile(bytes,originFilePath);
File originFile = new File(originFilePath);
String fileSize = xxSimpleConfig.getInstance().getSize();
if(Utils.isNotNullStr(fileSize)){
long size = Long.parseLong(fileSize);
long fileSizeReal = originFile.length()/1024/1024;
if(fileSizeReal > size){
throw new IOException("导出文件超出文件大小设置!");
}
}
String result = HttpUtils.HttpPostJson("http://localhost:8090/encrypt?fileName="+filename,"",null);
JSONObject jsonObject = new JSONObject(result);
String filePath = jsonObject.getString("filePath");
byte[] encryptFile = FileUtils.readBytesFromFile(filePath);
while (validateFile(bytes,encryptFile)){
encryptFile = FileUtils.readBytesFromFile(filePath);
}
byte[] resultByte = encryptFile.clone();
if(originFile.exists() && originFile.isFile()){
originFile.delete();
}
File targetFile = new File(filePath);
if(targetFile.exists() && targetFile.isFile()){
targetFile.delete();
}
return resultByte;
}
private boolean validateFile(byte[] bytes, byte[] encryptFile) {
return bytes[0] == encryptFile[0] && bytes[bytes.length-1] == encryptFile[encryptFile.length-1];
}
@Override
public void write(int b) throws IOException {
if (closed) {
throw new IOException("Cannot write to a closed output stream");
}
crypt.write(b);
}
@Override
public void close() throws IOException {
if( !closed ){
byte[] bytes = crypt.toByteArray();
bytes = encrypt(bytes);
original.write(bytes);
original.flush();
original.close();
closed = true;
}
}
@Override
public void flush() throws IOException {
crypt.flush();
}
@Override
public void write(byte[] data) throws IOException {
write(data, 0, data.length);
}
@Override
public void write(byte[] data, int off, int len) throws IOException {
if (closed) {
throw new IOException("Cannot write to a closed output stream");
}
crypt.write(data, off, len);
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener listener) {
}
}