Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gic-enterprise-base
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
base_platform_enterprise
gic-enterprise-base
Commits
d3a73048
Commit
d3a73048
authored
Nov 06, 2019
by
guojuxing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
下载组件完善
parent
4a097956
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
19 deletions
+72
-19
ExcelExtensionEnum.java
...n/java/com/gic/download/constants/ExcelExtensionEnum.java
+51
-0
DownloadUtils.java
...d/src/main/java/com/gic/download/utils/DownloadUtils.java
+21
-19
No files found.
gic-enterprise-download/src/main/java/com/gic/download/constants/ExcelExtensionEnum.java
0 → 100644
View file @
d3a73048
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
;
}
}
gic-enterprise-download/src/main/java/com/gic/download/utils/
Base
Utils.java
→
gic-enterprise-download/src/main/java/com/gic/download/utils/
Download
Utils.java
View file @
d3a73048
...
...
@@ -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
Base
Utils
{
private
static
final
Logger
LOGGER
=
LogManager
.
getLogger
(
Base
Utils
.
class
);
public
class
Download
Utils
{
private
static
final
Logger
LOGGER
=
LogManager
.
getLogger
(
Download
Utils
.
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
(
propertyName
s
))
{
throw
new
RuntimeException
(
"参数错误。FileName:"
+
fileName
+
",DataLoader:"
+
loader
+
",PropertyName:"
+
propertyName
s
);
if
(
StringUtils
.
isEmpty
(
fileName
)
||
loader
==
null
||
CollectionUtils
.
isEmpty
(
propertyName
List
))
{
throw
new
RuntimeException
(
"参数错误。FileName:"
+
fileName
+
",DataLoader:"
+
loader
+
",PropertyName:"
+
propertyName
List
);
}
// 获取输出流,设置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
(
fileNam
e
);
String
extension
=
ExcelExtensionEnum
.
getExtensionByCode
(
excelExtensionCod
e
);
// 如果是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
,
propertyName
s
,
writer
);
writeOutputStream
(
loader
,
propertyName
List
,
writer
);
// 写入excel
if
(!
ExcelUtils
.
setExcelInfo
(
sheet
,
columnWidth
,
header
,
rows
))
{
if
(!
ExcelUtils
.
setExcelInfo
(
sheet
,
columnWidth
,
header
List
,
rows
))
{
throw
new
IOException
(
"设置导出文件内容失败。"
);
}
...
...
@@ -148,9 +150,9 @@ public class BaseUtils {
}
};
// 写文件头
writer
.
write
(
header
);
writer
.
write
(
header
List
);
// 写文件
writeOutputStream
(
loader
,
propertyName
s
,
writer
);
writeOutputStream
(
loader
,
propertyName
List
,
writer
);
}
else
{
writer
=
new
Writer
()
{
@Override
...
...
@@ -161,14 +163,14 @@ public class BaseUtils {
}
};
// 写文件头
writer
.
write
(
header
);
writer
.
write
(
header
List
);
// 写文件
writeOutputStream
(
loader
,
propertyName
s
,
writer
);
writeOutputStream
(
loader
,
propertyName
List
,
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
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment