LAPTOP-SB56SG4Q\86185
3 years ago
16 changed files with 841 additions and 1 deletions
@ -1,3 +1,6 @@
|
||||
# open-JSD-8101 |
||||
|
||||
JSD-8101 异步流式导出(导出到服务器、异步下载) |
||||
JSD-8101 异步流式导出(导出到服务器、异步下载)\ |
||||
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\ |
||||
仅作为开发者学习参考使用!禁止用于任何商业用途!\ |
||||
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。 |
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||||
<plugin> |
||||
<id>com.fr.plugin.zk.jsd8101.excel</id> |
||||
<name><![CDATA[异步流式导出]]></name> |
||||
<active>yes</active> |
||||
<version>1.0.1</version> |
||||
<env-version>10.0</env-version> |
||||
<jartime>2020-08-20</jartime> |
||||
<vendor>fr.open</vendor> |
||||
<description> |
||||
<![CDATA[异步流式导出]]></description> |
||||
<change-notes><![CDATA[ |
||||
[2021-06-29]1.0.1 增加sessionid下载,用户操作记录,文件自定义<br/> |
||||
]]></change-notes> |
||||
|
||||
<main-package>com.fr.plugin.zk</main-package> |
||||
|
||||
<dependence> |
||||
<item type="plugin" key="com.fr.plugin.export.excel.stream"/> |
||||
</dependence> |
||||
|
||||
|
||||
<extra-core> |
||||
<DBAccessProvider class="com.fr.plugin.zk.stream.excel.db.ExcelAccessProviderImpl"/> |
||||
</extra-core> |
||||
|
||||
<extra-decision> |
||||
<HttpHandlerProvider class="com.fr.plugin.zk.stream.excel.HttpHandlerProvider"/> |
||||
<URLAliasProvider class="com.fr.plugin.zk.stream.excel.UrlAliasBridge"/> |
||||
</extra-decision> |
||||
|
||||
<function-recorder class="com.fr.plugin.zk.stream.excel.provider.ExportProvider"/> |
||||
|
||||
</plugin> |
@ -0,0 +1,20 @@
|
||||
package com.fr.plugin.zk.stream.excel; |
||||
|
||||
import com.fr.decision.fun.HttpHandler; |
||||
import com.fr.decision.fun.impl.AbstractHttpHandlerProvider; |
||||
import com.fr.plugin.zk.stream.excel.provider.DownloadProvider; |
||||
import com.fr.plugin.zk.stream.excel.provider.ExportProvider; |
||||
import com.fr.plugin.zk.stream.excel.provider.MessageProvider; |
||||
|
||||
public class HttpHandlerProvider extends AbstractHttpHandlerProvider { |
||||
|
||||
@Override |
||||
public HttpHandler[] registerHandlers() { |
||||
return new HttpHandler[]{ |
||||
new ExportProvider(), |
||||
new MessageProvider(), |
||||
new DownloadProvider() |
||||
}; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,17 @@
|
||||
package com.fr.plugin.zk.stream.excel; |
||||
|
||||
import com.fr.decision.fun.impl.AbstractURLAliasProvider; |
||||
import com.fr.decision.webservice.url.alias.URLAlias; |
||||
import com.fr.decision.webservice.url.alias.URLAliasFactory; |
||||
|
||||
public class UrlAliasBridge extends AbstractURLAliasProvider { |
||||
@Override |
||||
public URLAlias[] registerAlias() { |
||||
return new URLAlias[]{ |
||||
URLAliasFactory.createPluginAlias("/stream/export", "/stream/export", false), |
||||
URLAliasFactory.createPluginAlias("/stream/message", "/stream/message", false), |
||||
URLAliasFactory.createPluginAlias("/stream/download", "/stream/download", false), |
||||
}; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,31 @@
|
||||
package com.fr.plugin.zk.stream.excel.db; |
||||
|
||||
import com.fr.db.fun.impl.AbstractDBAccessProvider; |
||||
import com.fr.plugin.zk.stream.excel.db.dao.ExcelExportDao; |
||||
import com.fr.plugin.zk.stream.excel.db.entity.ExcelExportEntity; |
||||
import com.fr.plugin.zk.stream.excel.db.service.ExcelExportService; |
||||
import com.fr.stable.db.accessor.DBAccessor; |
||||
import com.fr.stable.db.dao.BaseDAO; |
||||
import com.fr.stable.db.dao.DAOProvider; |
||||
|
||||
public class ExcelAccessProviderImpl extends AbstractDBAccessProvider { |
||||
@Override |
||||
public DAOProvider[] registerDAO() { |
||||
return new DAOProvider[]{new DAOProvider() { |
||||
@Override |
||||
public Class getEntityClass() { |
||||
return ExcelExportEntity.class; |
||||
} |
||||
|
||||
@Override |
||||
public Class<? extends BaseDAO> getDAOClass() { |
||||
return ExcelExportDao.class; |
||||
} |
||||
}}; |
||||
} |
||||
|
||||
@Override |
||||
public void onDBAvailable(DBAccessor dbAccessor) { |
||||
ExcelExportService.getSingleton().init(dbAccessor); |
||||
} |
||||
} |
@ -0,0 +1,18 @@
|
||||
package com.fr.plugin.zk.stream.excel.db.dao; |
||||
|
||||
import com.fr.plugin.zk.stream.excel.db.entity.ExcelExportEntity; |
||||
import com.fr.stable.db.dao.BaseDAO; |
||||
import com.fr.stable.db.session.DAOSession; |
||||
|
||||
|
||||
public class ExcelExportDao extends BaseDAO<ExcelExportEntity> { |
||||
|
||||
public ExcelExportDao(DAOSession daoSession) { |
||||
super(daoSession); |
||||
} |
||||
|
||||
@Override |
||||
protected Class<ExcelExportEntity> getEntityClass() { |
||||
return ExcelExportEntity.class; |
||||
} |
||||
} |
@ -0,0 +1,136 @@
|
||||
package com.fr.plugin.zk.stream.excel.db.entity; |
||||
|
||||
import com.fr.stable.db.entity.BaseEntity; |
||||
import com.fr.third.javax.persistence.Column; |
||||
import com.fr.third.javax.persistence.Entity; |
||||
import com.fr.third.javax.persistence.Lob; |
||||
import com.fr.third.javax.persistence.Table; |
||||
|
||||
import java.util.Date; |
||||
import java.util.UUID; |
||||
|
||||
|
||||
@Entity |
||||
@Table(name = "fine_export_stream_excel") |
||||
public class ExcelExportEntity extends BaseEntity { |
||||
|
||||
//模板路径
|
||||
@Column(name = "path") |
||||
private String path; |
||||
|
||||
//模板参数
|
||||
@Lob |
||||
@Column(name = "parameters") |
||||
private String parameters; |
||||
|
||||
//导出状态 导出中、完成、失败
|
||||
@Column(name = "status") |
||||
private String status = "导出中"; |
||||
|
||||
@Column(name = "start_time") |
||||
private Date startTime; |
||||
|
||||
@Column(name = "end_time") |
||||
private Date endTime; |
||||
|
||||
@Column(name = "filename") |
||||
private String filename; |
||||
|
||||
@Column(name = "file_path") |
||||
private String filePath; |
||||
|
||||
@Column(name = "file_size") |
||||
private Integer fileSize; |
||||
|
||||
@Lob |
||||
@Column(name = "message") |
||||
private String message; |
||||
|
||||
@Column(name = "username") |
||||
private String username; |
||||
|
||||
public ExcelExportEntity() { |
||||
this.setId(UUID.randomUUID().toString()); |
||||
} |
||||
|
||||
|
||||
public String getPath() { |
||||
return path; |
||||
} |
||||
|
||||
public void setPath(String path) { |
||||
this.path = path; |
||||
} |
||||
|
||||
public String getParameters() { |
||||
return parameters; |
||||
} |
||||
|
||||
public void setParameters(String parameters) { |
||||
this.parameters = parameters; |
||||
} |
||||
|
||||
public String getStatus() { |
||||
return status; |
||||
} |
||||
|
||||
public void setStatus(String status) { |
||||
this.status = status; |
||||
} |
||||
|
||||
public Date getStartTime() { |
||||
return startTime; |
||||
} |
||||
|
||||
public void setStartTime(Date startTime) { |
||||
this.startTime = startTime; |
||||
} |
||||
|
||||
public Date getEndTime() { |
||||
return endTime; |
||||
} |
||||
|
||||
public void setEndTime(Date endTime) { |
||||
this.endTime = endTime; |
||||
} |
||||
|
||||
public Integer getFileSize() { |
||||
return fileSize; |
||||
} |
||||
|
||||
public void setFileSize(Integer fileSize) { |
||||
this.fileSize = fileSize; |
||||
} |
||||
|
||||
public String getFilePath() { |
||||
return filePath; |
||||
} |
||||
|
||||
public void setFilePath(String filePath) { |
||||
this.filePath = filePath; |
||||
} |
||||
|
||||
public String getMessage() { |
||||
return message; |
||||
} |
||||
|
||||
public void setMessage(String message) { |
||||
this.message = message; |
||||
} |
||||
|
||||
public String getFilename() { |
||||
return filename; |
||||
} |
||||
|
||||
public void setFilename(String filename) { |
||||
this.filename = filename; |
||||
} |
||||
|
||||
public String getUsername() { |
||||
return username; |
||||
} |
||||
|
||||
public void setUsername(String username) { |
||||
this.username = username; |
||||
} |
||||
} |
@ -0,0 +1,60 @@
|
||||
package com.fr.plugin.zk.stream.excel.db.service; |
||||
|
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.zk.stream.excel.db.dao.ExcelExportDao; |
||||
import com.fr.plugin.zk.stream.excel.db.entity.ExcelExportEntity; |
||||
import com.fr.stable.db.accessor.DBAccessor; |
||||
import com.fr.stable.db.action.DBAction; |
||||
import com.fr.stable.db.dao.DAOContext; |
||||
import com.fr.stable.query.QueryFactory; |
||||
import com.fr.stable.query.condition.QueryCondition; |
||||
import com.fr.stable.query.restriction.impl.EqRestriction; |
||||
|
||||
public class ExcelExportService { |
||||
|
||||
private ExcelExportService() { |
||||
} |
||||
|
||||
private static class Hoder { |
||||
private static ExcelExportService excelExportService = new ExcelExportService(); |
||||
} |
||||
|
||||
public static ExcelExportService getSingleton() { |
||||
return Hoder.excelExportService; |
||||
} |
||||
|
||||
private DBAccessor accessor = null; |
||||
|
||||
public void init(DBAccessor accessor) { |
||||
this.accessor = accessor; |
||||
} |
||||
|
||||
|
||||
public void addOrUpdate(ExcelExportEntity entity) { |
||||
try { |
||||
accessor.runDMLAction(new DBAction<Boolean>() { |
||||
@Override |
||||
public Boolean run(DAOContext context) throws Exception { |
||||
context.getDAO(ExcelExportDao.class).addOrUpdate(entity); |
||||
return true; |
||||
} |
||||
}); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error("存储导出记录失败:" + e.getMessage(), e); |
||||
} |
||||
} |
||||
|
||||
public ExcelExportEntity queryById(String id) throws Exception { |
||||
ExcelExportEntity entity = accessor.runQueryAction(new DBAction<ExcelExportEntity>() { |
||||
@Override |
||||
public ExcelExportEntity run(DAOContext context) throws Exception { |
||||
QueryCondition queryCondition = QueryFactory.create(); |
||||
queryCondition.addRestriction(new EqRestriction("id", id)); |
||||
return context.getDAO(ExcelExportDao.class).findOne(queryCondition); |
||||
} |
||||
}); |
||||
return entity; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,88 @@
|
||||
package com.fr.plugin.zk.stream.excel.dto; |
||||
|
||||
import com.fr.base.ServerConfig; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.zk.stream.excel.util.ExportUtil; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.third.org.apache.commons.collections4.CollectionUtils; |
||||
import com.fr.third.org.apache.commons.collections4.MapUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.net.URLEncoder; |
||||
import java.util.HashMap; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Project name finereport-gradle |
||||
* @description: |
||||
* @created on 2021-06-24 16:36. |
||||
* @Modified By |
||||
*/ |
||||
public class ExportDto { |
||||
|
||||
private String path; |
||||
|
||||
private String filename; |
||||
|
||||
private String username; |
||||
|
||||
private String fullUrl; |
||||
|
||||
private HashMap<String, String> parameters = new HashMap<>(); |
||||
|
||||
public String getPath() { |
||||
return path; |
||||
} |
||||
|
||||
public void setPath(String path) { |
||||
this.path = path; |
||||
} |
||||
|
||||
public HashMap<String, String> getParameters() { |
||||
return parameters; |
||||
} |
||||
|
||||
public void setParameters(HashMap<String, String> parameters) { |
||||
this.parameters = parameters; |
||||
} |
||||
|
||||
public String getFilename() { |
||||
return filename; |
||||
} |
||||
|
||||
public void setFilename(String filename) { |
||||
this.filename = filename; |
||||
} |
||||
|
||||
public String getUsername() { |
||||
return username; |
||||
} |
||||
public void setUsername(String username) { |
||||
this.username = username; |
||||
} |
||||
|
||||
public void setFullPath(HttpServletRequest req){ |
||||
String path = getPath(); |
||||
HashMap<String, String> parameters = getParameters(); |
||||
String root = ExportUtil.buildServerURL(req) + req.getContextPath() + "/" + ServerConfig.getInstance().getServletName(); |
||||
String url = null; |
||||
if(StringUtils.isNotBlank(path)){ |
||||
try { |
||||
url = root + "/view/report?viewlet=" + URLEncoder.encode(path, "UTF-8") + "&op=export&format=excel&extype=stream"; |
||||
} catch (UnsupportedEncodingException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
}else{ |
||||
url = root + "/view/report?op=export&format=excel&extype=stream"; |
||||
} |
||||
if(MapUtils.isNotEmpty(parameters)){ |
||||
url = ExportUtil.appendUrlPara(url,getParameters()); |
||||
} |
||||
this.fullUrl = url; |
||||
} |
||||
|
||||
public String getFullUrl() { |
||||
return fullUrl; |
||||
} |
||||
} |
@ -0,0 +1,103 @@
|
||||
package com.fr.plugin.zk.stream.excel.export; |
||||
|
||||
import com.fr.general.http.HttpClient; |
||||
import com.fr.io.context.ResourceModuleContext; |
||||
import com.fr.io.repository.ResourceRepository; |
||||
import com.fr.io.repository.base.fs.FileSystemRepository; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.zk.stream.excel.db.entity.ExcelExportEntity; |
||||
import com.fr.plugin.zk.stream.excel.db.service.ExcelExportService; |
||||
import com.fr.plugin.zk.stream.excel.dto.ExportDto; |
||||
import com.fr.plugin.zk.stream.excel.util.ExportUtil; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.third.org.apache.poi.util.IOUtils; |
||||
import com.fr.workspace.WorkContext; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.io.InputStream; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Date; |
||||
import java.util.HashMap; |
||||
import java.util.UUID; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Project finereport-gradle |
||||
* @description: |
||||
* @created on 2021-06-24 15:52. |
||||
* @Modified By |
||||
*/ |
||||
public class ExportAction implements Runnable { |
||||
|
||||
private HttpServletRequest req; |
||||
private ExportDto export; |
||||
|
||||
public ExportAction(HttpServletRequest req, ExportDto exportDto) { |
||||
this.req = req; |
||||
this.export = exportDto; |
||||
} |
||||
|
||||
@Override |
||||
public void run() { |
||||
String path = this.export.getPath(); |
||||
HashMap<String, String> parameters = this.export.getParameters(); |
||||
String url = this.export.getFullUrl(); |
||||
ExcelExportService excelExportService = ExcelExportService.getSingleton(); |
||||
ExcelExportEntity entity = new ExcelExportEntity(); |
||||
entity.setPath(path); |
||||
entity.setParameters(ExportUtil.writeToJson(parameters)); |
||||
entity.setStartTime(new Date()); |
||||
entity.setUsername(this.export.getUsername()); |
||||
excelExportService.addOrUpdate(entity); |
||||
HttpClient client = new HttpClient(url); |
||||
client.asGet(); |
||||
//file
|
||||
String type = client.getHeaderField("extension"); |
||||
if ("xlsx".equals(type)) { |
||||
try { |
||||
String filename = client.getHeaderField("Content-disposition").replace("attachment; filename=", ""); |
||||
InputStream inputStream = client.getResponseStream(); |
||||
Thread.sleep(1000); |
||||
filename = new String(filename.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); |
||||
byte[] fileByte = new byte[inputStream.available()]; |
||||
IOUtils.readFully(inputStream, fileByte); |
||||
inputStream.close(); |
||||
String filepath = "/resources/excel/" + UUID.randomUUID().toString() + ".xlsx"; |
||||
if (StringUtils.isNotBlank(this.export.getFilename())) { |
||||
entity.setFilename(this.export.getFilename()); |
||||
} else { |
||||
entity.setFilename(filename.replace(".xlsx", "")); |
||||
} |
||||
entity.setStatus("完成"); |
||||
entity.setFilePath(filepath); |
||||
entity.setFileSize(fileByte.length); |
||||
ResourceRepository repository = ResourceModuleContext.getManager().getCurrent(); |
||||
if (repository instanceof FileSystemRepository) { |
||||
//本地 远程
|
||||
WorkContext.getWorkResource().write(filepath, fileByte); |
||||
} else { |
||||
//集群 文件节点
|
||||
ResourceModuleContext.getManager().getCurrent().write(filepath, fileByte); |
||||
} |
||||
entity.setEndTime(new Date()); |
||||
excelExportService.addOrUpdate(entity); |
||||
} catch (Exception e) { |
||||
String message = "导出失败。详情:" + e.getMessage(); |
||||
entity.setEndTime(new Date()); |
||||
entity.setStatus("失败"); |
||||
entity.setMessage(message); |
||||
FineLoggerFactory.getLogger().error(message, e); |
||||
} |
||||
} else { |
||||
// 存数据库
|
||||
String text = client.getResponseText(); |
||||
FineLoggerFactory.getLogger().error("导出失败。" + client.getResponseText()); |
||||
entity.setEndTime(new Date()); |
||||
entity.setStatus("失败"); |
||||
entity.setMessage(text); |
||||
excelExportService.addOrUpdate(entity); |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,80 @@
|
||||
package com.fr.plugin.zk.stream.excel.provider; |
||||
|
||||
import com.fr.decision.fun.impl.BaseHttpHandler; |
||||
import com.fr.io.context.ResourceModuleContext; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.zk.stream.excel.db.entity.ExcelExportEntity; |
||||
import com.fr.plugin.zk.stream.excel.db.service.ExcelExportService; |
||||
import com.fr.plugin.zk.stream.excel.util.ExportUtil; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
||||
import com.fr.third.v2.org.apache.poi.util.IOUtils; |
||||
import com.fr.web.utils.WebUtils; |
||||
import com.fr.workspace.WorkContext; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.InputStream; |
||||
import java.nio.charset.StandardCharsets; |
||||
|
||||
public class DownloadProvider extends BaseHttpHandler { |
||||
|
||||
@Override |
||||
public RequestMethod getMethod() { |
||||
return RequestMethod.GET; |
||||
} |
||||
|
||||
@Override |
||||
public String getPath() { |
||||
return "/stream/download"; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isPublic() { |
||||
return false; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception { |
||||
String username = ExportUtil.getLoginUserAndResponse(req, res); |
||||
if (StringUtils.isBlank(username)) { |
||||
return; |
||||
} |
||||
String id = WebUtils.getHTTPRequestParameter(req, "id"); |
||||
ExcelExportEntity entity = ExcelExportService.getSingleton().queryById(id); |
||||
if (entity == null) { |
||||
res.setContentType("text/plain"); |
||||
WebUtils.printAsString(res, "找不到对应数据"); |
||||
} else { |
||||
try { |
||||
InputStream inputStream = read(entity.getFilePath()); |
||||
if (inputStream == null) { |
||||
throw new Exception("找不到文件:" + entity.getFilePath()); |
||||
} |
||||
res.setHeader("Content-Disposition", "attachment; filename=" |
||||
+ new String(entity.getFilename().concat(".xlsx").getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1)); |
||||
res.setContentLength(entity.getFileSize()); |
||||
res.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
||||
IOUtils.copy(inputStream, res.getOutputStream()); |
||||
} catch (Exception e) { |
||||
res.setContentType("text/plain"); |
||||
WebUtils.printAsString(res, "找不到文件:" + entity.getFilePath()); |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private InputStream read(String path) { |
||||
// 防止某些情况找不到文件
|
||||
InputStream inputStream = ResourceModuleContext.getManager().getCurrent().read(path); |
||||
if (inputStream == null) { |
||||
inputStream = ResourceModuleContext.getManager().getCurrent().openStream(path); |
||||
} |
||||
if (inputStream == null) { |
||||
inputStream = WorkContext.getWorkResource().openStream(path); |
||||
} |
||||
return inputStream; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,73 @@
|
||||
package com.fr.plugin.zk.stream.excel.provider; |
||||
|
||||
import com.fr.decision.fun.impl.BaseHttpHandler; |
||||
import com.fr.decision.webservice.Response; |
||||
import com.fr.decision.webservice.v10.user.UserService; |
||||
import com.fr.intelli.record.Focus; |
||||
import com.fr.intelli.record.Original; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.transform.ExecuteFunctionRecord; |
||||
import com.fr.plugin.transform.FunctionRecorder; |
||||
import com.fr.plugin.zk.stream.excel.dto.ExportDto; |
||||
import com.fr.plugin.zk.stream.excel.export.ExportAction; |
||||
import com.fr.plugin.zk.stream.excel.util.Constants; |
||||
import com.fr.plugin.zk.stream.excel.util.ExportUtil; |
||||
import com.fr.record.analyzer.EnableMetrics; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.util.concurrent.ExecutorService; |
||||
import java.util.concurrent.Executors; |
||||
|
||||
@EnableMetrics |
||||
@FunctionRecorder |
||||
public class ExportProvider extends BaseHttpHandler { |
||||
|
||||
@Override |
||||
public RequestMethod getMethod() { |
||||
return RequestMethod.POST; |
||||
} |
||||
|
||||
@Override |
||||
public String getPath() { |
||||
return "/stream/export"; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isPublic() { |
||||
return false; |
||||
} |
||||
|
||||
@Focus(id = Constants.PLUGIN_ID, text = "Async export stream excel", source = Original.PLUGIN) |
||||
@ExecuteFunctionRecord |
||||
@Override |
||||
public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception { |
||||
res.setContentType("application/json; charset=UTF-8"); |
||||
String username = ExportUtil.getLoginUserAndResponse(req, res); |
||||
if (StringUtils.isBlank(username)) { |
||||
return; |
||||
} |
||||
try { |
||||
String body = ExportUtil.requestBody2JsonString(req); |
||||
ExportDto exportDto = ExportUtil.OBJECT_MAPPER.readValue(body, ExportDto.class); |
||||
exportDto.setUsername(username); |
||||
exportDto.setFullPath(req); |
||||
ExecutorService service = Executors.newSingleThreadExecutor(); |
||||
service.execute(new ExportAction(req, exportDto)); |
||||
service.shutdown(); |
||||
Response response = Response.success(); |
||||
response.status(200); |
||||
WebUtils.printAsString(res, ExportUtil.OBJECT_MAPPER.writeValueAsString(response)); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error("获取参数失败:" + e.getMessage(), e); |
||||
Response response = Response.error(500, "", "获取参数失败,请填写正确的参数!"); |
||||
WebUtils.printAsString(res, ExportUtil.OBJECT_MAPPER.writeValueAsString(response)); |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,53 @@
|
||||
package com.fr.plugin.zk.stream.excel.provider; |
||||
|
||||
import com.fr.decision.fun.impl.BaseHttpHandler; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.plugin.zk.stream.excel.db.entity.ExcelExportEntity; |
||||
import com.fr.plugin.zk.stream.excel.db.service.ExcelExportService; |
||||
import com.fr.plugin.zk.stream.excel.util.ExportUtil; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
|
||||
public class MessageProvider extends BaseHttpHandler { |
||||
|
||||
@Override |
||||
public RequestMethod getMethod() { |
||||
return RequestMethod.GET; |
||||
} |
||||
|
||||
@Override |
||||
public String getPath() { |
||||
return "/stream/message"; |
||||
} |
||||
|
||||
@Override |
||||
public boolean isPublic() { |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public void handle(HttpServletRequest req, HttpServletResponse res) throws Exception { |
||||
String username = ExportUtil.getLoginUserAndResponse(req, res); |
||||
if (StringUtils.isBlank(username)) { |
||||
return; |
||||
} |
||||
String id = WebUtils.getHTTPRequestParameter(req, "id"); |
||||
try { |
||||
ExcelExportEntity entity = ExcelExportService.getSingleton().queryById(id); |
||||
if (entity == null) { |
||||
WebUtils.printAsString(res, "找不到对应的消息"); |
||||
} else { |
||||
res.setContentType("text/html; charset=gb2312"); |
||||
WebUtils.printAsString(res, entity.getMessage()!=null?entity.getMessage():"无错误信息"); |
||||
} |
||||
} catch (Exception e) { |
||||
WebUtils.printAsString(res, "找不到对应的消息"); |
||||
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,9 @@
|
||||
package com.fr.plugin.zk.stream.excel.util; |
||||
|
||||
public interface Constants { |
||||
/*** |
||||
* 插件id |
||||
*/ |
||||
String PLUGIN_ID = "com.fr.plugin.zk.jsd8101.excel"; |
||||
|
||||
} |
@ -0,0 +1,115 @@
|
||||
package com.fr.plugin.zk.stream.excel.util; |
||||
|
||||
import com.fr.decision.webservice.Response; |
||||
import com.fr.decision.webservice.v10.user.UserService; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.stable.CommonCodeUtils; |
||||
import com.fr.stable.StringUtils; |
||||
import com.fr.third.fasterxml.jackson.core.JsonProcessingException; |
||||
import com.fr.third.fasterxml.jackson.databind.ObjectMapper; |
||||
import com.fr.web.utils.WebUtils; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.BufferedReader; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.net.URLEncoder; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Iterator; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author fr.open |
||||
* @Project name finereport-gradle |
||||
* @description: |
||||
* @created on 2021-06-24 16:01. |
||||
* @Modified By |
||||
*/ |
||||
public class ExportUtil { |
||||
|
||||
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
||||
|
||||
public static String requestBody2JsonString(HttpServletRequest req) throws IOException { |
||||
String data = ""; |
||||
StringBuffer stringBuffer = new StringBuffer(); |
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream(), StandardCharsets.UTF_8)); |
||||
while (null != (data = reader.readLine())) { |
||||
stringBuffer.append(data); |
||||
} |
||||
return stringBuffer.toString(); |
||||
} |
||||
|
||||
public static String buildServerURL(HttpServletRequest req) { |
||||
StringBuffer serverURIBuf = new StringBuffer(); |
||||
serverURIBuf.append("http"); |
||||
serverURIBuf.append("://"); |
||||
serverURIBuf.append("127.0.0.1"); |
||||
serverURIBuf.append(':'); |
||||
serverURIBuf.append(req.getServerPort()); |
||||
return serverURIBuf.toString(); |
||||
} |
||||
|
||||
public static String writeToJson(Object o) { |
||||
try { |
||||
return OBJECT_MAPPER.writeValueAsString(o); |
||||
} catch (JsonProcessingException e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
return ""; |
||||
} |
||||
|
||||
public static String getLoginUserAndResponse(HttpServletRequest req, HttpServletResponse res) { |
||||
try { |
||||
res.setContentType("application/json; charset=UTF-8"); |
||||
String username = UserService.getInstance().getUserByRequestCookie(req).getUserName(); |
||||
if (StringUtils.isBlank(username)) { |
||||
Response response = Response.error(500, "", "用户未登录!"); |
||||
WebUtils.printAsString(res, ExportUtil.OBJECT_MAPPER.writeValueAsString(response)); |
||||
return null; |
||||
} |
||||
return username; |
||||
} catch (Exception e) { |
||||
res.setContentType("application/json; charset=UTF-8"); |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
Response response = Response.error(500, "", "用户未登录!"); |
||||
try { |
||||
WebUtils.printAsString(res, ExportUtil.OBJECT_MAPPER.writeValueAsString(response)); |
||||
} catch (Exception exception) { |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static String appendUrlPara(String url,Map<String, String> maps) { |
||||
if (maps != null) { |
||||
StringBuilder builder = new StringBuilder(); |
||||
builder.append(url); |
||||
Iterator iterator = maps.entrySet().iterator(); |
||||
while (iterator.hasNext()) { |
||||
Map.Entry mapEntry = (Map.Entry) iterator.next(); |
||||
builder.append('&'); |
||||
builder.append(encodeString(mapEntry.getKey().toString(),true)); |
||||
builder.append('='); |
||||
builder.append(encodeString(mapEntry.getValue().toString(),true)); |
||||
} |
||||
return builder.toString(); |
||||
} |
||||
return url; |
||||
} |
||||
|
||||
public static String encodeString(String var1, boolean var2) { |
||||
String var3 = CommonCodeUtils.cjkEncode(var1); |
||||
if (!var2) { |
||||
return var3; |
||||
} else { |
||||
try { |
||||
return URLEncoder.encode(var3, "UTF-8"); |
||||
} catch (UnsupportedEncodingException var5) { |
||||
return var3; |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Binary file not shown.
Loading…
Reference in new issue