ajax 如何实现文件下载 ajax 如何实现文件下载 点下按钮就下载了 不要到另一个窗口去下载文章和代码都可以 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一般情况下的下载也不需要跳到另一个窗口下载啊。你会实现下载么?如果会,那你会ajax么?两个都会了不就ok了 直接一个a标签,href是下载文件的路径 就ok 这种在html行一般不用 D_attachment att = new D_attachment(); DaoIn in = new DaoIn(); att.setAttachment_types(typeid); att.setItem_id(itemid); in.setData(att); DaoOut out = DaoController.execute(D_attachmentS02Dao.class, in); F_upload_file filedb = new F_upload_file(); filedb = (F_upload_file) out.getData(); //下载 String filePath = filedb.getFile_url(); String filetype = filePath.substring(filePath.lastIndexOf('.')); String msg = ""; if(filePath != null && !"".equals(filePath)) { String path = getServlet().getServletContext().getRealPath("")+filePath;//找到文件 response.addHeader("Content-Disposition", "attachment; filename="+new String((filename+filetype).getBytes("gb2312"), "ISO8859-1")); response.setContentType("application/octet-stream"); ServletOutputStream output = response.getOutputStream(); InputStream fis = null; byte[] buffer = new byte[1204]; int byteRead; try { fis = new FileInputStream(path); while ((byteRead = fis.read(buffer)) != -1) { output.write(buffer, 0, byteRead); } output.flush(); } catch (Exception e) { String simplename = e.getClass().getSimpleName(); if(!"ClientAbortException".equals(simplename)) // 忽略用户点击下载取消按钮时的警告异常 { response.setContentType("text/html; charset=utf-8"); response.setHeader("Content-disposition", "inline"); output.println(" <HTML> <BODY> <P>"); output.println("文件下载发生错误!"); output.println(" </P> </BODY> </HTML>"); output.close(); } } finally { if (fis != null) { fis.close(); } } } else { msg = "找不到文件,该资料可能未上传"; } response.getWriter().write(msg); response.getWriter().flush(); response.getWriter().close(); return null;直接请求action,action里面大概这样写就行,你看着改改 求JSP编写的登录界面源代码!!! 关于BeanUtils.copyProperties()的问题,急! JSP里边怎么样在加载一个窗口后自动刷新一下?谢谢了! 怎么获取客户端文件(如:图片等) JAVA的JS正则问题,帮帮忙 谢谢 struts中,如何阻止用户走静态页面进入? UNIX下绝对路径问题 我做了一个bbs写入英文很正常可是写入中文就显示的都是问号阿 高人们我该怎么办啊 jsp配置问题 坐等大神求解答~~~~~ OpenbravoERP添加POS Synchronization WebService模块出现的异常 jsp怎么遍历2个list?
你会实现下载么?如果会,那你会ajax么?两个都会了不就ok了
DaoIn in = new DaoIn();
att.setAttachment_types(typeid);
att.setItem_id(itemid);
in.setData(att);
DaoOut out = DaoController.execute(D_attachmentS02Dao.class, in);
F_upload_file filedb = new F_upload_file();
filedb = (F_upload_file) out.getData();
//下载
String filePath = filedb.getFile_url();
String filetype = filePath.substring(filePath.lastIndexOf('.'));
String msg = "";
if(filePath != null && !"".equals(filePath))
{
String path = getServlet().getServletContext().getRealPath("")+filePath;//找到文件
response.addHeader("Content-Disposition", "attachment; filename="+new String((filename+filetype).getBytes("gb2312"), "ISO8859-1"));
response.setContentType("application/octet-stream");
ServletOutputStream output = response.getOutputStream();
InputStream fis = null;
byte[] buffer = new byte[1204];
int byteRead;
try
{
fis = new FileInputStream(path);
while ((byteRead = fis.read(buffer)) != -1)
{
output.write(buffer, 0, byteRead);
}
output.flush();
}
catch (Exception e)
{
String simplename = e.getClass().getSimpleName();
if(!"ClientAbortException".equals(simplename)) // 忽略用户点击下载取消按钮时的警告异常
{
response.setContentType("text/html; charset=utf-8");
response.setHeader("Content-disposition", "inline");
output.println(" <HTML> <BODY> <P>");
output.println("文件下载发生错误!");
output.println(" </P> </BODY> </HTML>");
output.close();
}
}
finally
{
if (fis != null)
{
fis.close();
}
}
}
else
{
msg = "找不到文件,该资料可能未上传";
}
response.getWriter().write(msg);
response.getWriter().flush();
response.getWriter().close();
return null;直接请求action,action里面大概这样写就行,你看着改改