1.因为Aiax不能返回流 所以用js表单提交发送请求
var form = document.createElement("form");
document.body.appendChild(form);
var nameInput = document.createElement("input");
var pathInput = document.createElement("input");
nameInput.type = "hidden";
pathInput.type = "hidden";
form.appendChild(nameInput);
form.appendChild(pathInput);
nameInput.value =data;
pathInput.value=name;
nameInput.name = "filePath";
pathInput.name = "fileName";
form.action = "${ctx}/XX/download";
form.submit();
传入需要的参数
2.controller
response.setContentType("application/octet-stream");
response.setContentType("application/OCTET-STREAM;charset=UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+localName);= ======弹出保存框即资源选择器
File file=Upload.downLoadFile(filePath,fileName);//ftp获取文件------绝对路径 File file =new File("c:/xx/tt.ext");========相对路径
FileInputStream fis = new FileInputStream(file);
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
out.write(buffer, 0, len);
out.flush();
}
绝对路径:服务器上的文件, 绝对路径 根据路径和名字通过Ftp查找到file并返回 ======ftp获取文件请参照博客中ftp下载 在将所得文件输入保存
相对路径:相对路径只需new File(相对路径);即可得到文件
最后保存文件的名字如果只保存英文: 需要将localName转成GB2312 String localName = new String(fileName.getBytes("GB2312"), "ISO_8859_1");