Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_53077510
production_ssm
提交
c5daf88a
P
production_ssm
项目概览
weixin_53077510
/
production_ssm
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
production_ssm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c5daf88a
编写于
9月 28, 2016
作者:
M
megagao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
添加了文件上传下载的工具类
上级
7cb03c45
变更
4
展开全部
显示空白变更内容
内联
并排
Showing
4 changed file
with
2697 addition
and
0 deletion
+2697
-0
src/main/java/org/hqu/production_ms/util/DownloadUtil.java
src/main/java/org/hqu/production_ms/util/DownloadUtil.java
+111
-0
src/main/java/org/hqu/production_ms/util/FileUtil.java
src/main/java/org/hqu/production_ms/util/FileUtil.java
+986
-0
src/main/java/org/hqu/production_ms/util/FormatStyle.java
src/main/java/org/hqu/production_ms/util/FormatStyle.java
+59
-0
src/main/java/org/hqu/production_ms/util/UtilFuns.java
src/main/java/org/hqu/production_ms/util/UtilFuns.java
+1541
-0
未找到文件。
src/main/java/org/hqu/production_ms/util/DownloadUtil.java
0 → 100644
浏览文件 @
c5daf88a
package
org.hqu.production_ms.util
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
javax.servlet.ServletOutputStream
;
import
javax.servlet.http.HttpServletResponse
;
public
class
DownloadUtil
{
/**
* @param filePath 要下载的文件路径
* @param returnName 返回的文件名
* @param response HttpServletResponse
* @param delFlag 是否删除文件
*/
protected
void
download
(
String
filePath
,
String
returnName
,
HttpServletResponse
response
,
boolean
delFlag
){
this
.
prototypeDownload
(
new
File
(
filePath
),
returnName
,
response
,
delFlag
);
}
/**
* @param file 要下载的文件
* @param returnName 返回的文件名
* @param response HttpServletResponse
* @param delFlag 是否删除文件
*/
protected
void
download
(
File
file
,
String
returnName
,
HttpServletResponse
response
,
boolean
delFlag
){
this
.
prototypeDownload
(
file
,
returnName
,
response
,
delFlag
);
}
/**
* @param file 要下载的文件
* @param returnName 返回的文件名
* @param response HttpServletResponse
* @param delFlag 是否删除文件
*/
public
void
prototypeDownload
(
File
file
,
String
returnName
,
HttpServletResponse
response
,
boolean
delFlag
){
// 下载文件
FileInputStream
inputStream
=
null
;
ServletOutputStream
outputStream
=
null
;
try
{
if
(!
file
.
exists
())
return
;
response
.
reset
();
//设置响应类型 PDF文件为"application/pdf",WORD文件为:"application/msword", EXCEL文件为:"application/vnd.ms-excel"。
response
.
setContentType
(
"application/octet-stream;charset=utf-8"
);
//设置响应的文件名称,并转换成中文编码
//returnName = URLEncoder.encode(returnName,"UTF-8");
returnName
=
response
.
encodeURL
(
new
String
(
returnName
.
getBytes
(),
"iso8859-1"
));
//保存的文件名,必须和页面编码一致,否则乱码
//attachment作为附件下载;inline客户端机器有安装匹配程序,则直接打开;注意改变配置,清除缓存,否则可能不能看到效果
response
.
addHeader
(
"Content-Disposition"
,
"attachment;filename="
+
returnName
);
//将文件读入响应流
inputStream
=
new
FileInputStream
(
file
);
outputStream
=
response
.
getOutputStream
();
int
length
=
1024
;
int
readLength
=
0
;
byte
buf
[]
=
new
byte
[
1024
];
readLength
=
inputStream
.
read
(
buf
,
0
,
length
);
while
(
readLength
!=
-
1
)
{
outputStream
.
write
(
buf
,
0
,
readLength
);
readLength
=
inputStream
.
read
(
buf
,
0
,
length
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
outputStream
.
flush
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
outputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
inputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
//删除原文件
if
(
delFlag
)
{
file
.
delete
();
}
}
}
/**
* by tony 2013-10-17
* @param byteArrayOutputStream 将文件内容写入ByteArrayOutputStream
* @param response HttpServletResponse 写入response
* @param returnName 返回的文件名
*/
public
void
download
(
ByteArrayOutputStream
byteArrayOutputStream
,
HttpServletResponse
response
,
String
returnName
)
throws
IOException
{
response
.
setContentType
(
"application/octet-stream;charset=utf-8"
);
returnName
=
response
.
encodeURL
(
new
String
(
returnName
.
getBytes
(),
"iso8859-1"
));
//保存的文件名,必须和页面编码一致,否则乱码
response
.
addHeader
(
"Content-Disposition"
,
"attachment;filename="
+
returnName
);
response
.
setContentLength
(
byteArrayOutputStream
.
size
());
ServletOutputStream
outputstream
=
response
.
getOutputStream
();
//取得输出流
byteArrayOutputStream
.
writeTo
(
outputstream
);
//写到输出流
byteArrayOutputStream
.
close
();
//关闭
outputstream
.
flush
();
//刷数据
}
}
src/main/java/org/hqu/production_ms/util/FileUtil.java
0 → 100644
浏览文件 @
c5daf88a
此差异已折叠。
点击以展开。
src/main/java/org/hqu/production_ms/util/FormatStyle.java
0 → 100644
浏览文件 @
c5daf88a
package
org.hqu.production_ms.util
;
public
class
FormatStyle
{
public
static
void
main
(
String
[]
args
)
{
// TODO: Add your code here
FormatStyle
formatStyle
=
new
FormatStyle
();
System
.
out
.
println
(
formatStyle
.
fileSize
(
"10737418240"
));
}
public
String
fileSize
(
String
s1
)
{
int
iPos
=
0
;
String
s
=
""
;
StringBuffer
sBuf
=
new
StringBuffer
();
try
{
if
(
s1
.
trim
().
compareTo
(
""
)==
0
){
return
""
;
}
long
g
=
Long
.
parseLong
(
"1099511627776"
);
//数字太大,JAVA直接写会无法识别,会引起下面比较失败
//int i = Integer.parseInt(s1);
double
i
=
Double
.
parseDouble
(
s1
);
if
(
i
<=
0
){
sBuf
.
append
(
""
);
}
else
if
(
i
<
1024
){
sBuf
.
append
(
i
).
append
(
" B"
);
//四舍五入
iPos
=
sBuf
.
lastIndexOf
(
".00 B"
);
if
(
iPos
>
0
){
sBuf
.
delete
(
iPos
,
sBuf
.
length
()-
2
);
}
}
else
if
(
i
<
1024
*
1024
){
sBuf
.
append
(
new
java
.
text
.
DecimalFormat
(
".00"
).
format
(
i
/
1024
)).
append
(
" KB"
);
//四舍五入
iPos
=
sBuf
.
lastIndexOf
(
".00 KB"
);
if
(
iPos
>
0
){
sBuf
.
delete
(
iPos
,
sBuf
.
length
()-
3
);
}
}
else
if
(
i
<
1024
*
1024
*
1024
){
sBuf
.
append
(
new
java
.
text
.
DecimalFormat
(
".00"
).
format
(
i
/(
1024
*
1024
))).
append
(
" M"
);
//四舍五入
iPos
=
sBuf
.
lastIndexOf
(
".00 M"
);
if
(
iPos
>
0
){
sBuf
.
delete
(
iPos
,
sBuf
.
length
()-
2
);
}
}
else
{
sBuf
.
append
(
new
java
.
text
.
DecimalFormat
(
".00"
).
format
(
i
/(
1024
*
1024
*
1024
))).
append
(
" G"
);
//四舍五入
iPos
=
sBuf
.
lastIndexOf
(
".00 G"
);
if
(
iPos
>
0
){
sBuf
.
delete
(
iPos
,
sBuf
.
length
()-
2
);
}
}
}
catch
(
Exception
e
){
return
""
;
}
return
sBuf
.
toString
();
}
}
src/main/java/org/hqu/production_ms/util/UtilFuns.java
0 → 100644
浏览文件 @
c5daf88a
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录