本文实例为大家分享了javaweb多文件上传及zip打包下载的具体代码,供大家参考,具体内容如下
项目中经常会使用到文件上传及下载的功能。本篇文章总结场景在javaweb环境下,多文件上传及批量打包下载功能,包括前台及后台部分。
首先明确一点:
无法通过页面的无刷新ajax请求,直接发下载、上传请求。上传和下载,均需要在整页请求的基础上实现。项目中一般通过构建form表单形式实现这一功能。
一、多文件上传
项目需求为实现多图片上传功能。参考测试了网上找到的众多插件方法后,决定选用jquery原始上传方案。以下按步骤贴出具体代码。
1、html部分(可省略使用js构建)
1
2
3
4
|
<form id= "uploadform" method= "post" enctype= "multipart/form-data" >
<input type= "file" hidden name= "fileimage" multiple/>
<a href= "javascript:void(0);" rel= "external nofollow" rel= "external nofollow" id= "filesubmit" onclick= "uploadfilemulti()" >上传资料</a>
</form>
|
有几点说明:
1. form中 enctype=”multipart/form-data”
2. 例中使用标签,构建submit
2、js部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
var formdata = new formdata($( "#uploadform" )[ 0 ]);
formdata.append( "foldname" , "datumlist" ); //设置父级文件夹名称
formdata.append( "odercode" , selfordercode);
formdata.append( "datumtype" , datumtype);
$.ajax({
type: "post" ,
data: formdata,
url: "order/datumlist/batchinsertdatumlists" ,
contenttype: false ,
processdata: false ,
success: function (result) {
if (result.success) {
//清空框文件内容
$( "#fileimage" ).val( "" );
var obj = document.getelementbyid( 'fileimage' );
obj.outerhtml = obj.outerhtml;
refreshdatumlist();
showsuccesstoast(result.message);
} else {
showwarningtoast(result.message);
}
},
error: function () {
showerrortoast( '请求失败!' )
}
});
|
以上有几点说明:
1. var formdata = new formdata($(“#uploadform”)[0]);
2. 使用 formdata.append(“odercode”, selfordercode); 添加其他参数
java后台
1
2
|
multiparthttpservletrequest mrequest = (multiparthttpservletrequest) request;
list<multipartfile> files = mrequest.getfiles( "fileimage" );
|
以上有几点说明:
1. 获取multiparthttpservletrequest,对应file标签的name
二、文件批量下载
本项目中,需求为批量下载某一批次文件。使用zip在服务器压缩文件,之后将文件下载到客户机。
网上查询,使用java自带的文件输出类不能解决压缩文件中文件名乱码的问题。解决方法:使用ant.jar包,创建压缩文件时,可以设置文件的编码格式,文件名乱码的问题就解决了。
html部分(可省略使用js构建)
1
2
3
4
5
6
7
|
<form id= "uploadform" method= "post" enctype= "multipart/form-data" >
<div class = "product-dl" >
<input type= "hidden" name= "ordercode" />
<input type= "hidden" name= "datumtype" />
<a href= "javascript:void(0);" rel= "external nofollow" rel= "external nofollow" class = "btn" onclick= "batchdatumlistdownload()" >批量下载</a>
</div>
</form>
|
js部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//批量下载
function batchdatumlistdownload() {
var param = {};
param.datumtype = $( "#datumtypeq" ).val();
if (param.datumtype == - 1 ) {
param.datumtype = null ; //查询所有
}
param.ordercode = selfordercode;
$( "#uploadform input[name=ordercode]" ).val(param.ordercode);
$( "#uploadform input[name=datumtype]" ).val(param.datumtype);
var form = $( "#uploadform" )[ 0 ];
form.action = "order/datumlist/batchdownloaddatumlist" ;
form.method = "post" ;
form.submit(); //表单提交
}
|
后台部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
public void batchdownloaddatumlist(datumlistvo datumlistvo, httpservletresponse response) {
try {
//查询文件列表
list<datumlistvo> volist = datumlistservice.querydatumlists(datumlistvo);
//压缩文件
list<file> files = new arraylist<>();
for (datumlistvo vo : volist) {
file file = new file(vo.getdatumurl());
files.add(file);
}
string filename = datumlistvo.getordercode() + "_" + datumlistvo.getdatumtype() + ".zip" ;
//在服务器端创建打包下载的临时文件
string globaluploadpath = "" ;
string osname = system.getproperty( "os.name" );
if (osname.tolowercase().indexof( "windows" ) >= 0 ) {
globaluploadpath = globalkeys.getstring(globalkeys.windows_upload_path);
} else if (osname.tolowercase().indexof( "linux" ) >= 0 || osname.tolowercase().indexof( "mac" ) >= 0 ) {
globaluploadpath = globalkeys.getstring(globalkeys.linux_upload_path);
}
string outfilepath = globaluploadpath + file.separator + filename;
file file = new file(outfilepath);
//文件输出流
fileoutputstream outstream = new fileoutputstream(file);
//压缩流
zipoutputstream toclient = new zipoutputstream(outstream);
//设置压缩文件内的字符编码,不然会变成乱码
toclient.setencoding( "gbk" );
ziputil.zipfile(files, toclient);
toclient.close();
outstream.close();
ziputil.downloadzip(file, response);
} catch (exception e) {
e.printstacktrace();
}
}
|
其中ziputil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
/**
* 压缩文件列表中的文件
*
* @param files
* @param outputstream
* @throws ioexception
*/
public static void zipfile(list files, zipoutputstream outputstream) throws ioexception, servletexception {
try {
int size = files.size();
//压缩列表中的文件
for ( int i = 0 ; i < size; i++) {
file file = (file) files.get(i);
try {
zipfile(file, outputstream);
} catch (exception e) {
continue ;
}
}
} catch (exception e) {
throw e;
}
}
/**
* 将文件写入到zip文件中
*
* @param inputfile
* @param outputstream
* @throws exception
*/
public static void zipfile(file inputfile, zipoutputstream outputstream) throws ioexception, servletexception {
try {
if (inputfile.exists()) {
if (inputfile.isfile()) {
fileinputstream instream = new fileinputstream(inputfile);
bufferedinputstream binstream = new bufferedinputstream(instream);
zipentry entry = new zipentry(inputfile.getname());
outputstream.putnextentry(entry);
final int max_byte = 10 * 1024 * 1024 ; //最大的流为10m
long streamtotal = 0 ; //接受流的容量
int streamnum = 0 ; //流需要分开的数量
int leavebyte = 0 ; //文件剩下的字符数
byte [] inoutbyte; //byte数组接受文件的数据
streamtotal = binstream.available(); //通过available方法取得流的最大字符数
streamnum = ( int ) math.floor(streamtotal / max_byte); //取得流文件需要分开的数量
leavebyte = ( int ) streamtotal % max_byte; //分开文件之后,剩余的数量
if (streamnum > 0 ) {
for ( int j = 0 ; j < streamnum; ++j) {
inoutbyte = new byte [max_byte];
//读入流,保存在byte数组
binstream.read(inoutbyte, 0 , max_byte);
outputstream.write(inoutbyte, 0 , max_byte); //写出流
}
}
//写出剩下的流数据
inoutbyte = new byte [leavebyte];
binstream.read(inoutbyte, 0 , leavebyte);
outputstream.write(inoutbyte);
outputstream.closeentry(); //closes the current zip entry and positions the stream for writing the next entry
binstream.close(); //关闭
instream.close();
}
} else {
throw new servletexception( "文件不存在!" );
}
} catch (ioexception e) {
throw e;
}
}
/**
* 下载打包的文件
*
* @param file
* @param response
*/
public static void downloadzip(file file, httpservletresponse response) {
try {
// 以流的形式下载文件。
bufferedinputstream fis = new bufferedinputstream( new fileinputstream(file.getpath()));
byte [] buffer = new byte [fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
outputstream toclient = new bufferedoutputstream(response.getoutputstream());
response.setcontenttype( "application/octet-stream" );
response.setheader( "content-disposition" , "attachment;filename=" + file.getname());
toclient.write(buffer);
toclient.flush();
toclient.close();
file.delete(); //将生成的服务器端文件删除
} catch (ioexception ex) {
ex.printstacktrace();
}
}
|
以上基本满足文件上传下载所需。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_37878879/article/details/77197448