在spring mvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?
controller方法
@requestmapping(/tcdl)
public modelandview dlcode(httpservletrequest request,httpservletresponse response,@requestparam(value=ids) string ids,@requestparam(value = funid, required = false) integer funid) throws ioexception{
modelandview mav = new modelandview();
//response.setheader(charset, utf-8);
//response.setcontenttype(text/html; charset=utf-8);
list filelist = new arraylist();
string path = request.getsession().getservletcontext().getrealpath();
string[] trims = ids.split(,);
string type=; //文件格式后缀
for(int i=0;i operator oper = opservice.getoperatorbyid(integer.parseint(trims[i]));
if(!.equals(oper.getopcardurl())){
file f = new file(path+oper.getopcodeurl());
/*int k = oper.getopcodeurl().indexof(other); ///userpic/3/other/2014011617535382684910.png
int j =0;
while (j != -1) {
j = oper.getopcodeurl().indexof(.);
type = type.substring(j + 1);
}
file f = new file(path+oper.getopcodeurl());
string newname = oper.getopcodeurl().substring(0,k+1)+oper.getopusername()+oper.getopmobile()+type;
system.out.println(newname+,newname);*/
if(f.exists()){
// f.renameto(new file(path+oper.getopcodeurl().substring(0,k+1)+newname));
filelist.add(f);
}
}
}
string filename = twocodedown+.zip;
/**在服务器端创建打包下载的临时文件*/
file f = new file(path+/tmp);
if(!f.exists()){
f.mkdirs();
}
string outfilepath = path+/tmp/+filename;
file file = new file(outfilepath);
/**文件输出流*/
fileoutputstream outputstream = new fileoutputstream(file);
/**压缩流*/
zipoutputstream toclient = new zipoutputstream(outputstream);
//this.downloadzip(file);
/**压缩文件*/
opservice.downloadzip(filelist,toclient);
/**下载压缩*/
opservice.downloadzip(file, response);
return mav;
}
下载方法
**
* 下载打包的文件
* @throws ioexception
* */
public void downloadzip(file file,httpservletresponse response) throws ioexception{
/**依流的形式下载文件*/
try {
bufferedinputstream bis = new bufferedinputstream(new fileinputstream(file.getpath()));
fileoutputstream outstream = new fileoutputstream(file.getpath());
byte[] buffer = new byte[bis.available()];
bis.read(buffer);
bis.close();
outputstream toclient = new bufferedoutputstream(outstream);
response.setcontenttype(application/x-download);
response.setheader(content-disposition, attachment;filename= + file.getname());
toclient.write(buffer);
toclient.flush();
toclient.close();
file.delete(); //将生成的服务器端文件删除
} catch (filenotfoundexception e) {
e.printstacktrace();
}
}
回复讨论(解决方案) 你用的是ajax来请求的,ajax请求是不会弹出提示保存位置选框的。建议你用js或jquery动态的创建form表单来提交,记得要加上
response.setheader(content-disposition, attachment;filename=+imagename);。
