JQuery的ajax函数的返回类型只有xml、text、json、html等类型,没有“流”类型,所以我们要实现ajax下载,不能够使用相应的ajax函数进行文件下载。但可以用js生成一个form,用这个form提交参数,并返回“流”类型的数据。在实现过程中,页面也没有进行刷新
1.单个文件下载的js代码
单个文件下载:
//filePath为存储文件的下载地址
function downloadFile(filePath){
if(filePath){
var form=$("<form>");//定义一个form表单
form.attr("style","display:none");
form.attr("target","");
form.attr("method","post");
form.attr("action",'/formengineWebService/downFileByPath');
var fileInput=$("<input>");
fileInput.attr("type","hidden");
fileInput.attr("id","macroPath");//设置属性的名字
fileInput.attr("name","macroPath");//设置属性的名字
fileInput.attr("value",filePath);//设置属性的值
$("body").append(form);//将表单放置在web中
form.append(fileInput);
form.submit();//表单提交
//form.remove();
}
}
多个文件下载:(下载easyui勾选的文件)
//下载勾选的文件
function downloadAllFiles(){
var rows = $('#filesTable').datagrid('getSelections');
if(rows.length == 0){
showError("请勾选至少一条数据!");
return;
}
//文件存储的地址的数组
var allFilePaths = [];
//查找所有行的文件路径字段的值
for (var i = 0; i < rows.length; i++) {
allFilePaths.push(removeSpace(rows[i]['filePath']));
}
var form=$("<form>");//定义一个form表单
form.attr("style","display:none");
form.attr("target","");
form.attr("method","post");
form.attr("action",'/formengineWebService/downFiles');
var fileInput=$("<input>");
fileInput.attr("type","hidden");
fileInput.attr("id","macroPath");//设置属性的名字
fileInput.attr("name","macroPath");//设置属性的名字
fileInput.attr("value",allFilePaths);//设置属性的值
$("body").append(form);//将表单放置在web中
form.append(fileInput);
form.submit();//表单提交
//form.remove();
}
2.springmvc controller层
单个文件:
@RequestMapping("/downFileByPath")
@ResponseBody
public void downFileByPath(String macroPath) throws IOException
{
String sext = "";
//获取文件扩展名
int pos = macroPath.lastIndexOf(".");
if (pos > 0)
{
sext = macroPath.substring(pos + 1);
}
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
Date time = new Date();
String tStamp = format.format(time);
String filename = "Export" + tStamp + "."+sext;
response.setContentType("application/octet-stream;charset=utf-8");
response.addHeader("Content-Disposition", "attachment; filename=" + filename);
mFileService.downloadByPath(macroPath, response.getOutputStream());;
}
多个文件:
@RequestMapping("/downFiles")
@ResponseBody
public void downFiles(String[] macroPath) throws IOException
{
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
Date time = new Date();
String tStamp = format.format(time);
String filename = "Export" + tStamp + ".zip";
response.setContentType("application/octet-stream;charset=utf-8");
response.addHeader("Content-Disposition", "attachment; filename=" + filename);
mFileService.downloadZipByPath(Arrays.asList(macroPath), response.getOutputStream());
}
3.service层
单个文件:
public void downloadByPath(final String macroPath, OutputStream output) throws IOException
{
if (macroPath == null)
{
throw new ArgumentNullException("macroPath");
}
if (output == null)
{
throw new ArgumentNullException("output");
}
//获取macroPath(文件存储路径)的文件输入流
InputStream in = MacroPath.getInputStream(macroPath, this.mWebRootPath, this.mMacroNameProperties);
byte buffer[] = new byte[1024];
int len = 0;
try
{
while((len = in.read(buffer)) >= 0)
{
output.write(buffer, 0, len);
}
}
finally
{
in.close();
}
}
多个文件:
@Override
public void downloadZipByPath(List<String> macroPath, OutputStream output) throws IOException
{
if (macroPath == null)
{
throw new ArgumentNullException("macroPath");
}
if (output == null)
{
throw new ArgumentNullException("output");
}
ZipOutputStream zipOutputStream = null;
InputStream zipSource = null;
BufferedInputStream buffInputStream = null;
try
{
// 构造最终压缩包的输出流
zipOutputStream = new ZipOutputStream(output);
for(String sPath : macroPath)
{
File file = new File(sPath);
// 将需要压缩的文件格式化为输入流
zipSource = MacroPath.getInputStream(sPath, this.mWebRootPath, this.mMacroNameProperties);
// 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
ZipEntry zipEntry = new ZipEntry(file.getName());
// 定位该压缩条目位置,开始写入文件到压缩包中
zipOutputStream.putNextEntry(zipEntry);
// 输入缓冲流
buffInputStream = new BufferedInputStream(zipSource, 1024 * 10);
int read = 0;
// 创建读写缓冲区
byte[] buf = new byte[1024 * 10];
while((read = buffInputStream.read(buf, 0, 1024 * 10)) != -1)
{
zipOutputStream.write(buf, 0, read);
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
// 关闭流
try
{
if (null != buffInputStream)
buffInputStream.close();
if (null != zipOutputStream)
zipOutputStream.close();
if (null != zipSource)
zipSource.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}