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.

115 lines
5.8 KiB

package com.hzdatalink.gsdes.filter;
import com.fr.base.ServerConfig;
import com.fr.base.core.IgnoreBytesInputStream;
import com.fr.base.core.ParseResult;
import com.fr.base.core.PostParseUtils;
import com.fr.general.CommonIOUtils;
import com.fr.log.FineLoggerFactory;
import com.fr.stable.StringUtils;
import com.fr.third.org.apache.commons.io.IOUtils;
import com.fr.web.utils.WebUtils;
import com.hzdatalink.gsdes.FileOperation;
import com.hzdatalink.gsdes.IsFileEncryptedResult;
import com.hzdatalink.gsdes.bean.BodyReaderHttpServletRequestWrapper;
import com.hzdatalink.gsdes.util.FileUtil;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* @author xx
* @date 2020/11/20
*/
public class DecodeFilter {
private static final byte[] NEW_LINE_BYTES = new byte[]{13, 10};
private static final byte[] BOUNDARY_END = new byte[]{45, 45};
private static final String TMP_PATH = System.getProperty("java.io.tmpdir");
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) {
try {
FineLoggerFactory.getLogger().info("decode request uri: {}", req.getRequestURI());
BodyReaderHttpServletRequestWrapper wrapper = new BodyReaderHttpServletRequestWrapper(req);
InputStream inputStream = wrapper.getInputStream();
ParseResult parseResult = PostParseUtils.parse(inputStream, req.getCharacterEncoding());
InputStream fileDecorator = new IgnoreBytesInputStream(inputStream, this.concat(this.concat(NEW_LINE_BYTES, parseResult.getBoundary().getBytes()), BOUNDARY_END));
String filename = WebUtils.getHTTPRequestParameter(req, "filename");
File file = saveTmp(fileDecorator, filename);
FineLoggerFactory.getLogger().info("record file to {}", file.getPath());
byte[] bytes = null;
IsFileEncryptedResult encode = FileOperation.isFileEncrypted(file.getAbsolutePath());
FineLoggerFactory.getLogger().info("isFileEncrypted res is {}", encode.getSucceeded());
if (encode != null && encode.getEncrypted()) {
FineLoggerFactory.getLogger().info("current file is encode file");
File decodeFile = FileUtil.createTempFile();
FineLoggerFactory.getLogger().info("decode file path is {}",decodeFile.getAbsolutePath());
FileOperation.decodeFile(file.getAbsolutePath(), decodeFile.getAbsolutePath());
bytes = CommonIOUtils.inputStream2Bytes(new FileInputStream(decodeFile));
decodeFile.deleteOnExit();
}
if (bytes == null) {
FineLoggerFactory.getLogger().info("current file is not decode");
} else {
FineLoggerFactory.getLogger().info("decode file byte size is {}", bytes.length);
byte[] body = null;
String character = req.getCharacterEncoding();
if (StringUtils.isNotEmpty(character)) {
body = handleBody(bytes, parseResult, req.getCharacterEncoding());
} else {
body = handleBody(bytes, parseResult, ServerConfig.getInstance().getServerCharset());
}
wrapper.setBody(body);
}
file.delete();
FineLoggerFactory.getLogger().info("delete temp file");
filterChain.doFilter(wrapper, res);
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
private File saveTmp(InputStream input, String filename) throws IOException {
File file = FileUtil.createTempFile();
try {
FileOutputStream outputStream = new FileOutputStream(file.getPath());
IOUtils.copyLarge(input, outputStream);
FineLoggerFactory.getLogger().info("save tmp file path is {}", file.getPath());
} catch (IOException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
return file;
}
private byte[] concat(byte[] a, byte[] b) {
byte[] c = new byte[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
private byte[] handleBody(byte[] body, ParseResult parseResult, String charsetName) throws UnsupportedEncodingException {
byte[] newBody;
byte[] start;
byte[] end;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(parseResult.getBoundary()).append("\r\n")
.append("Content-Disposition:").append(parseResult.getDispline())
.append("; name=\"").append(parseResult.getFieldName())
.append("\"; filename=\"").append(parseResult.getFileName()).append("\"\r\n")
.append("Content-Type: ").append(parseResult.getMime()).append("/").append(parseResult.getSubMime()).append("\r\n").append("\r\n");
start = stringBuilder.toString().getBytes(charsetName);
StringBuilder stringBuilder1 = new StringBuilder();
stringBuilder1.append("\r\n").append(parseResult.getBoundary()).append("--\r\n");
end = stringBuilder1.toString().getBytes(charsetName);
// newBody = this.concat(this.concat(("------WebKitFormBoundary6c2DUJvlVKG0I1po\r\n" +
// "Content-Disposition: form-data; name=\"FileData\"; filename=\"1.xlsx\"\r\n" +
// "Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n" +
// "\r\n").getBytes(charsetName), body), ("\r\n------WebKitFormBoundary6c2DUJvlVKG0I1po--\r\n").getBytes(charsetName));
newBody = this.concat(this.concat(start, body), end);
return newBody;
}
}