(1)---pox配置
<
dependency
>
<
groupId
>
org.apache.commons
</
groupId
>
<
artifactId
>
commons-compress
</
artifactId
>
<
version
>
1.9
</
version
>
</dependency>
(2)---建立公共方法
/**
*Zip文件工具类
*
*
@author
@author
wenfei.fang
*/
public
class
ZipFileUtil {
/**
*通过 try-with-resources 的语法来创建流,可以安全的自动关闭
*
@author
wenfei.fang
*
@param
ZipArchiveOutputStream 压缩的文件流
*
@param
sonFileName 向压缩文件插入的文件名 test.txt
*
@param
inStreamFile test.txt文件输入流
*
@throws
IOException Io异常
*/
public
static
void
compressFilesZip(ZipArchiveOutputStream
zaos
, String
SonFileName
, InputStream
inStreamFile
) {
if
(
SonFileName
!=
null
&&
SonFileName
.length() > 0) {
try
{
zaos
.setUseZip64(Zip64Mode.
AsNeeded
);
ZipArchiveEntry
zipArchiveEntry
=
new
ZipArchiveEntry(
SonFileName
);
zaos
.putArchiveEntry(
zipArchiveEntry
);
try
{
byte
[]
buffer
=
new
byte
[1024 * 5];
int
len
= -1;
while
((
len
=
inStreamFile
.read(
buffer
)) != -1) {
zaos
.write(
buffer
, 0,
len
);
}
zaos
.closeArchiveEntry();
}
catch
(Exception
e
) {
throw
new
RuntimeException(
e
);
}
finally
{
if
(
inStreamFile
!=
null
)
inStreamFile
.close();
}
zaos
.finish();
}
catch
(Exception
e
) {
throw
new
RuntimeException(
e
);
}
finally
{
try
{
if
(
inStreamFile
!=
null
) {
inStreamFile
.close();
}
}
catch
(IOException
e
) {
throw
new
RuntimeException(
e
);
}
}
}
}
/**
*
@author
wenfei.fang
*
@param
zipOutputStream 压缩的文件流
*
@param
sonFileName 向压缩文件插入的文件名 test.txt
*
@param
inStreamFile test.txt文件输入流
*
@throws
IOException Io异常
*/
public
static
void
writeFile(ZipOutputStream
zipOutputStream
, String
sonFileName
, InputStream
inStreamFile
)
throws
IOException {
ZipEntry
ze
=
new
ZipEntry(
sonFileName
);
zipOutputStream
.putNextEntry(
ze
);
byte
[]
buffer
=
new
byte
[1204];
int
len
= -1;
while
((
len
=
inStreamFile
.read(
buffer
)) != -1) {
System.
err
.println(
len
);
zipOutputStream
.write(
buffer
, 0,
len
);
}
inStreamFile
.close();
}
/**
*
@author
wenfei.fang
*
@用apache
commentsio包中的IOUtils中的方法
*
@param
zipOutputStream 压缩的文件流
*
@param
sonFileName 向压缩文件插入的文件名 test.txt
*
@param
inStreamFile test.txt文件输入流
*
@throws
IOException Io异常
*
@message
Copy bytes from a large (over 2GB) InputStream to an OutputStream. 将字节超过2G的字节输入流复制到输出流中
* //IOUtils.copyLarge(input, output)
*/
public
static
void
writeFileUseIOUtils(ZipOutputStream
zipOutputStream
, String
sonFileName
, InputStream
inStreamFile
)
throws
IOException {
ZipEntry
ze
=
new
ZipEntry(
sonFileName
);
zipOutputStream
.putNextEntry(
ze
);
IOUtils.copy(
inStreamFile
,
zipOutputStream
);
IOUtils.closeQuietly(
inStreamFile
);
}
}
在control层的操作
@RequestMapping
(value =
"/api/fblMaster/downloadNet23"
)
public
@ResponseBody
String downloadNet2(HttpServletResponse
response
)
throws
MalformedURLException {
//通过给定的URL字符串创建URL
URL
url
=
new
URL(
"http://10.152.244.25:16200/cs/idcplg?IdcService=GET_FILE&dID=23378&dDocName=GGSTEST16200022613&allowInterrupt=1"
);
try
{
URLConnection
conn
=
url
.openConnection();
InputStream
inStreamFile
=
conn
.getInputStream();
URLConnection
conn1
=
url
.openConnection();
InputStream
inStreamFile1
=
conn1
.getInputStream();
URLConnection
conn2
=
url
.openConnection();
InputStream
inStreamFile2
=
conn2
.getInputStream();
URLConnection
conn21
=
url
.openConnection();
InputStream
inStreamFile21
=
conn21
.getInputStream();
response
.setHeader(
"Content-Disposition"
,
"attachment; filename=Barcode_Label_Approval_Verification"
+ String.valueOf(System.currentTimeMillis()).substring(4, 13) +
".zip"
);
response
.setCharacterEncoding(
"UTF-8"
);
response
.setContentType(
"application/x-msdownload"
);
OutputStream
out
=
response
.getOutputStream();
ZipOutputStream
zos
=
new
ZipOutputStream(
out
);
ZipFileUtil.writeFileUseIOUtils(
zos
,
"test1.jpg"
,
inStreamFile
);
ZipFileUtil.writeFileUseIOUtils(
zos
,
"sonFileName1/test1.jpg"
,
inStreamFile1
);
ZipFileUtil.writeFileUseIOUtils(
zos
,
"sonFileName2/test2.jpg"
,
inStreamFile2
);
ZipFileUtil.writeFileUseIOUtils(
zos
,
"sonFileName2/test1.jpg"
,
inStreamFile21
);
//关闭文件流(如果不关闭可能在压缩包中的文件只有名称没有文件内容)
IOUtils.closeQuietly(
zos
);
}
catch
(FileNotFoundException
e
) {
e
.printStackTrace();
}
catch
(IOException
e
) {
e
.printStackTrace();
}
return
null
;
}