Commit d3a73048 by guojuxing

下载组件完善

parent 4a097956
package com.gic.download.constants;
/**
* Excel文件下载扩展类型
* @ClassName: ExcelExtensionEnum

* @Description: 

* @author guojuxing

* @date 2019/11/6 9:41 AM

*/
public enum ExcelExtensionEnum {
EXCEL_2007(1, ".xlsx"),
EXCEL_2003(2, ".xls"),
CSV(3, ".csv");
private int code;
private String extension;
private ExcelExtensionEnum(int code, String extension) {
this.code = code;
this.extension = extension;
}
public static String getExtensionByCode(Integer code) {
if (code == null) {
return ".csv";
}
for (ExcelExtensionEnum extensionEnum : values()) {
if (code.intValue() == extensionEnum.getCode()) {
return extensionEnum.getExtension();
}
}
return ".csv";
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
}
......@@ -8,6 +8,7 @@ import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.gic.download.constants.ExcelExtensionEnum;
import com.gic.thirdparty.BucketNameEnum;
import com.gic.thirdparty.FileOperateUtils;
import org.apache.commons.beanutils.BeanUtils;
......@@ -22,8 +23,8 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class BaseUtils {
private static final Logger LOGGER = LogManager.getLogger(BaseUtils.class);
public class DownloadUtils {
private static final Logger LOGGER = LogManager.getLogger(DownloadUtils.class);
private static final String SUFFIX_EXCEL_2007 = "xlsx";
private static final String SUFFIX_CSV = "csv";
......@@ -90,19 +91,20 @@ public class BaseUtils {
* Web下载文件
* @param tempPath 临时路径
* @param reportId 报表中心ID
* @param fileName 文件名称,如果文件名称为空的情况默认【 日期.csv】格式
* @param header 表头名称
* @param columnWidth 列宽
* @param fileName 文件名称
* @param excelExtensionCode Excel文件扩展名 枚举 ExcelExtensionEnum
* @param headerList 表头名称
* @param propertyNameList 字段名
* @param loader 数据加载类
* @param propertyNames
* @param columnWidth 列宽
* @throws Exception
* @author Silver
* @date 2017年3月16日 上午11:47:31
*/
protected <T> void download(String tempPath, Integer reportId, String fileName, List<String> header, List<Integer> columnWidth, DownloadDataLoader<T> loader, List<String> propertyNames) throws Exception {
protected <T> void download(String tempPath, Integer reportId, String fileName, Integer excelExtensionCode, List<String> headerList, List<String> propertyNameList, DownloadDataLoader<T> loader, List<Integer> columnWidth) throws Exception {
if (StringUtils.isEmpty(fileName) || loader == null || CollectionUtils.isEmpty(propertyNames)) {
throw new RuntimeException("参数错误。FileName:" + fileName + ",DataLoader:" + loader + ",PropertyName:" + propertyNames);
if (StringUtils.isEmpty(fileName) || loader == null || CollectionUtils.isEmpty(propertyNameList)) {
throw new RuntimeException("参数错误。FileName:" + fileName + ",DataLoader:" + loader + ",PropertyName:" + propertyNameList);
}
// 获取输出流,设置content-type等头域
//final OutputStream out = getResponseStream(response ,fileName);
......@@ -111,12 +113,12 @@ public class BaseUtils {
DataDownloadUtils.mkDir(new File(tempPath));
File file = new File(tempPath);
//初始化数据
tempFile = File.createTempFile(fileName, ".csv", file);
tempFile = File.createTempFile(fileName, ExcelExtensionEnum.getExtensionByCode(excelExtensionCode), file);
final OutputStream out = new FileOutputStream(tempFile);
try {
Writer writer = null;
// 获取文件后缀名
String extension = FilenameUtils.getExtension(fileName);
String extension = ExcelExtensionEnum.getExtensionByCode(excelExtensionCode);
// 如果是excel的后缀
if (SUFFIX_EXCEL_2003.equalsIgnoreCase(extension) || SUFFIX_EXCEL_2007.equalsIgnoreCase(extension)) {
Workbook workbook = new HSSFWorkbook();
......@@ -129,9 +131,9 @@ public class BaseUtils {
}
};
writeOutputStream(loader, propertyNames, writer);
writeOutputStream(loader, propertyNameList, writer);
// 写入excel
if (!ExcelUtils.setExcelInfo(sheet, columnWidth, header, rows)) {
if (!ExcelUtils.setExcelInfo(sheet, columnWidth, headerList, rows)) {
throw new IOException("设置导出文件内容失败。");
}
......@@ -148,9 +150,9 @@ public class BaseUtils {
}
};
// 写文件头
writer.write(header);
writer.write(headerList);
// 写文件
writeOutputStream(loader, propertyNames, writer);
writeOutputStream(loader, propertyNameList, writer);
} else {
writer = new Writer() {
@Override
......@@ -161,14 +163,14 @@ public class BaseUtils {
}
};
// 写文件头
writer.write(header);
writer.write(headerList);
// 写文件
writeOutputStream(loader, propertyNames, writer);
writeOutputStream(loader, propertyNameList, writer);
}
out.flush();
tempFile.deleteOnExit();
//上传文件
String fieldCode = "/" + System.currentTimeMillis() + "_" + fileName;
String fieldCode = "/" + System.currentTimeMillis() + "_" + fileName + ExcelExtensionEnum.getExtensionByCode(excelExtensionCode);
//文件存进腾讯云
String url = FileOperateUtils.simpleUploadFileFromLocal(tempFile, fieldCode, BucketNameEnum.REPORT_50000.getName());
if (org.apache.commons.lang.StringUtils.isNotBlank(url)) {
......@@ -176,7 +178,7 @@ public class BaseUtils {
}
LOGGER.info("上传文件到腾讯云,路径:" + url);
//更新数据
DataDownloadUtils.uploadFile(3, url);
DataDownloadUtils.uploadFile(reportId, url);
} finally {
IOUtils.closeQuietly(out);
}
......
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