前台ajax请求后台实现下载
来源:互联网 发布:java xml生成pdf 编辑:程序博客网 时间:2024/05/17 05:14
下面是大致代码。
jsp页面:
function getDada(){
var returnFilePath = "";
....
...
$.ajax({
url: "a/b",
data: {data1: data1,data2: data2,},
type: "post",
async: false, // 必须同步
success: function (data) {
returnFilePath = data;
}
});
return returnFilePath;
}
要在后台实现下载页面需要增加一个函数:
function getDada() {
var filePath = getDada();
window.location.href = "a/c?filePath="+encodeURI(encodeURI(filePath));
}
java后台:
// 创建文件
@RequestMapping("/getDada")
public void getDada(String data1,String data2,HttpServletRequest request,HttpServletResponse response) throws Exception{
.....
.....
try {
String fileTime = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date().getTime());
String filePath = request.getSession().getServletContext().getRealPath("/") + "WEB-INF\\note.sql";
System.out.println("filePath==="+filePath);
File f = new File(filePath);
if (f.exists()) {
System.out.print("文件存在");
} else {
System.out.print("文件不存在");
f.createNewFile();// 不存在则创建
}
str = sb.toString();
BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write(str);
output.close();
response.getWriter().write(filePath); // 将路径返回到页面,即ajax请求返回的结果
} catch (Exception e) {
e.printStackTrace();
}
}
// 下载
@RequestMapping("/downloadFile")
public static void download(String filePath, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
filePath = URLDecoder.decode(request.getParameter("filePath"), "UTF-8");
String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
response.reset();
response.setContentType("application/octet-stream");
//如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(fileName, "UTF-8"));
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileInputStream fis = null;
OutputStream fos = null;
File fPath = new File(filePath);
try {
if (fPath.exists()) {
fis = new FileInputStream(filePath);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
int bytesRead = 0;
byte[] buffer = new byte[5 * 1024];
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);// 将文件发送到客户端
}
} else {
System.out.print("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if (bos != null) {
bos.close();
}
if(fos != null){
fos.close();
}
if(bis != null){
bis.close();
}
if(fis != null){
fis.close();
}
// 下载完成后删除服务器上的原文件
fPath.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 前台ajax请求后台实现下载
- web前台后台实现下载功能
- 笔记1----前台ajax请求,后台返回list对象集合!
- 前台发送 ajax 请求到后台传递数组参数
- ajax请求能进后台,但是前台报找不到url
- 前台jquery+ajax向后台请求数据,后台返回json,前台显示
- ajax请求后台,后台有数据,前台得不到数据(json)
- 不用AJAX实现前台JS调用后台C#方法
- 不用前台ajax,实现javascript调用后台c#
- 不用AJAX实现前台JS调用后台C#方法
- Ajax实现前台页面调用后台方法(mvc)
- ajax后台调用前台方法
- ajax 前台后台传输数据
- 网页下载模板前台后台
- 前台用的是ajax请求,后台json字符串拼接方式
- SpringMvc中ajax后台可以收到请求,前台依旧报错
- Ajax前台请求 以及 后台 Java 处理 (包含undefined的问题)
- ajax请求数据,后台提供数据返回json, 前台js拼html,回填
- Qt: 学生信息管理里系统,QTreewidget、QTableView实现
- Android Dialog 去除背景内容模糊
- 指纹解锁
- Android 的菜单
- 结构体对齐问题
- 前台ajax请求后台实现下载
- java String字符串空格隔开
- JQuery笔记二 :动画效果【蓝鸥HTML5出品】
- linux ssh服务是否已经启动?
- 云服务总线CSB
- Spring的AOP基于xml常用的几种配置
- 对象作用域与Servlet事件监听器
- Android弹幕功能实现,模仿斗鱼直播的弹幕效果
- 相关网站