45 changed files with 2237 additions and 1 deletions
Binary file not shown.
Binary file not shown.
@ -1,3 +1,6 @@
|
||||
# open-JSD-8646 |
||||
|
||||
JSD-8646 导出导入excel文件加解密 |
||||
JSD-8646 导入导出excel加解密 开源任务材料\ |
||||
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||||
<plugin> |
||||
<id>com.fr.plugin.xxxx.report.ecode</id> |
||||
<main-package>com.fr.plugin.xxxx.report.ecode</main-package> |
||||
<name><![CDATA[report加密解密]]></name> |
||||
<active>yes</active> |
||||
<version>1.4</version> |
||||
<env-version>10.0</env-version> |
||||
<jartime>2018-07-31</jartime> |
||||
<vendor>fr.open</vendor> |
||||
<description><![CDATA[折叠树导出]]></description> |
||||
<change-notes><![CDATA[ |
||||
[2021-10-13]【1.0】初始化插件。<br/> |
||||
[2021-10-21]【1.1】增加返回请求长度。<br/> |
||||
[2021-10-21]【1.2】修改导出加密方法。<br/> |
||||
[2021-10-22]【1.3】增加填报上传支持。<br/> |
||||
[2021-10-22]【1.4】修改获取文件名为空的问题。<br/> |
||||
]]></change-notes> |
||||
<extra-decision> |
||||
<GlobalRequestFilterProvider class="com.fr.plugin.xxxx.report.ecode.GlobalFilter"/> |
||||
</extra-decision> |
||||
<extra-report> |
||||
<ExportExtensionProcessor class="com.fr.plugin.xxxx.report.ecode.EncodeExtensionProcessor"/> |
||||
</extra-report> |
||||
<function-recorder class="com.fr.plugin.xxxx.report.ecode.GlobalFilter"/> |
||||
</plugin> |
@ -0,0 +1,9 @@
|
||||
package com.fr.plugin.xxxx.report.ecode; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @date 2020/5/14 |
||||
*/ |
||||
public class Constants { |
||||
public static final String PLUGIN_ID = "com.fr.plugin.xxxx.report.ecode"; |
||||
} |
@ -0,0 +1,46 @@
|
||||
package com.fr.plugin.xxxx.report.ecode; |
||||
|
||||
import com.fr.io.collection.ExportCollection; |
||||
import com.fr.plugin.xxxx.report.ecode.operate.EncodeExcelOperate; |
||||
import com.fr.plugin.xxxx.report.ecode.operate.EncodeImageOperate; |
||||
import com.fr.plugin.xxxx.report.ecode.operate.EncodePDFOperate; |
||||
import com.fr.plugin.xxxx.report.ecode.operate.EncodeWordOperate; |
||||
import com.fr.web.core.ReportSessionIDInfor; |
||||
import com.fr.web.core.reserve.DefaultExportExtension; |
||||
import com.fr.web.core.reserve.Operate; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodeExtensionProcessor extends DefaultExportExtension { |
||||
/** |
||||
* 简单定义一个map来存储导出格式和Operate之间的关系, |
||||
* 并且初始化 |
||||
*/ |
||||
private static Map<String, Operate> operateMap = new HashMap<String, Operate>(); |
||||
|
||||
static { |
||||
//pdf就使用自定义的EncodePDFOperate
|
||||
operateMap.put("pdf", new EncodePDFOperate()); |
||||
operateMap.put("word", new EncodeWordOperate()); |
||||
operateMap.put("image", new EncodeImageOperate()); |
||||
operateMap.put("excel", new EncodeExcelOperate()); |
||||
} |
||||
|
||||
@Override |
||||
public ExportCollection createCollection(HttpServletRequest req, HttpServletResponse res, ReportSessionIDInfor reportSessionIDInfor, String format, String fileName, boolean isEmbed) throws Exception { |
||||
//优先从自定义的map中获取Operate
|
||||
Operate operate = operateMap.get(format.toLowerCase()); |
||||
if (operate != null) { |
||||
//调用Operate的createCollection方法返回
|
||||
return operate.newExportCollection(req, res, reportSessionIDInfor, fileName); |
||||
} |
||||
return super.createCollection(req, res, reportSessionIDInfor, format, fileName, isEmbed); |
||||
} |
||||
} |
@ -0,0 +1,95 @@
|
||||
package com.fr.plugin.xxxx.report.ecode; |
||||
|
||||
import com.fr.decision.fun.impl.AbstractGlobalRequestFilterProvider; |
||||
import com.fr.decision.webservice.v10.config.ConfigService; |
||||
import com.fr.intelli.record.Focus; |
||||
import com.fr.intelli.record.Original; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.context.PluginContexts; |
||||
import com.fr.plugin.xxxx.report.ecode.filter.DecodeFilter; |
||||
import com.fr.plugin.transform.FunctionRecorder; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.stable.fun.Authorize; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.ServletException; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* @Author fr.open |
||||
* @Date 2021/8/1 |
||||
* @Description |
||||
**/ |
||||
@FunctionRecorder |
||||
@Authorize(callSignKey = Constants.PLUGIN_ID) |
||||
@EnableMetrics |
||||
public class GlobalFilter extends AbstractGlobalRequestFilterProvider { |
||||
@Override |
||||
public String filterName() { |
||||
return "global"; |
||||
} |
||||
|
||||
@Override |
||||
@Focus(id = Constants.PLUGIN_ID, text = "report加密", source = Original.PLUGIN) |
||||
public String[] urlPatterns() { |
||||
if (PluginContexts.currentContext().isAvailable()) { |
||||
String servletPathName = "decision"; |
||||
try { |
||||
servletPathName = ConfigService.getInstance().getBasicParam().getServletPathName(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
return new String[]{ |
||||
"/" + servletPathName + "/v10/attach/upload", |
||||
"/" + servletPathName + "/v10/dataset/upload", |
||||
"/" + servletPathName + "/view/report", |
||||
|
||||
}; |
||||
}else { |
||||
return new String[0]; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) { |
||||
|
||||
if (isDncode(req)) { |
||||
new DecodeFilter().doFilter(req, res, filterChain); |
||||
return; |
||||
} |
||||
/*if(isEncode(req)){ |
||||
new EncodeFilter().doFilter(req,res,filterChain); |
||||
return; |
||||
}*/ |
||||
|
||||
try { |
||||
filterChain.doFilter(req, res); |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage()); |
||||
} catch (ServletException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage()); |
||||
} |
||||
} |
||||
|
||||
private boolean isDncode(HttpServletRequest req){ |
||||
if(req.getRequestURI().endsWith("attach/upload")){ |
||||
return true; |
||||
} |
||||
if(req.getRequestURI().endsWith("/dataset/upload")){ |
||||
return true; |
||||
} |
||||
String op = WebUtils.getHTTPRequestParameter(req, "op"); |
||||
String cmd = WebUtils.getHTTPRequestParameter(req, "cmd"); |
||||
if(StringUtils.equals("fr_attach", op) && StringUtils.equals("ah_upload", cmd)){ |
||||
return true; |
||||
}if(StringUtils.equals("fr_write", op) && StringUtils.equals("imp_w_excel_data", cmd)){ |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,63 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.bean; |
||||
|
||||
import com.fr.third.jodd.io.StreamUtil; |
||||
|
||||
import javax.servlet.ReadListener; |
||||
import javax.servlet.ServletInputStream; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletRequestWrapper; |
||||
import java.io.BufferedReader; |
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @date 2021/08/01 |
||||
*/ |
||||
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper { |
||||
private BufferedReader br; |
||||
private byte[] body; |
||||
public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException { |
||||
super(request); |
||||
//this.br = request.getReader();
|
||||
body = StreamUtil.readBytes(request.getInputStream()); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public BufferedReader getReader() throws IOException { |
||||
return new BufferedReader(new InputStreamReader(getInputStream())); |
||||
} |
||||
|
||||
@Override |
||||
public ServletInputStream getInputStream() throws IOException { |
||||
final ByteArrayInputStream bais = new ByteArrayInputStream(body); |
||||
return new ServletInputStream() { |
||||
|
||||
@Override |
||||
public boolean isFinished() { |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isReady() { |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public void setReadListener(ReadListener readListener) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public int read() throws IOException { |
||||
return bais.read(); |
||||
} |
||||
}; |
||||
} |
||||
|
||||
public void setBody(byte[] body) { |
||||
this.body = body; |
||||
} |
||||
} |
@ -0,0 +1,138 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.bean; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @date 2021/8/1 |
||||
*/ |
||||
public class EncryptInfo { |
||||
|
||||
/** |
||||
*应用标识(AppId) |
||||
*/ |
||||
private String app_id; |
||||
|
||||
/** |
||||
*请求时间戳(秒级)//1493468759
|
||||
*/ |
||||
private String time_stamp; |
||||
|
||||
/** |
||||
*随机字符串 |
||||
*/ |
||||
private String nonce_str; |
||||
|
||||
/** |
||||
*签名 |
||||
*/ |
||||
private String sign; |
||||
/** |
||||
*文件名 |
||||
*/ |
||||
private String filename; |
||||
|
||||
/** |
||||
*加解密类型 1:解密 2:加密 |
||||
*/ |
||||
private int type; |
||||
|
||||
/** |
||||
* 是否是ipg加密,1:ipg加密 2:凤凰卫视 3:未知 |
||||
*/ |
||||
private int decrypttype; |
||||
|
||||
/** |
||||
*用户ID |
||||
*/ |
||||
private String userid; |
||||
|
||||
|
||||
/** |
||||
*文件字节 |
||||
*/ |
||||
private String bytes; |
||||
|
||||
/** |
||||
*描述 |
||||
*/ |
||||
private String desc; |
||||
|
||||
public String getApp_id() { |
||||
return app_id; |
||||
} |
||||
|
||||
public void setApp_id(String app_id) { |
||||
this.app_id = app_id; |
||||
} |
||||
|
||||
public String getTime_stamp() { |
||||
return time_stamp; |
||||
} |
||||
|
||||
public void setTime_stamp(String time_stamp) { |
||||
this.time_stamp = time_stamp; |
||||
} |
||||
|
||||
public String getNonce_str() { |
||||
return nonce_str; |
||||
} |
||||
|
||||
public void setNonce_str(String nonce_str) { |
||||
this.nonce_str = nonce_str; |
||||
} |
||||
|
||||
public String getSign() { |
||||
return sign; |
||||
} |
||||
|
||||
public void setSign(String sign) { |
||||
this.sign = sign; |
||||
} |
||||
|
||||
public String getFilename() { |
||||
return filename; |
||||
} |
||||
|
||||
public void setFilename(String filename) { |
||||
this.filename = filename; |
||||
} |
||||
|
||||
public int getDecrypttype() { |
||||
return decrypttype; |
||||
} |
||||
|
||||
public void setDecrypttype(int decrypttype) { |
||||
this.decrypttype = decrypttype; |
||||
} |
||||
|
||||
public String getUserid() { |
||||
return userid; |
||||
} |
||||
|
||||
public void setUserid(String userid) { |
||||
this.userid = userid; |
||||
} |
||||
|
||||
public String getBytes() { |
||||
return bytes; |
||||
} |
||||
|
||||
public void setBytes(String bytes) { |
||||
this.bytes = bytes; |
||||
} |
||||
|
||||
public String getDesc() { |
||||
return desc; |
||||
} |
||||
|
||||
public void setDesc(String desc) { |
||||
this.desc = desc; |
||||
} |
||||
|
||||
public int getType() { |
||||
return type; |
||||
} |
||||
|
||||
public void setType(int type) { |
||||
this.type = type; |
||||
} |
||||
} |
@ -0,0 +1,19 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.bean; |
||||
|
||||
import java.io.OutputStream; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
@FunctionalInterface |
||||
public interface ExportFunc { |
||||
/** |
||||
* 写入到临时输出流 |
||||
* |
||||
* @author Zhanying |
||||
* @param os 临时输出流 |
||||
* @return void |
||||
**/ |
||||
void export(OutputStream os) throws Exception; |
||||
} |
@ -0,0 +1,38 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.bean; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @date 2021/8/1 |
||||
*/ |
||||
public class JsonResult implements Serializable { |
||||
private int ret; |
||||
private String msg; |
||||
private Object result; |
||||
|
||||
public int getRet() { |
||||
return ret; |
||||
} |
||||
|
||||
public void setRet(int ret) { |
||||
this.ret = ret; |
||||
} |
||||
|
||||
public String getMsg() { |
||||
return msg; |
||||
} |
||||
|
||||
public void setMsg(String msg) { |
||||
this.msg = msg; |
||||
} |
||||
|
||||
public Object getResult() { |
||||
return result; |
||||
} |
||||
|
||||
public void setResult(Object result) { |
||||
this.result = result; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,79 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.bean; |
||||
|
||||
import javax.servlet.ServletOutputStream; |
||||
import javax.servlet.WriteListener; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import javax.servlet.http.HttpServletResponseWrapper; |
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.DataOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.OutputStream; |
||||
|
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/8/1 |
||||
*/ |
||||
public class ResponseWrapperImpl extends HttpServletResponseWrapper { |
||||
ByteArrayOutputStream output; |
||||
FilterServletOutputStream filterOutput; |
||||
|
||||
public ResponseWrapperImpl(HttpServletResponse response) { |
||||
super(response); |
||||
output = new ByteArrayOutputStream(); |
||||
} |
||||
|
||||
@Override |
||||
public ServletOutputStream getOutputStream() throws IOException { |
||||
if (filterOutput == null) { |
||||
filterOutput = new FilterServletOutputStream(output); |
||||
} |
||||
return filterOutput; |
||||
} |
||||
|
||||
public void clear(){ |
||||
output = new ByteArrayOutputStream(); |
||||
filterOutput = new FilterServletOutputStream(output); |
||||
} |
||||
public ByteArrayOutputStream getOut(){ |
||||
return output; |
||||
} |
||||
|
||||
public byte[] getDataStream() { |
||||
return output.toByteArray(); |
||||
} |
||||
|
||||
class FilterServletOutputStream extends ServletOutputStream { |
||||
DataOutputStream output; |
||||
|
||||
public FilterServletOutputStream(OutputStream output) { |
||||
this.output = new DataOutputStream(output); |
||||
} |
||||
|
||||
@Override |
||||
public void write(int arg0) throws IOException { |
||||
output.write(arg0); |
||||
} |
||||
|
||||
@Override |
||||
public void write(byte[] arg0, int arg1, int arg2) throws IOException { |
||||
output.write(arg0, arg1, arg2); |
||||
} |
||||
|
||||
@Override |
||||
public void write(byte[] arg0) throws IOException { |
||||
output.write(arg0); |
||||
System.out.printf(""); |
||||
} |
||||
|
||||
@Override |
||||
public boolean isReady() { |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public void setWriteListener(WriteListener writeListener) { |
||||
|
||||
} |
||||
} |
||||
} |
@ -0,0 +1,45 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.data; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.general.IOUtils; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.xxxx.report.ecode.data.fun.AbstractExcelDealFile; |
||||
import com.fr.plugin.xxxx.report.ecode.util.DecodeUtil; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileOutputStream; |
||||
import java.io.InputStream; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class DealExcelFileImp extends AbstractExcelDealFile { |
||||
|
||||
public DealExcelFileImp(String type, Boolean version2007) { |
||||
super(type, version2007); |
||||
} |
||||
|
||||
public DealExcelFileImp() { |
||||
super(); |
||||
} |
||||
|
||||
@Override |
||||
public File encodeFile(String sfilePath, UserBean current,String reportId) { |
||||
String cp = sfilePath + ".bak"; |
||||
FineLoggerFactory.getLogger().info("encode file path is {}",cp); |
||||
try { |
||||
InputStream inputStream = DecodeUtil.encryptFileByFh(new FileInputStream(sfilePath), "test"); |
||||
FileOutputStream fileOutputStream = new FileOutputStream(cp); |
||||
fileOutputStream.write(IOUtils.inputStream2Bytes(inputStream)); |
||||
fileOutputStream.flush(); |
||||
fileOutputStream.close(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
//FileDlpUtil.encryptFile(sfilePath,cp);
|
||||
new File(sfilePath).delete(); |
||||
return new File(cp); |
||||
} |
||||
} |
@ -0,0 +1,40 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.data; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.general.IOUtils; |
||||
import com.fr.io.exporter.AppExporter; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.xxxx.report.ecode.data.fun.AbstractOthrtFileDeal; |
||||
import com.fr.plugin.xxxx.report.ecode.util.DecodeUtil; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileOutputStream; |
||||
import java.io.InputStream; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class DealOtherFileImp extends AbstractOthrtFileDeal { |
||||
public DealOtherFileImp(AppExporter exporter, String fileSuffix) { |
||||
super(exporter,fileSuffix); |
||||
} |
||||
|
||||
@Override |
||||
public File encodeFile(String sfilePath, UserBean current,String reportId) { |
||||
String cp = sfilePath + ".bak"; |
||||
FineLoggerFactory.getLogger().info("encode file path is {}",cp); |
||||
try { |
||||
InputStream inputStream = DecodeUtil.encryptFileByFh(new FileInputStream(sfilePath), "test"); |
||||
FileOutputStream fileOutputStream = new FileOutputStream(cp); |
||||
fileOutputStream.write(IOUtils.inputStream2Bytes(inputStream)); |
||||
fileOutputStream.flush(); |
||||
fileOutputStream.close(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
new File(sfilePath).delete(); |
||||
return new File(cp); |
||||
} |
||||
} |
@ -0,0 +1,29 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.data; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodePermission { |
||||
private List<String> currentPermission; |
||||
|
||||
private List<String> AdminUsers; |
||||
|
||||
public List<String> getCurrentPermission() { |
||||
return currentPermission; |
||||
} |
||||
|
||||
public void setCurrentPermission(List<String> currentPermission) { |
||||
this.currentPermission = currentPermission; |
||||
} |
||||
|
||||
public List<String> getAdminUsers() { |
||||
return AdminUsers; |
||||
} |
||||
|
||||
public void setAdminUsers(List<String> adminUsers) { |
||||
AdminUsers = adminUsers; |
||||
} |
||||
} |
@ -0,0 +1,148 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.data.fun; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.general.ComparatorUtils; |
||||
import com.fr.io.exporter.*; |
||||
import com.fr.io.exporter.excel.stream.StreamExcel2007Exporter; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.main.workbook.ResultWorkBook; |
||||
import com.fr.report.core.ReportUtils; |
||||
import com.fr.workspace.WorkContext; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.InputStream; |
||||
import java.util.Map; |
||||
|
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public abstract class AbstractExcelDealFile implements DealFile { |
||||
public static String folderPath = WorkContext.getCurrent().getPath() + "/file/"; |
||||
private String type; |
||||
private Boolean version2007; |
||||
|
||||
public AbstractExcelDealFile(String type, Boolean version2007) { |
||||
this(); |
||||
this.type = type; |
||||
this.version2007 = version2007; |
||||
} |
||||
|
||||
public AbstractExcelDealFile() { |
||||
init(); |
||||
} |
||||
|
||||
/** |
||||
* 初始化 |
||||
*/ |
||||
private void init() { |
||||
File file = new File(folderPath.substring(0, folderPath.length() - 1)); |
||||
if ((!file.exists()) || (!file.isDirectory())) { |
||||
file.mkdir(); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public File encodeFile(ResultWorkBook resultWorkBook, boolean b, int[] ints, UserBean current, String reportId) { |
||||
AbstractExcelExporter excelExporter = getEmmableExporter(resultWorkBook); |
||||
FileOutputStream out = null; |
||||
String filePath = folderPath + System.currentTimeMillis() + fileNamePostfix(); |
||||
File excelFile = null; |
||||
try { |
||||
excelFile = new File(filePath); |
||||
out = new FileOutputStream(excelFile); |
||||
//先后台导出本地的excel,需要对这个excel进行加密得到新的excel
|
||||
excelExporter.export(out, resultWorkBook); |
||||
out.flush(); |
||||
out.close(); |
||||
excelFile = null; |
||||
File newFile = encodeFile(filePath,current,reportId); |
||||
//返回加密后的文件
|
||||
return newFile; |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
|
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 真正文件加密 |
||||
* |
||||
* @param sfilePath |
||||
* @param current |
||||
* @param reportId |
||||
* @return |
||||
*/ |
||||
public abstract File encodeFile(String sfilePath, UserBean current, String reportId); |
||||
|
||||
@Override |
||||
public File decodeFile(InputStream inputStream, String s, Map<String, Object> map) { |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 得到原来应该导出exporter |
||||
* |
||||
* @return |
||||
*/ |
||||
public AbstractExcelExporter<Boolean> getEmmableExporter(ResultWorkBook resultWorkBook) { |
||||
if (ComparatorUtils.equalsIgnoreCase("simple", getType())) { |
||||
if (getVersion2007()) { |
||||
return new StreamExcel2007Exporter(); |
||||
|
||||
} else { |
||||
ExcelExporter excel = new ExcelExporter(); |
||||
excel.setVersion(true); |
||||
return excel; |
||||
} |
||||
} else if (ComparatorUtils.equalsIgnoreCase("sheet", getType())) { |
||||
if (getVersion2007()) { |
||||
return new PageToSheetExcel2007Exporter(ReportUtils.getPaperSettingListFromWorkBook(resultWorkBook)); |
||||
} else { |
||||
return new PageToSheetExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(resultWorkBook)); |
||||
} |
||||
} else if (ComparatorUtils.equalsIgnoreCase("page", getType())) { |
||||
if (getVersion2007()) { |
||||
return new PageExcel2007Exporter(ReportUtils.getPaperSettingListFromWorkBook(resultWorkBook)); |
||||
} else { |
||||
PageExcelExporter page = new PageExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(resultWorkBook)); |
||||
page.setVersion(true); |
||||
return page; |
||||
} |
||||
} |
||||
|
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 获取文件名后缀,导出到服务器的时候 |
||||
* |
||||
* @return |
||||
*/ |
||||
protected String fileNamePostfix() { |
||||
if (!getVersion2007()) { |
||||
return ".xls"; |
||||
} else { |
||||
return ".xlsx"; |
||||
} |
||||
} |
||||
|
||||
public String getType() { |
||||
return type; |
||||
} |
||||
|
||||
public void setType(String type) { |
||||
this.type = type; |
||||
} |
||||
|
||||
public Boolean getVersion2007() { |
||||
return version2007; |
||||
} |
||||
|
||||
public void setVersion2007(Boolean version2007) { |
||||
this.version2007 = version2007; |
||||
} |
||||
} |
@ -0,0 +1,79 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.data.fun; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.io.exporter.AppExporter; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.main.workbook.ResultWorkBook; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.InputStream; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public abstract class AbstractOthrtFileDeal implements DealFile { |
||||
private AppExporter exporter; |
||||
private String fileSuffix; |
||||
|
||||
public AbstractOthrtFileDeal() { |
||||
init(); |
||||
} |
||||
|
||||
public AbstractOthrtFileDeal(AppExporter exporter, String fileSuffix) { |
||||
init(); |
||||
this.exporter = exporter; |
||||
this.fileSuffix = fileSuffix; |
||||
} |
||||
|
||||
@Override |
||||
public File encodeFile(ResultWorkBook resultWorkBook, boolean b, int[] ints, UserBean current, String reportId) { |
||||
FileOutputStream out = null; |
||||
String filePath = AbstractExcelDealFile.folderPath + System.currentTimeMillis() + this.getFileSuffix(); |
||||
try { |
||||
out = new FileOutputStream(filePath); |
||||
this.getExporter().export(out,resultWorkBook); |
||||
out.flush(); |
||||
out.close(); |
||||
File newFile=encodeFile(filePath,current,reportId); |
||||
return newFile; |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
return null; |
||||
} |
||||
protected abstract File encodeFile(String sfilePath, UserBean current, String reportId); |
||||
|
||||
|
||||
@Override |
||||
public File decodeFile(InputStream inputStream, String s, Map<String, Object> map) { |
||||
return null; |
||||
} |
||||
/** |
||||
* 初始化 |
||||
*/ |
||||
private void init(){ |
||||
File file=new File(AbstractExcelDealFile.folderPath.substring(0,AbstractExcelDealFile.folderPath.length()-1)); |
||||
if((!file.exists())||(!file.isDirectory())){ |
||||
file.mkdir(); |
||||
} |
||||
} |
||||
|
||||
public AppExporter getExporter() { |
||||
return exporter; |
||||
} |
||||
|
||||
public void setExporter(AppExporter exporter) { |
||||
this.exporter = exporter; |
||||
} |
||||
|
||||
public String getFileSuffix() { |
||||
return fileSuffix; |
||||
} |
||||
|
||||
public void setFileSuffix(String fileSuffix) { |
||||
this.fileSuffix = fileSuffix; |
||||
} |
||||
} |
@ -0,0 +1,34 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.data.fun; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.main.workbook.ResultWorkBook; |
||||
|
||||
import java.io.File; |
||||
import java.io.InputStream; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public interface DealFile { |
||||
/** |
||||
* 加密文件 |
||||
* @param resultWorkBook |
||||
* @param b |
||||
* @param ints |
||||
* @param current |
||||
* @param reportId |
||||
* @return |
||||
*/ |
||||
File encodeFile(ResultWorkBook resultWorkBook, boolean b, int[] ints, UserBean current, String reportId); |
||||
|
||||
/** |
||||
* 解密文件 |
||||
* @param inputStream |
||||
* @param s |
||||
* @param map |
||||
* @return |
||||
*/ |
||||
File decodeFile(InputStream inputStream, String s, Map<String, Object> map); |
||||
} |
@ -0,0 +1,47 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.export; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.io.exporter.ExcelExporter; |
||||
import com.fr.main.workbook.ResultWorkBook; |
||||
import com.fr.plugin.xxxx.report.ecode.data.DealExcelFileImp; |
||||
import com.fr.plugin.xxxx.report.ecode.data.fun.DealFile; |
||||
import com.fr.plugin.xxxx.report.ecode.utils.IEUtils; |
||||
|
||||
import java.io.File; |
||||
import java.io.OutputStream; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodeExcelExporter extends ExcelExporter { |
||||
|
||||
private UserBean current; |
||||
private String reportId; |
||||
public EncodeExcelExporter(List list, UserBean userBean, String reportId) { |
||||
super(list); |
||||
this.current = userBean; |
||||
this.reportId = reportId; |
||||
} |
||||
|
||||
@Override |
||||
public void export(OutputStream outputStream, ResultWorkBook resultWorkBook, boolean b, int[] ints) throws Exception { |
||||
DealFile dealFile = getDeal(outputStream, resultWorkBook, b, ints); |
||||
File file = dealFile.encodeFile(resultWorkBook, b, ints,current, reportId); |
||||
if (file == null) { |
||||
super.export(outputStream, resultWorkBook, b, ints); |
||||
return; |
||||
} |
||||
IEUtils.exportFile(outputStream, file); |
||||
} |
||||
|
||||
|
||||
public DealFile getDeal(OutputStream outputStream, ResultWorkBook resultWorkBook, boolean b, int[] ints) { |
||||
DealExcelFileImp dealFile = new DealExcelFileImp(); |
||||
dealFile.setType("simple"); |
||||
dealFile.setVersion2007(checkExcelExportVersion()); |
||||
return dealFile; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,44 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.export; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.io.exporter.ImageExporter; |
||||
import com.fr.main.workbook.ResultWorkBook; |
||||
import com.fr.plugin.xxxx.report.ecode.data.DealOtherFileImp; |
||||
import com.fr.plugin.xxxx.report.ecode.utils.IEUtils; |
||||
|
||||
import java.io.File; |
||||
import java.io.OutputStream; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodeImageExporter extends ImageExporter { |
||||
private String type; |
||||
private UserBean current; |
||||
private String reportId; |
||||
public EncodeImageExporter(String s, UserBean current, String reportId) { |
||||
this.setType(s); |
||||
this.current = current; |
||||
this.reportId = reportId; |
||||
} |
||||
|
||||
@Override |
||||
public void export(OutputStream outputStream, ResultWorkBook resultWorkBook) throws Exception { |
||||
DealOtherFileImp imp = new DealOtherFileImp(new ImageExporter(this.getType(), 96), "." + this.getType()); |
||||
File newFile = imp.encodeFile(resultWorkBook, false, null, current, reportId); |
||||
if (newFile == null) { |
||||
super.export(outputStream, resultWorkBook); |
||||
return; |
||||
} |
||||
IEUtils.exportFile(outputStream, newFile); |
||||
} |
||||
|
||||
public String getType() { |
||||
return type; |
||||
} |
||||
|
||||
public void setType(String type) { |
||||
this.type = type; |
||||
} |
||||
} |
@ -0,0 +1,46 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.export; |
||||
|
||||
import com.fr.general.IOUtils; |
||||
import com.fr.io.exporter.LargeDataPageExcelExporter; |
||||
import com.fr.main.workbook.ResultWorkBook; |
||||
import com.fr.plugin.xxxx.report.ecode.util.DecodeUtil; |
||||
import com.fr.workspace.WorkContext; |
||||
|
||||
import java.io.*; |
||||
import java.util.List; |
||||
import java.util.UUID; |
||||
|
||||
/** |
||||
* @Author fr.open |
||||
* @Date 2021/10/20 |
||||
* @Description |
||||
**/ |
||||
public class EncodeLargeDataPageExcelExporter extends LargeDataPageExcelExporter { |
||||
public static String folderPath = WorkContext.getCurrent().getPath() + "/file/"; |
||||
|
||||
private String filename; |
||||
|
||||
public EncodeLargeDataPageExcelExporter(List list, boolean page, String filename) { |
||||
super(list, page); |
||||
this.filename = filename; |
||||
} |
||||
|
||||
@Override |
||||
public void export(OutputStream outputStream, ResultWorkBook resultWorkBook) throws Exception { |
||||
String path = folderPath + UUID.randomUUID(); |
||||
File temp = new File(path); |
||||
if (!temp.exists()) { |
||||
temp.createNewFile(); |
||||
} |
||||
FileOutputStream out = new FileOutputStream(temp); |
||||
super.export(out, resultWorkBook); |
||||
out.flush(); |
||||
out.close(); |
||||
InputStream inputStream = DecodeUtil.encryptFileByFh(new FileInputStream(path), filename); |
||||
outputStream.write(IOUtils.inputStream2Bytes(inputStream)); |
||||
outputStream.flush(); |
||||
outputStream.close(); |
||||
temp.delete(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,49 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.export; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.io.exporter.PDFExporter; |
||||
import com.fr.main.workbook.ResultWorkBook; |
||||
import com.fr.plugin.xxxx.report.ecode.data.DealOtherFileImp; |
||||
import com.fr.plugin.xxxx.report.ecode.utils.IEUtils; |
||||
|
||||
import java.io.File; |
||||
import java.io.OutputStream; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodePDFExporter extends PDFExporter { |
||||
private UserBean current; |
||||
private String reportId; |
||||
public EncodePDFExporter(UserBean current, String reportId) { |
||||
this.current = current; |
||||
this.reportId = reportId; |
||||
} |
||||
|
||||
/** |
||||
* 重写export |
||||
* |
||||
* @param outputStream |
||||
* @param resultWorkBook |
||||
* @throws Exception |
||||
*/ |
||||
@Override |
||||
public void export(OutputStream outputStream, ResultWorkBook resultWorkBook) throws Exception { |
||||
/** |
||||
* 这边自己定义的一个接口,利用默认的PDFExporter类导出文件到服务器一个缓存目录下面, |
||||
* 并且对文件进行加密处理,返回该文件对象 |
||||
* 关于如何导出文件到服务器上面可以查看帆软的帮助文档: |
||||
* 导出API-https://help.finereport.com/doc-view-735.html
|
||||
*/ |
||||
DealOtherFileImp imp = new DealOtherFileImp(new PDFExporter(), ".pdf"); |
||||
File newFile = imp.encodeFile(resultWorkBook, false, null, current, reportId); |
||||
if (newFile == null) { |
||||
//如果加密过的文件是null,那么还是走默认的逻辑
|
||||
super.export(outputStream, resultWorkBook); |
||||
return; |
||||
} |
||||
//利用新的文件进行相应返回
|
||||
IEUtils.exportFile(outputStream, newFile); |
||||
} |
||||
} |
@ -0,0 +1,44 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.export; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.io.exporter.PageExcelExporter; |
||||
import com.fr.main.workbook.ResultWorkBook; |
||||
import com.fr.plugin.xxxx.report.ecode.data.DealExcelFileImp; |
||||
import com.fr.plugin.xxxx.report.ecode.data.fun.DealFile; |
||||
import com.fr.plugin.xxxx.report.ecode.utils.IEUtils; |
||||
|
||||
import java.io.File; |
||||
import java.io.OutputStream; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodePageExcelExporter extends PageExcelExporter { |
||||
private UserBean current; |
||||
private String reportId; |
||||
public EncodePageExcelExporter(List list, UserBean current, String reportId) { |
||||
super(list); |
||||
this.current =current; |
||||
this.reportId = reportId; |
||||
} |
||||
|
||||
@Override |
||||
public void export(OutputStream outputStream, ResultWorkBook resultWorkBook, boolean b, int[] ints) throws Exception { |
||||
DealFile dealFile = getDeal(outputStream, resultWorkBook, b, ints); |
||||
File file = dealFile.encodeFile(resultWorkBook, b, ints, current, reportId); |
||||
if (file == null) { |
||||
super.export(outputStream, resultWorkBook, b, ints); |
||||
return; |
||||
} |
||||
IEUtils.exportFile(outputStream, file); |
||||
} |
||||
|
||||
public DealFile getDeal(OutputStream outputStream, ResultWorkBook resultWorkBook, boolean b, int[] ints) { |
||||
DealExcelFileImp dealFile = new DealExcelFileImp(); |
||||
dealFile.setType("page"); |
||||
dealFile.setVersion2007(checkExcelExportVersion()); |
||||
return dealFile; |
||||
} |
||||
} |
@ -0,0 +1,45 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.export; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.io.exporter.PageToSheetExcelExporter; |
||||
import com.fr.main.workbook.ResultWorkBook; |
||||
import com.fr.plugin.xxxx.report.ecode.data.DealExcelFileImp; |
||||
import com.fr.plugin.xxxx.report.ecode.data.fun.DealFile; |
||||
import com.fr.plugin.xxxx.report.ecode.utils.IEUtils; |
||||
|
||||
import java.io.File; |
||||
import java.io.OutputStream; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodePageToSheetExcelExporter extends PageToSheetExcelExporter { |
||||
|
||||
private UserBean current; |
||||
private String reportId; |
||||
public EncodePageToSheetExcelExporter(List list, UserBean current, String reportId) { |
||||
super(list); |
||||
this.current =current; |
||||
this.reportId = reportId; |
||||
} |
||||
|
||||
@Override |
||||
public void export(OutputStream outputStream, ResultWorkBook resultWorkBook, boolean b, int[] ints) throws Exception { |
||||
DealFile dealFile = getDeal(outputStream, resultWorkBook, b, ints); |
||||
File file = dealFile.encodeFile(resultWorkBook, b, ints, current, reportId); |
||||
if (file == null) { |
||||
super.export(outputStream, resultWorkBook, b, ints); |
||||
return; |
||||
} |
||||
IEUtils.exportFile(outputStream, file); |
||||
} |
||||
|
||||
public DealFile getDeal(OutputStream outputStream, ResultWorkBook resultWorkBook, boolean b, int[] ints) { |
||||
DealExcelFileImp dealFile = new DealExcelFileImp(); |
||||
dealFile.setType("sheet"); |
||||
dealFile.setVersion2007(checkExcelExportVersion()); |
||||
return dealFile; |
||||
} |
||||
} |
@ -0,0 +1,34 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.export; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.io.exporter.WordExporter; |
||||
import com.fr.main.workbook.ResultWorkBook; |
||||
import com.fr.plugin.xxxx.report.ecode.data.DealOtherFileImp; |
||||
import com.fr.plugin.xxxx.report.ecode.utils.IEUtils; |
||||
|
||||
import java.io.File; |
||||
import java.io.OutputStream; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodeWordEcporter extends WordExporter { |
||||
private UserBean current; |
||||
private String reportId; |
||||
public EncodeWordEcporter(UserBean current, String reportId) { |
||||
this.current = current; |
||||
this.reportId = reportId; |
||||
} |
||||
|
||||
@Override |
||||
public void export(OutputStream outputStream, ResultWorkBook resultWorkBook) throws Exception { |
||||
DealOtherFileImp imp = new DealOtherFileImp(new WordExporter(), ".doc"); |
||||
File newFile = imp.encodeFile(resultWorkBook, false, null, current,reportId); |
||||
if (newFile == null) { |
||||
super.export(outputStream, resultWorkBook); |
||||
return; |
||||
} |
||||
IEUtils.exportFile(outputStream, newFile); |
||||
} |
||||
} |
@ -0,0 +1,71 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.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.plugin.xxxx.report.ecode.bean.BodyReaderHttpServletRequestWrapper; |
||||
import com.fr.plugin.xxxx.report.ecode.util.DecodeUtil; |
||||
|
||||
import javax.servlet.FilterChain; |
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.InputStream; |
||||
import java.io.UnsupportedEncodingException; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class DecodeFilter { |
||||
private static final byte[] NEW_LINE_BYTES = new byte[]{13, 10}; |
||||
private static final byte[] BOUNDARY_END = new byte[]{45, 45}; |
||||
|
||||
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) { |
||||
try { |
||||
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)); |
||||
FineLoggerFactory.getLogger().info("record file request url is {}",req.getRequestURI()+req.getQueryString()); |
||||
String filename = parseResult.getFileName(); |
||||
InputStream decode = DecodeUtil.decryptFileByFh(fileDecorator, filename); |
||||
if (decode != null) { |
||||
FineLoggerFactory.getLogger().info("file {} is encode file ",filename); |
||||
wrapper.setBody(handleBody(req,CommonIOUtils.inputStream2Bytes(decode), parseResult, req.getCharacterEncoding() == null ? ServerConfig.getInstance().getServerCharset() : req.getCharacterEncoding())); |
||||
}else { |
||||
FineLoggerFactory.getLogger().info("file {} is no encode file ",filename); |
||||
} |
||||
filterChain.doFilter(wrapper, res); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
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(HttpServletRequest req, 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(req.getContentType()).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(start, body), end); |
||||
return newBody; |
||||
} |
||||
} |
@ -0,0 +1,122 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.operate; |
||||
|
||||
import com.fr.base.ExcelUtils; |
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.general.DeclareRecordType; |
||||
import com.fr.general.ReportDeclareRecordType; |
||||
import com.fr.io.collection.ExportCollection; |
||||
import com.fr.io.exporter.AppExporter; |
||||
import com.fr.io.exporter.ExcelExportType; |
||||
import com.fr.io.exporter.LargeDataPageExcelExporter; |
||||
import com.fr.main.FineBook; |
||||
import com.fr.plugin.xxxx.report.ecode.export.EncodeExcelExporter; |
||||
import com.fr.plugin.xxxx.report.ecode.export.EncodeLargeDataPageExcelExporter; |
||||
import com.fr.plugin.xxxx.report.ecode.export.EncodePageExcelExporter; |
||||
import com.fr.plugin.xxxx.report.ecode.export.EncodePageToSheetExcelExporter; |
||||
import com.fr.plugin.xxxx.report.ecode.utils.PrintUtils; |
||||
import com.fr.report.ExtraReportClassManager; |
||||
import com.fr.report.core.ReportUtils; |
||||
import com.fr.report.fun.ExcelExportAppProvider; |
||||
import com.fr.stable.web.SessionProvider; |
||||
import com.fr.web.Browser; |
||||
import com.fr.web.core.reserve.ExcelOperate; |
||||
import com.fr.web.core.utils.ExportUtils; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.util.Iterator; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodeExcelOperate extends ExcelOperate { |
||||
|
||||
private UserBean current; |
||||
|
||||
private String reportId; |
||||
|
||||
@Override |
||||
public AppExporter<Boolean> createExcelExporter(ExportCollection exportCollection, ExcelExportType excelExportType, SessionProvider sessionProvider) { |
||||
Set providerSet = ExtraReportClassManager.getInstance().getArray("ExcelExportAppProvider"); |
||||
Iterator it = providerSet.iterator(); |
||||
|
||||
ExcelExportAppProvider provider; |
||||
do { |
||||
if (!it.hasNext()) { |
||||
FineBook book = (FineBook) sessionProvider.getOriginalObject(); |
||||
Object exporter; |
||||
if ("simple".equalsIgnoreCase(excelExportType.getExportType())) { |
||||
exporter = new EncodeExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(book),current,reportId); |
||||
exportCollection.setExporter((AppExporter) exporter); |
||||
exportCollection.setRecordType(DeclareRecordType.EXPORT_TYPE_EXCEL_ORIGINAL); |
||||
} else if ("sheet".equalsIgnoreCase(excelExportType.getExportType())) { |
||||
exporter = new EncodePageToSheetExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(book),current,reportId); |
||||
exportCollection.setExporter((AppExporter) exporter); |
||||
exportCollection.setRecordType(DeclareRecordType.EXPORT_TYPE_EXCEL_PAGESHEET); |
||||
} else { |
||||
exporter = new EncodePageExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(book),current,reportId); |
||||
exportCollection.setExporter((AppExporter) exporter); |
||||
exportCollection.setRecordType(DeclareRecordType.EXPORT_TYPE_EXCEL_PAGE); |
||||
} |
||||
|
||||
return (AppExporter) exporter; |
||||
} |
||||
|
||||
provider = (ExcelExportAppProvider) it.next(); |
||||
} while (!provider.exportType().equalsIgnoreCase(excelExportType.getExportType())); |
||||
|
||||
return provider.newAppExporter(exportCollection, excelExportType, sessionProvider); |
||||
} |
||||
|
||||
@Override |
||||
public ExportCollection createExcelExportCollection(HttpServletRequest req, HttpServletResponse res, SessionProvider provider, String fileName) { |
||||
ExcelExportType exportType = this.createExcelExportType(req, provider); |
||||
if ("ldpage".equalsIgnoreCase(exportType.getExportType())) { |
||||
Set set = ExtraReportClassManager.getInstance().getArray("ExcelExportAppProvider"); |
||||
Iterator it =set.iterator(); |
||||
|
||||
while(it.hasNext()) { |
||||
ExcelExportAppProvider excelExportAppProvider = (ExcelExportAppProvider)it.next(); |
||||
if (excelExportAppProvider.exportType().equalsIgnoreCase(exportType.getExportType())) { |
||||
ExportCollection collection = excelExportAppProvider.newLargeDataExportCollection(req, res, provider, fileName, exportType); |
||||
if (collection != null) { |
||||
return collection; |
||||
} |
||||
} |
||||
} |
||||
return this.createLargeDataExportCollection(req, res, provider, fileName, exportType); |
||||
} else { |
||||
if (res != null) { |
||||
if (ExcelUtils.checkThirdJarSupportPOI() && !WebUtils.getHTTPRequestBoolParameter(req, "isExcel2003")) { |
||||
PrintUtils.setExcel2007Content(res, fileName); |
||||
} else { |
||||
PrintUtils.setExcelContent(res, fileName); |
||||
} |
||||
|
||||
} |
||||
this.current = PrintUtils.getUserById(req); |
||||
this.reportId = PrintUtils.getReportId(req); |
||||
ExportCollection exportCollection = ExportCollection.create(); |
||||
AppExporter exporter = this.createExcelExporter(exportCollection, exportType, provider); |
||||
exporter.setVersion(WebUtils.getHTTPRequestBoolParameter(req, "isExcel2003")); |
||||
return exportCollection; |
||||
} |
||||
} |
||||
@Override |
||||
public ExportCollection createLargeDataExportCollection(HttpServletRequest req, HttpServletResponse res, SessionProvider sessionProvider, String filename, ExcelExportType excelExportType) { |
||||
Browser browser = Browser.resolve(req); |
||||
if (res != null) { |
||||
ExportUtils.setZipContext(res, filename, browser.shouldSetContentTypeOnZipDownload()); |
||||
} |
||||
|
||||
FineBook book = sessionProvider.getOriginalObject(); |
||||
LargeDataPageExcelExporter largeDataPageExcelExporter = new EncodeLargeDataPageExcelExporter(ReportUtils.getPaperSettingListFromWorkBook(book), excelExportType.isPage(),filename); |
||||
ExportCollection exportCollection = ExportCollection.create(); |
||||
exportCollection.setExporter(largeDataPageExcelExporter); |
||||
exportCollection.setRecordType(ReportDeclareRecordType.EXPORT_TYPE_EXCEL_LARGE); |
||||
return exportCollection; |
||||
} |
||||
} |
@ -0,0 +1,51 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.operate; |
||||
|
||||
import com.fr.general.DeclareRecordType; |
||||
import com.fr.io.collection.ExportCollection; |
||||
import com.fr.io.exporter.ImageExporter; |
||||
import com.fr.plugin.xxxx.report.ecode.export.EncodeImageExporter; |
||||
import com.fr.plugin.xxxx.report.ecode.utils.PrintUtils; |
||||
import com.fr.web.core.ReportSessionIDInfor; |
||||
import com.fr.web.core.reserve.DefaultOperate; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodeImageOperate extends DefaultOperate { |
||||
@Override |
||||
public void setContent(HttpServletRequest req, HttpServletResponse res, String fileName, boolean var4) { |
||||
DeclareRecordType type = this.getImageExportType(req); |
||||
PrintUtils.setImageContext(res, fileName, type.getTypeString()); |
||||
} |
||||
@Override |
||||
public ExportCollection createCollection(HttpServletRequest req, HttpServletResponse res, ReportSessionIDInfor var3, String fileName) { |
||||
ExportCollection exportCollection = ExportCollection.create(); |
||||
DeclareRecordType type = this.getImageExportType(req); |
||||
ImageExporter exporter = new EncodeImageExporter(type.getTypeString(),PrintUtils.getUserById(req),PrintUtils.getReportId(req)); |
||||
setContent(req,res,fileName,false); |
||||
exportCollection.setExporter(exporter); |
||||
exportCollection.setRecordType(type); |
||||
return exportCollection; |
||||
} |
||||
|
||||
private DeclareRecordType getImageExportType(HttpServletRequest var1) { |
||||
DeclareRecordType[] var2 = new DeclareRecordType[]{DeclareRecordType.EXPORT_TYPE_IMAGE_PNG, DeclareRecordType.EXPORT_TYPE_IMAGE_JPG, DeclareRecordType.EXPORT_TYPE_IMAGE_GIF, DeclareRecordType.EXPORT_TYPE_IMAGE_BMP, DeclareRecordType.EXPORT_TYPE_IMAGE_WBMP}; |
||||
String var3 = WebUtils.getHTTPRequestParameter(var1, "extype"); |
||||
int var4 = 0; |
||||
if (var3 != null) { |
||||
for(int var5 = 0; var5 < var2.length; ++var5) { |
||||
if (var3.equalsIgnoreCase(var2[var5].getTypeString())) { |
||||
var4 = var5; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
return var2[var4]; |
||||
} |
||||
} |
@ -0,0 +1,47 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.operate; |
||||
|
||||
import com.fr.general.DeclareRecordType; |
||||
import com.fr.io.collection.ExportCollection; |
||||
import com.fr.plugin.xxxx.report.ecode.export.EncodePDFExporter; |
||||
import com.fr.plugin.xxxx.report.ecode.utils.PrintUtils; |
||||
import com.fr.web.core.ReportSessionIDInfor; |
||||
import com.fr.web.core.reserve.DefaultOperate; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodePDFOperate extends DefaultOperate { |
||||
/** |
||||
* 返回一个带有EncodePDFExporter导出的ExportCollection |
||||
* @param req |
||||
* @param res |
||||
* @param reportSessionIDInfor |
||||
* @param fileName |
||||
* @return |
||||
*/ |
||||
@Override |
||||
public ExportCollection createCollection(HttpServletRequest req, HttpServletResponse res, ReportSessionIDInfor reportSessionIDInfor, String fileName) { |
||||
ExportCollection exportCollection = ExportCollection.create(); |
||||
exportCollection.setExporter(new EncodePDFExporter(PrintUtils.getUserById(req),PrintUtils.getReportId(req))); |
||||
exportCollection.setRecordType(DeclareRecordType.EXPORT_TYPE_PDF); |
||||
this.setContent(req, res, fileName, false); |
||||
return exportCollection; |
||||
} |
||||
|
||||
/** |
||||
* 设置响应的类型,设置文件名等 |
||||
* @param req |
||||
* @param res |
||||
* @param fileName |
||||
* @param isEmbed |
||||
*/ |
||||
@Override |
||||
public void setContent(HttpServletRequest req, HttpServletResponse res, String fileName, boolean isEmbed) { |
||||
//直接调用工具方法
|
||||
PrintUtils.setPDFContent(res, fileName, isEmbed); |
||||
} |
||||
} |
@ -0,0 +1,31 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.operate; |
||||
|
||||
import com.fr.general.DeclareRecordType; |
||||
import com.fr.io.collection.ExportCollection; |
||||
import com.fr.plugin.xxxx.report.ecode.export.EncodeWordEcporter; |
||||
import com.fr.plugin.xxxx.report.ecode.utils.PrintUtils; |
||||
import com.fr.web.core.ReportSessionIDInfor; |
||||
import com.fr.web.core.reserve.DefaultOperate; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class EncodeWordOperate extends DefaultOperate { |
||||
@Override |
||||
public ExportCollection createCollection(HttpServletRequest req, HttpServletResponse res, ReportSessionIDInfor reportSessionIDInfor, String fileName) { |
||||
ExportCollection exportCollection = ExportCollection.create(); |
||||
this.setContent(req,res,fileName,false); |
||||
exportCollection.setExporter(new EncodeWordEcporter(PrintUtils.getUserById(req),PrintUtils.getReportId(req))); |
||||
exportCollection.setRecordType(DeclareRecordType.EXPORT_TYPE_WORD); |
||||
return exportCollection; |
||||
} |
||||
|
||||
@Override |
||||
public void setContent(HttpServletRequest req, HttpServletResponse res, String fileName, boolean b) { |
||||
PrintUtils.setWordConetent(res, fileName); |
||||
} |
||||
} |
@ -0,0 +1,54 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.util; |
||||
|
||||
import com.fr.log.FineLoggerFactory; |
||||
import org.apache.commons.codec.binary.Base64; |
||||
import sun.misc.BASE64Decoder; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @date 2021/8/1 |
||||
*/ |
||||
public class Base64Util { |
||||
|
||||
public static String inputStream2Base(InputStream in) { |
||||
byte[] data = null; |
||||
try { |
||||
ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); |
||||
byte[] buff = new byte[100]; |
||||
int rc = 0; |
||||
while ((rc = in.read(buff, 0, 100)) > 0) { |
||||
swapStream.write(buff, 0, rc); |
||||
} |
||||
data = swapStream.toByteArray(); |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} finally { |
||||
if (in != null) { |
||||
try { |
||||
in.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
||||
return new String(Base64.encodeBase64(data)); |
||||
} |
||||
|
||||
public static InputStream Base2InputStream(String base) { |
||||
byte[] bytes = new byte[0]; |
||||
try { |
||||
bytes = new BASE64Decoder().decodeBuffer(base.trim()); |
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); |
||||
return inputStream; |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
//转化为输入流
|
||||
return null; |
||||
} |
||||
} |
@ -0,0 +1,74 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.util; |
||||
|
||||
import com.fr.log.FineLoggerFactory; |
||||
|
||||
import java.io.*; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @date 2019/6/17 |
||||
*/ |
||||
public class DecodeUtil { |
||||
public static InputStream decryptFileByFh(InputStream encode, String fileName) { |
||||
FineLoggerFactory.getLogger().info("start decode"); |
||||
String str = Base64Util.inputStream2Base(encode); |
||||
FineLoggerFactory.getLogger().info("file Transcoding success"); |
||||
String base = HttpUtil.decode(str, fileName); |
||||
FineLoggerFactory.getLogger().info("file decode finish,base is:{}", base); |
||||
if (base == null) { |
||||
return null; |
||||
} |
||||
return Base64Util.Base2InputStream(base); |
||||
} |
||||
|
||||
public static InputStream encryptFileByFh(InputStream data, String fileName) { |
||||
FineLoggerFactory.getLogger().info("start encode"); |
||||
String str = Base64Util.inputStream2Base(data); |
||||
FineLoggerFactory.getLogger().info("file Transcoding success"); |
||||
String base = HttpUtil.encode(str, fileName); |
||||
FineLoggerFactory.getLogger().info("file encode finish,base is:{}", base); |
||||
if (base == null) { |
||||
return null; |
||||
} |
||||
return Base64Util.Base2InputStream(base); |
||||
} |
||||
|
||||
public static String getTxt(){ |
||||
File file = new File("/Users/hujian/Desktop/111.txt"); |
||||
try (FileReader reader = new FileReader(file); |
||||
BufferedReader br = new BufferedReader(reader)){ |
||||
|
||||
StringBuilder sb = new StringBuilder(); |
||||
String s = ""; |
||||
while((s = br.readLine()) != null){ |
||||
sb.append(s); |
||||
} |
||||
br.close(); |
||||
return sb.toString(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return ""; |
||||
|
||||
} |
||||
/* |
||||
public static void main(String[] args) { |
||||
|
||||
//System.out.println(sb.toString());
|
||||
File newFile = new File("/Users/hujian/Desktop/www2.xlsx"); |
||||
if(newFile.exists()){ |
||||
newFile.delete(); |
||||
} |
||||
newFile.createNewFile(); |
||||
FileOutputStream fileOutputStream = new FileOutputStream(newFile); |
||||
InputStream inputStream = Base64Util.Base2InputStream(sb.toString()); |
||||
fileOutputStream.write(IOUtils.inputStream2Bytes(inputStream)); |
||||
fileOutputStream.flush(); |
||||
fileOutputStream.close(); |
||||
|
||||
}*/ |
||||
|
||||
|
||||
|
||||
|
||||
} |
@ -0,0 +1,115 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.util; |
||||
|
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.xxxx.report.ecode.bean.ExportFunc; |
||||
import com.fr.third.org.apache.commons.io.IOUtils; |
||||
|
||||
import java.io.*; |
||||
import java.util.UUID; |
||||
|
||||
/** |
||||
* 文件工具类 |
||||
* <p> |
||||
* |
||||
* @author fr.open |
||||
* @date 2021/08/01 |
||||
*/ |
||||
public class FileUtil { |
||||
|
||||
/** |
||||
* 文件加密之后再写入指定的输出流 |
||||
* |
||||
* @param file |
||||
* @param stream |
||||
* @return void |
||||
**/ |
||||
public static void handle(File file, OutputStream stream) { |
||||
InputStream is = null; |
||||
try { |
||||
//走一个自定义的加密方法
|
||||
File txt = new File("/Users/hujian/Desktop/111.txt"); |
||||
is = new FileInputStream(txt);//DecodeUtil.encryptFileByFh(new FileInputStream(file), file.getName());
|
||||
//结果文件再写入原始的输出流里
|
||||
IOUtils.copyLarge(is, stream); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} finally { |
||||
if (is != null) { |
||||
try { |
||||
is.close(); |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
if (stream != null) { |
||||
try { |
||||
stream.flush(); |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 加密导出文件后写入原始输出流中 |
||||
* 回调函数式处理 |
||||
* |
||||
* @param outputStream 原始输出流 |
||||
* @param func 临时输出流处理 |
||||
* @return void |
||||
**/ |
||||
public static void handle(OutputStream outputStream, ExportFunc func) { |
||||
File tempFile = null; |
||||
OutputStream os = null; |
||||
//写入到临时文件
|
||||
try { |
||||
tempFile = FileUtil.createTempFile(); |
||||
os = new FileOutputStream(tempFile); |
||||
func.export(os);//直接写到临时文件里边
|
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} finally { |
||||
if (os != null) { |
||||
try { |
||||
os.close(); |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
} |
||||
|
||||
//处理临时文件,写到原始输出流
|
||||
try { |
||||
handle(tempFile, outputStream);//临时加密后写到原始输出流中
|
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} finally { |
||||
if (tempFile != null) { |
||||
deleteFile(tempFile);//最后删除临时文件
|
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 创建临时文件 |
||||
* |
||||
* @return java.io.File |
||||
**/ |
||||
public static File createTempFile() throws IOException { |
||||
return File.createTempFile("tmp" + UUID.randomUUID().toString(), null);//创建一个临时文件
|
||||
} |
||||
|
||||
/** |
||||
* 删除文件 |
||||
* |
||||
* @param file |
||||
* @return boolean |
||||
* @author Zhanying |
||||
**/ |
||||
public static boolean deleteFile(File file) { |
||||
boolean flag = file.exists() && file.isFile() && file.delete(); |
||||
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
|
||||
return flag; |
||||
} |
||||
} |
@ -0,0 +1,137 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.util; |
||||
|
||||
import com.fr.base.PropertiesUtils; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.xxxx.report.ecode.bean.EncryptInfo; |
||||
import com.fr.plugin.xxxx.report.ecode.bean.JsonResult; |
||||
import com.google.gson.Gson; |
||||
import org.apache.http.HttpEntity; |
||||
import org.apache.http.HttpResponse; |
||||
import org.apache.http.client.HttpClient; |
||||
import org.apache.http.client.methods.HttpPost; |
||||
import org.apache.http.entity.ByteArrayEntity; |
||||
import org.apache.http.impl.client.HttpClientBuilder; |
||||
import org.apache.http.util.EntityUtils; |
||||
|
||||
import java.net.URLEncoder; |
||||
import java.util.*; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @date 2021/8/1 |
||||
*/ |
||||
public class HttpUtil { |
||||
static transient Gson gson = new Gson(); |
||||
|
||||
public static String decode(String base, String filename) { |
||||
try { |
||||
|
||||
FineLoggerFactory.getLogger().info("host:{}", PropertiesUtils.getProperties("conf").getProperty("host")); |
||||
String url = PropertiesUtils.getProperties("conf").getProperty("host") + "/lbxdecrypt/ecrypt/decrypt"; |
||||
HttpClient httpClient = HttpClientBuilder.create().build(); |
||||
HttpPost httpPost = new HttpPost(url); |
||||
httpPost.setHeader("Content-Type", "application/json; charset=UTF-8"); |
||||
EncryptInfo EncryptInfo = new EncryptInfo(); |
||||
FineLoggerFactory.getLogger().info("APPID:{}", PropertiesUtils.getProperties("conf").getProperty("APPID")); |
||||
EncryptInfo.setApp_id(PropertiesUtils.getProperties("conf").getProperty("APPID")); |
||||
EncryptInfo.setTime_stamp((Calendar.getInstance().getTimeInMillis() / 1000) + ""); |
||||
EncryptInfo.setNonce_str("90001"); |
||||
String Sign = ""; |
||||
Sign = verify(EncryptInfo); |
||||
EncryptInfo.setSign(Sign); |
||||
EncryptInfo.setFilename(filename); |
||||
EncryptInfo.setType(1); |
||||
EncryptInfo.setBytes(base); |
||||
//用户ID
|
||||
FineLoggerFactory.getLogger().info("userId:{}", PropertiesUtils.getProperties("conf").getProperty("userId")); |
||||
EncryptInfo.setUserid(PropertiesUtils.getProperties("conf").getProperty("userId")); |
||||
EncryptInfo.setDesc(""); |
||||
String jsonString = gson.toJson(EncryptInfo); |
||||
HttpEntity entity = new ByteArrayEntity(jsonString.getBytes("UTF-8")); |
||||
httpPost.setEntity(entity); |
||||
HttpResponse httpResponse = httpClient.execute(httpPost); |
||||
String response = EntityUtils.toString(httpResponse.getEntity()); |
||||
System.out.println(response); |
||||
JsonResult res = new Gson().fromJson(response, JsonResult.class); |
||||
if (res.getRet() == 0) { |
||||
return res.getResult().toString(); |
||||
} else { |
||||
return null; |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
public static String encode(String BytesStr, String filename) { |
||||
HttpClient httpClient = HttpClientBuilder.create().build(); // Use this
|
||||
try { |
||||
HttpPost httpPost = new HttpPost(PropertiesUtils.getProperties("conf").getProperty("host") + "/lbxdecrypt/ecrypt/encrypt"); |
||||
httpPost.setHeader("Content-Type", "application/json; charset=UTF-8"); |
||||
EncryptInfo EncryptInfo = new EncryptInfo(); |
||||
EncryptInfo.setApp_id(PropertiesUtils.getProperties("conf").getProperty("APPID")); |
||||
EncryptInfo.setTime_stamp((Calendar.getInstance().getTimeInMillis() / 1000) + "");//时间戳
|
||||
EncryptInfo.setNonce_str("90001");//随机数
|
||||
String Sign = ""; |
||||
Sign = verify(EncryptInfo); |
||||
EncryptInfo.setSign(Sign);//签名
|
||||
EncryptInfo.setFilename(filename); |
||||
EncryptInfo.setType(2);//加解密类型 1:解密 2:加密
|
||||
EncryptInfo.setDecrypttype(1);//是否是ipg加密,1:ipg加密 2:凤凰卫视 3:未知
|
||||
EncryptInfo.setBytes(BytesStr);//字节
|
||||
EncryptInfo.setUserid(PropertiesUtils.getProperties("conf").getProperty("userId")); |
||||
EncryptInfo.setDesc(""); |
||||
String jsonString = gson.toJson(EncryptInfo); |
||||
System.out.println(); |
||||
HttpEntity entity = new ByteArrayEntity(jsonString.getBytes("UTF-8")); |
||||
httpPost.setEntity(entity); |
||||
// httpPost.setEntity(entity);
|
||||
HttpResponse httpResponse = httpClient.execute(httpPost); |
||||
String response = EntityUtils.toString(httpResponse.getEntity()); |
||||
System.out.println(response); |
||||
JsonResult JsonResult = gson.fromJson(response, JsonResult.class); |
||||
if (JsonResult.getRet() == 0) { |
||||
return JsonResult.getResult().toString(); |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} finally { |
||||
|
||||
} |
||||
return null; |
||||
} |
||||
|
||||
//参数验证方法
|
||||
public static String verify(EncryptInfo encryptInfo) throws Exception { |
||||
String app_id = encryptInfo.getApp_id(); |
||||
String time_stamp = encryptInfo.getTime_stamp(); |
||||
String nonce_str = encryptInfo.getNonce_str(); |
||||
String sign = encryptInfo.getSign(); |
||||
StringBuffer sb = new StringBuffer(); |
||||
SortedMap<Object, Object> parameters = new TreeMap<Object, Object>(); |
||||
String sign2 = null; |
||||
parameters.put("app_id", app_id); |
||||
parameters.put("time_stamp", time_stamp); |
||||
parameters.put("nonce_str", nonce_str); |
||||
//所有参与传参i的参数按照accsii排序(升序)
|
||||
Set es = parameters.entrySet(); |
||||
Iterator it = es.iterator(); |
||||
while (it.hasNext()) { |
||||
Map.Entry entry = (Map.Entry) it.next(); |
||||
String k = (String) entry.getKey(); |
||||
Object v = entry.getValue(); |
||||
if (null != v && !"".equals(v) && !"sign".equals(k) && !"app_key".equals((k))) { |
||||
String value = URLEncoder.encode(v.toString(), "UTF-8"); |
||||
sb.append(k + "=" + value + "&"); |
||||
} else { |
||||
|
||||
} |
||||
} |
||||
FineLoggerFactory.getLogger().info("APPKEY:{}", PropertiesUtils.getProperties("conf").getProperty("APPKEY")); |
||||
sb.append("app_key=" + PropertiesUtils.getProperties("conf").getProperty("APPKEY")); |
||||
//md5编码并大写*/
|
||||
sign2 = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase(); |
||||
return sign2; |
||||
} |
||||
} |
@ -0,0 +1,45 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.util; |
||||
|
||||
import java.security.MessageDigest; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @date 2021/8/1 |
||||
*/ |
||||
public class MD5Util { |
||||
private static String byteArrayToHexString(byte b[]) { |
||||
StringBuffer resultSb = new StringBuffer(); |
||||
for (int i = 0; i < b.length; i++) |
||||
resultSb.append(byteToHexString(b[i])); |
||||
|
||||
return resultSb.toString(); |
||||
} |
||||
|
||||
private static String byteToHexString(byte b) { |
||||
int n = b; |
||||
if (n < 0) |
||||
n += 256; |
||||
int d1 = n / 16; |
||||
int d2 = n % 16; |
||||
return hexDigits[d1] + hexDigits[d2]; |
||||
} |
||||
|
||||
public static String MD5Encode(String origin, String charsetName) { |
||||
String resultString = null; |
||||
try { |
||||
resultString = new String(origin); |
||||
MessageDigest md = MessageDigest.getInstance("MD5"); |
||||
if (charsetName == null || "".equals(charsetName)) |
||||
resultString = byteArrayToHexString(md.digest(resultString |
||||
.getBytes())); |
||||
else |
||||
resultString = byteArrayToHexString(md.digest(resultString |
||||
.getBytes(charsetName))); |
||||
} catch (Exception exception) { |
||||
} |
||||
return resultString; |
||||
} |
||||
|
||||
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", |
||||
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; |
||||
} |
@ -0,0 +1,144 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.utils; |
||||
|
||||
import com.fr.json.JSONObject; |
||||
import com.fr.log.FineLoggerFactory; |
||||
|
||||
import java.io.BufferedReader; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.io.PrintWriter; |
||||
import java.net.URL; |
||||
import java.net.URLConnection; |
||||
import java.util.Iterator; |
||||
import java.util.Map; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class HttpUtil { |
||||
|
||||
/** |
||||
* 发送get请求 |
||||
* @param url |
||||
* @param param |
||||
* @param header |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public static String sendGet(String url, Map<String,String> param, Map<String, String> header){ |
||||
String result = ""; |
||||
BufferedReader in = null; |
||||
String urlNameString = url; |
||||
if( param != null && !param.isEmpty()){ |
||||
urlNameString += "?"; |
||||
urlNameString += param.entrySet() |
||||
.stream() |
||||
.map(entry -> entry.getKey() + "=" + entry.getValue()) |
||||
.collect(Collectors.joining("&")); |
||||
} |
||||
try { |
||||
URL realUrl = new URL(urlNameString); |
||||
// 打开和URL之间的连接
|
||||
URLConnection connection = realUrl.openConnection(); |
||||
//设置超时时间
|
||||
connection.setConnectTimeout(5000); |
||||
connection.setReadTimeout(15000); |
||||
// 设置通用的请求属性
|
||||
if (header != null) { |
||||
Iterator<Map.Entry<String, String>> it = header.entrySet().iterator(); |
||||
while (it.hasNext()) { |
||||
Map.Entry<String, String> entry = it.next(); |
||||
connection.setRequestProperty(entry.getKey(), entry.getValue()); |
||||
} |
||||
} |
||||
connection.setRequestProperty("accept", "*/*"); |
||||
connection.setRequestProperty("connection", "Keep-Alive"); |
||||
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
||||
// 建立实际的连接
|
||||
connection.connect(); |
||||
// 定义 BufferedReader输入流来读取URL的响应,设置utf8防止中文乱码
|
||||
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); |
||||
String line; |
||||
while ((line = in.readLine()) != null) { |
||||
result += line; |
||||
} |
||||
}catch (IOException e){ |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
}finally { |
||||
if (in != null) { |
||||
try { |
||||
in.close(); |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
|
||||
public static String sendPost(String url,Map<String,String> header, JSONObject param) { |
||||
/*if(1 == 1 ){ |
||||
return "{\"responsecode\":\"0\",\"permissionList\":[{\"dsmPerssion\":\"A\"},{\"dsmPerssion\":\"R\"}],\"userList\":[{\"manageUser\":\"12345\"}]}"; |
||||
}*/ |
||||
PrintWriter out = null; |
||||
BufferedReader in = null; |
||||
String result = ""; |
||||
try { |
||||
URL realUrl = new URL(url); |
||||
// 打开和URL之间的连接
|
||||
URLConnection conn = realUrl.openConnection(); |
||||
// 设置通用的请求属性
|
||||
conn.setRequestProperty("accept", "*/*"); |
||||
conn.setRequestProperty("connection", "Keep-Alive"); |
||||
conn.setRequestProperty("user-agent", |
||||
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); |
||||
conn.setRequestProperty("Content-Type","application/json;;charset=UTF-8"); |
||||
if(header != null){ |
||||
header.forEach((k, v) -> { |
||||
conn.setRequestProperty(k, v); |
||||
}); |
||||
} |
||||
// 发送POST请求必须设置如下两行
|
||||
conn.setDoOutput(true); |
||||
conn.setDoInput(true); |
||||
//获取请求头
|
||||
|
||||
// 获取URLConnection对象对应的输出流
|
||||
out = new PrintWriter(conn.getOutputStream()); |
||||
// 发送请求参数
|
||||
out.print(param.toString()); |
||||
// flush输出流的缓冲
|
||||
out.flush(); |
||||
// 定义BufferedReader输入流来读取URL的响应
|
||||
in = new BufferedReader( |
||||
new InputStreamReader(conn.getInputStream())); |
||||
String line; |
||||
while ((line = in.readLine()) != null) { |
||||
result += line; |
||||
} |
||||
return result; |
||||
|
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
//使用finally块来关闭输出流、输入流
|
||||
finally{ |
||||
try{ |
||||
if(out!=null){ |
||||
out.close(); |
||||
} |
||||
if(in!=null){ |
||||
in.close(); |
||||
} |
||||
} |
||||
catch(IOException e){ |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,29 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.utils; |
||||
|
||||
import com.fr.log.FineLoggerFactory; |
||||
|
||||
import java.io.*; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class IEUtils { |
||||
public static void exportFile(OutputStream outputStream, File file) { |
||||
try { |
||||
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file)); |
||||
int len = 0; |
||||
byte[] buffer = new byte[1024]; |
||||
while ((len = bufferedInputStream.read(buffer)) > 0) { |
||||
outputStream.write(buffer, 0, len); |
||||
} |
||||
bufferedInputStream.close(); |
||||
outputStream.flush(); |
||||
outputStream.close(); |
||||
file.delete(); |
||||
} catch (IOException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,112 @@
|
||||
package com.fr.plugin.xxxx.report.ecode.utils; |
||||
|
||||
import com.fr.decision.webservice.bean.user.UserBean; |
||||
import com.fr.decision.webservice.v10.login.LoginService; |
||||
import com.fr.decision.webservice.v10.login.TokenResource; |
||||
import com.fr.decision.webservice.v10.user.UserService; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.web.core.SessionPoolManager; |
||||
import com.fr.web.core.WidgetSessionIDInfor; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Date 2021/08/01 |
||||
*/ |
||||
public class PrintUtils { |
||||
private static final String DSM = ""; |
||||
|
||||
public static void setPDFContent(HttpServletResponse var0, String var1, boolean var2) { |
||||
var0.setContentType("application/pdf"); |
||||
var0.setHeader("extension", "pdf"); |
||||
if (var2) { |
||||
var0.setHeader("Content-disposition", "anyword; filename=" + var1 + ".pdf"+DSM); |
||||
} else { |
||||
var0.setHeader("Content-disposition", "attachment; filename=" + var1 + ".pdf"+DSM); |
||||
} |
||||
|
||||
} |
||||
public static void setImageContext(HttpServletResponse var0, String var1, String var2) { |
||||
var0.setContentType("image/" + var2); |
||||
var0.setHeader("extension", var2); |
||||
var0.setHeader("Content-disposition", "attachment; filename=" + var1 + "." + var2+DSM); |
||||
} |
||||
|
||||
public static void setExcelContent(HttpServletResponse var0, String var1) { |
||||
var0.setContentType("application/x-excel"); |
||||
var0.setHeader("extension", "xls"); |
||||
var0.setHeader("Content-disposition", "attachment; filename=" + var1 + ".xls"+DSM); |
||||
} |
||||
|
||||
public static void setExcel2007Content(HttpServletResponse var0, String var1) { |
||||
var0.setContentType("application/x-excel"); |
||||
var0.setHeader("extension", "xlsx"); |
||||
var0.setHeader("Content-disposition", "attachment; filename=" + var1 + ".xlsx"+DSM); |
||||
} |
||||
|
||||
public static void setWordConetent(HttpServletResponse var0, String var1) { |
||||
var0.setContentType("application/msword"); |
||||
var0.setHeader("extension", "doc"); |
||||
var0.setHeader("Content-disposition", "attachment; filename=" + var1 + ".doc"+DSM); |
||||
} |
||||
|
||||
public static UserBean getUserById(HttpServletRequest req){ |
||||
String id = getCurrentUserFromRequest(req); |
||||
try { |
||||
if(StringUtils.isBlank(id)){ |
||||
return null; |
||||
} |
||||
return UserService.getInstance().getUser(id); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 判断FR是否登录了 |
||||
* |
||||
* @param req |
||||
* @return |
||||
*/ |
||||
private static Boolean isLoginedFR(HttpServletRequest req) { |
||||
try { |
||||
String token = TokenResource.COOKIE.getToken(req); |
||||
LoginService.getInstance().loginStatusValid(token, null); |
||||
return true; |
||||
} catch (Exception e) { |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 获取当前用户名 |
||||
* |
||||
* @param req |
||||
* @return |
||||
*/ |
||||
private static String getCurrentUserFromRequest(HttpServletRequest req) { |
||||
if (!isLoginedFR(req)) { |
||||
return StringUtils.EMPTY; |
||||
} |
||||
try { |
||||
return UserService.getInstance().getCurrentUserIdFromCookie(req); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
return StringUtils.EMPTY; |
||||
} |
||||
|
||||
public static String getReportId(HttpServletRequest req) { |
||||
String sessionID = WebUtils.getHTTPRequestParameter(req, "sessionID"); |
||||
WidgetSessionIDInfor sinfo = SessionPoolManager.getSessionIDInfor(sessionID, WidgetSessionIDInfor.class); |
||||
if(StringUtils.isNotBlank(sessionID)){ |
||||
return (String) sinfo.getParameterMap4Execute().get("reportId".toUpperCase()); |
||||
} |
||||
return StringUtils.EMPTY; |
||||
} |
||||
} |
Loading…
Reference in new issue