Commit b7ade44a authored by yanzg's avatar yanzg

下载视频

parent 6b0a246a
......@@ -4,11 +4,7 @@ package com.yanzuoguang.cloud.excel;
import com.yanzuoguang.cloud.helper.HttpFileHelper;
import com.yanzuoguang.excel.*;
import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.helper.FileHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.thread.ThreadHelper;
import org.springframework.http.MediaType;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
......@@ -27,8 +23,8 @@ public class ExcelHttp<T extends Object> extends ExcelConsole<T> {
*
* @param exportData 导出信息
*/
public ExcelHttp(ExportData exportData) {
super(exportData);
public ExcelHttp(ExportData exportData, ExcelStatus<T> excelStatus) {
super(exportData, excelStatus);
}
/**
......@@ -37,8 +33,8 @@ public class ExcelHttp<T extends Object> extends ExcelConsole<T> {
* @param config 导出信息
* @param rowHandle 导出下载信息
*/
public ExcelHttp(ExportData config, ExcelRow<T> rowHandle) {
super(config, rowHandle);
public ExcelHttp(ExportData config, ExcelStatus<T> excelStatus, ExcelRow<T> rowHandle) {
super(config, excelStatus, rowHandle);
}
/**
......@@ -67,41 +63,23 @@ public class ExcelHttp<T extends Object> extends ExcelConsole<T> {
// 正式文件
String fileName = this.getFileName();
File file = new File(fileName);
// 临时文件
String fileNameTemp = this.getFileNameTemp();
File fileTemp = new File(fileNameTemp);
// 临时文件行数
String fileNameRow = this.getFileNameRow();
File fileRow = new File(fileNameRow);
boolean ok = file.exists();
boolean isDown = ok && config.getExportType() != ExportData.EXPORT_TYPE_WAIT;
if (isDown) {
if (file.exists()) {
// 下载文件
HttpFileHelper.localToDown(this.getFileName(), downFileName, response);
} else {
// 返回文件名和生成的文件大小
// 假如连续1分钟返回文件大小为0,则说明性能存在问题,或者是其他线程已经删除文件
ExportTempRes res = new ExportTempRes();
res.setFileName(config.getFileName());
res.setSize(fileTemp.length());
res.setRow(StringHelper.toLong(FileHelper.readFile(fileRow, "utf-8")));
res.setOk(ok);
// 输出结果
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
response.getWriter().print(JsonHelper.serialize(res));
return true;
}
return isDown;
return false;
}
/**
* 导出数据
*
* @param req 请求对接
* @param response 输出结果
* @param excelDao dao层实现接口
*/
public static <T extends Object, M extends Object> void export(ExportBase<T> req, HttpServletResponse response, ExcelDao<T, M> excelDao) {
public static <T extends Object, M extends Object> ExcelHttp<M> export(ExportBase<T> req, ExcelDao<T, M> excelDao) {
ExportData config = req.getConfig();
if (config == null) {
throw YzgError.getRuntimeException("024");
......@@ -116,33 +94,37 @@ public class ExcelHttp<T extends Object> extends ExcelConsole<T> {
if (StringHelper.isEmpty(config.getDownFileName())) {
config.setDownFileName(String.format("%s-%s.xlsx", config.getTitle(), config.getSubTitle()));
}
ExcelHttp<M> excel = new ExcelHttp<>(config);
boolean isDown = false;
ExcelHttp<M> excel = null;
if (excelDao instanceof ExcelStatus) {
excel = new ExcelHttp<>(config, (ExcelStatus<M>) excelDao);
} else {
excel = new ExcelHttp<>(config, null);
}
// 普通模式生成文件并下载
releaseExcel(req, excelDao, excel);
return excel;
}
/**
* 导出数据
*
* @param req 请求对接
* @param response 输出结果
* @param excelDao dao层实现接口
*/
public static <T extends Object, M extends Object> void export(ExportBase<T> req, HttpServletResponse response, ExcelDao<T, M> excelDao) {
ExcelHttp<M> excel = export(req, excelDao);
try {
int type = StringHelper.toInt(config.getExportType());
if (type == ExportData.EXPORT_TYPE_COMMON) {
// 普通模式生成文件并下载
releaseExcel(req, excelDao, excel);
} else if (isEmptyFile) {
// 等待下载模式,开启线程生成文件
ThreadHelper.runThread(new Runnable() {
@Override
public void run() {
releaseExcel(req, excelDao, excel);
}
});
}
// 等待下载模式,下载文件
isDown = excel.down(response);
excel.down(response);
} catch (IOException e) {
isDown = true;
e.printStackTrace();
throw YzgError.getRuntimeException(e, "045", e.getMessage());
} finally {
if (isDown) {
// 删除生成的临时文件
excel.remove();
}
// 删除生成的临时文件
excel.remove();
}
}
......
......@@ -12,7 +12,6 @@ import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
......@@ -41,6 +40,11 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*/
private final ExcelRow<T> rowHandle;
/**
* 行状态
*/
private final ExcelStatus<T> excelStatus;
/**
* 工作薄
*/
......@@ -85,18 +89,20 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
*
* @param exportData 导出数据
*/
public ExcelConsole(ExportData exportData) {
this(exportData, null);
public ExcelConsole(ExportData exportData, ExcelStatus<T> excelStatus) {
this(exportData, excelStatus, null);
}
/**
* 导出成Excel
*
* @param config 导出信息
* @param rowHandle 导出下载信息
* @param config 导出信息
* @param excelStatus 行状态处理
* @param rowHandle 导出下载信息
*/
public ExcelConsole(ExportData config, ExcelRow<T> rowHandle) {
public ExcelConsole(ExportData config, ExcelStatus<T> excelStatus, ExcelRow<T> rowHandle) {
this.config = config;
this.excelStatus = excelStatus;
this.rowHandle = rowHandle;
}
......@@ -316,8 +322,8 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
rowIndex++;
rowData++;
if (this.rowIndex % 100 == 0) {
FileHelper.writeFile(new File(this.getFileNameRow()), StringHelper.toString(this.rowIndex), "utf-8");
if (this.excelStatus != null) {
excelStatus.excelRow(t, rowIndex);
}
}
......@@ -365,11 +371,14 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
* 会自动在生成完毕调用该函数
*/
public ExcelConsole save() {
if (this.excelStatus != null) {
excelStatus.excelFinish();
}
if (workbook == null) {
return this;
}
// 合并数据配置
if(this.rowData > 0){
if (this.rowData > 0) {
int columnPos = 0;
for (ExportColumn column : this.config.getColumns()) {
mergerColumn(column, columnPos, true);
......@@ -401,7 +410,7 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
// 判断临时文件是否存在
File file = new File(fileNameTemp);
if (!file.exists()) {
throw YzgError.getRuntimeException("036",this.getFileName());
throw YzgError.getRuntimeException("036", this.getFileName());
}
// 重命名成正式文件
String fileName = this.getFileName();
......@@ -422,14 +431,10 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
if (this.workbook != null) {
workbook = null;
}
File fileRow = new File(this.getFileNameRow());
if (fileRow.exists()) {
fileRow.delete();
}
File file = new File(this.getFileName());
if (file.exists()) {
if (!file.delete()) {
throw YzgError.getRuntimeException("012",file.getName());
throw YzgError.getRuntimeException("012", file.getName());
}
}
return this;
......@@ -455,15 +460,6 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
return getFileName() + ".tmp";
}
/**
* 获取保存文件名(全路径)
*
* @return 文件名
*/
public String getFileNameRow() {
return getFileName() + ".row";
}
/**
* 创建单元格
*
......
package com.yanzuoguang.excel;
/**
* Excel导出接口
*
* @author 颜佐光
*/
public interface ExcelStatus<T extends Object> {
/**
* 循环处理每行数据
*
* @param excel excel对象
*/
void excelRow(T excel, int rowIndex);
/**
* 处理Excel结束
*/
void excelFinish();
}
......@@ -40,13 +40,6 @@ public class ExportData {
@ApiModelProperty(notes = "列默认宽度")
public static final short COLUMN_WIDTH = 0;
/**
* 导出类型,0-实时导出,1-等待生成文件后下载(该类型需要请求多次:
* 第一次请求会返回导出路径,后续几次会返回正在生成中,并且返回文件的大小,当生成完成后最后一次会下载文件)
*/
@ApiModelProperty(notes = "导出类型,0-实时导出,1-等待生成文件后下载(该类型需要请求多次:" +
"第一次请求会返回导出路径,后续几次会返回正在生成中,并且返回文件的大小,当生成完成后最后一次会下载文件),2-下载文件", required = true)
private int exportType;
/**
* 服务器保存路径
*/
......@@ -104,14 +97,6 @@ public class ExportData {
@ApiModelProperty(notes = "包含列")
private List<ExportColumn> columns = new ArrayList<>();
public int getExportType() {
return exportType;
}
public void setExportType(int exportType) {
this.exportType = exportType;
}
public String getServerPath() {
return serverPath;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment