这个提示怎么避免?

Gemerl 2014-08-21 10:33:55
我做的是excel导出。。
我用的是先保存要导出的数据到服务器的指定位置。 然后直接访问路径下载下来。。
问题是有提示下载但是点击下载并没有下载。。而是重新点击导出才会真正的下载。。下有截图
图片总是传不上来。。 看这个图片地址吧。。http://chuantu.biz/t/15/1408588112x-954497572.jpg
下面是我的源码 纯jsp。。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page language="java" import="org.apache.poi.*"%>
<%@page import="java.io.*"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCellStyle"%>
<%@page import="org.apache.poi.hssf.util.HSSFColor"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%!class User {
private int id;
private String name;
private String address;

public int getId() {
return id;
}

public String getName() {
return name;
}

public String getAddress() {
return address;
}

public void setId(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setAddress(String address) {
this.address = address;
}
}

public List<User> getAllUser() {
List<User> users = new ArrayList<User>();
User u1 = new User();
u1.id = 1;
u1.name = "张三";
u1.address = "北京";

User u2 = new User();
u2.id = 2;
u2.name = "李四";
u2.address = "武汉";

User u3 = new User();
u3.id = 3;
u3.name = "王五";
u3.address = "上海";
users.add(u1);
users.add(u2);
users.add(u3);
return users;
}

public User getUserById(List<User> users, int id) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).id == id) {
return users.get(i);
}
}
return null;
}

public void download(File file, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
//File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response
.getOutputStream());
response.setContentType("application/vnd.ms-excel;charset=gb2312");
toClient.write(buffer);
toClient.flush();
fis.close();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}%>
<%
List<User> uus = getAllUser();
List<User> users = new ArrayList<User>();
String method = request.getParameter("method");
if (method != null && method.equals("export")) {//导出
String date_ = request.getParameter("strData");
String path_ = session.getServletContext().getRealPath("/temp/export.xls");
File file = new File(path_);
if (null != date_ && !"".equals(date_)) {
String ids[] = date_.split(",");
for (int i = 0; i < ids.length; i++) {
User u = getUserById(uus, Integer.valueOf(ids[i]));
users.add(u);
}
if (users.size() > 0) {
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet("新手记录");
HSSFCellStyle style = book.createCellStyle();
style.setBorderBottom(HSSFCellStyle.ALIGN_CENTER);
style.setBorderLeft(HSSFCellStyle.ALIGN_CENTER);
style.setBorderRight(HSSFCellStyle.ALIGN_CENTER);
style.setBorderTop(HSSFCellStyle.ALIGN_CENTER);
style.setFillForegroundColor(HSSFColor.DARK_YELLOW.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

HSSFCellStyle style2 = book.createCellStyle();
style2.setBorderBottom(HSSFCellStyle.ALIGN_CENTER);
style2.setBorderLeft(HSSFCellStyle.ALIGN_CENTER);
style2.setBorderRight(HSSFCellStyle.ALIGN_CENTER);
style2.setBorderTop(HSSFCellStyle.ALIGN_CENTER);

HSSFRow titleRow = sheet.createRow(0);
HSSFCell noTitle = titleRow.createCell(0,HSSFCell.CELL_TYPE_STRING);
HSSFCell nameTitle = titleRow.createCell(1,HSSFCell.CELL_TYPE_STRING);
HSSFCell addressTitle = titleRow.createCell(2,HSSFCell.CELL_TYPE_STRING);
noTitle.setCellValue("编号");
nameTitle.setCellValue("姓名");
addressTitle.setCellValue("地址");
noTitle.setCellStyle(style);
nameTitle.setCellStyle(style);
addressTitle.setCellStyle(style);


for (int i = 0; i < users.size(); i++) {
HSSFRow contentRow = sheet.createRow(i + 1);
User uu = users.get(i);
HSSFCell contentCell_1 = contentRow.createCell(0,
HSSFCell.CELL_TYPE_NUMERIC);
HSSFCell contentCell_2 = contentRow.createCell(1,
HSSFCell.CELL_TYPE_STRING);
HSSFCell contentCell_3 = contentRow.createCell(2,
HSSFCell.CELL_TYPE_STRING);
contentCell_1.setCellValue(uu.getId());
contentCell_2.setCellValue(uu.getName());
contentCell_3.setCellValue(uu.getAddress());
contentCell_1.setCellStyle(style2);
contentCell_2.setCellStyle(style2);
contentCell_3.setCellStyle(style2);
}
FileOutputStream os = new FileOutputStream(file);
book.write(os);
os.close();
}
}
String downPath=basePath+"temp/"+file.getName();
//System.out.print(downPath);
PrintWriter pw = response.getWriter();
pw.write("{'mes':'ok','downPath':'"+downPath+"'}");
return;

//response.sendRedirect(basePath+"temp/"+file.getName());
//out.print("<script>document.location.href='"+basePath+"temp/"+file.getName()+"';</script>");
//download(file, response);
//System.out.print(file.getName());
//out.clear();
//out = pageContext.pushBody();

}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>excel导出测试</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
table{background-color:blue;}
td{background-color:white;}
</style>
<script type="text/javascript" src="<%=basePath%>js/jquery.js"></script>
<script type="text/javascript">
$(function(){
$("#box_").click(function(){
if($(this).attr("checked")==true){
$("input[name='box']").attr("checked",true);
}else{
$("input[name='box']").attr("checked",false);

}
});
$("#box_1").click(function(){
if($(this).attr("checked")==true){
$("input[name='box']").each(function(){
if($(this).attr("checked")==true){
$(this).attr("checked",false);
}else{
$(this).attr("checked",true);
}
});
}
});
});

function in_(){
document.location.href='<%=basePath%>index2.jsp';
}
function out(){
var boxs=$("input[name='box'][checked]");
var arr=new Array();
var strData="";
$(boxs).each(function(){
var parent=$(this).parent().parent().children('td');
var id=$(parent).eq(1).text();
arr.push(id);
});
if(arr.length>0){//有选中的条目
strData=arr.join();
$.ajax({
type:"post",
url:"<%=basePath%>index.jsp",
data:{"strData":strData,"method":"export"},
success:function(data){
var message=eval("("+data+")");
if(message.mes=='ok'){
alert(message.mes);
document.location.href=message.downPath;
}
}
});
}
}
</script>
</head>

<body>
<button onclick="out()">导出选中的条目</button><button onclick="in_()">导入选中的条目</button>
<table cellpadding="1" cellspacing="1" width="800" bordercolor="#0000FF" on>
<tr><td><input type="checkbox" id="box_"/>全选/全不选  <input type="checkbox" id="box_1"/>反选</td><td>编号</td><td>姓名</td><td>地址</td></tr>
<%
for (int i = 0; i < uus.size(); i++) {
%>
<tr><td><input type="checkbox" name="box"/></td><td><%=uus.get(i).id%></td><td><%=uus.get(i).name%></td><td><%=uus.get(i).address%></td></tr>
<%
}
%>
</table>
</body>
</html>


...全文
630 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shixitong 2014-08-21
  • 打赏
  • 举报
回复
把IE的安全级别调低,或者关掉 http://blog.sina.com.cn/s/blog_53873d95010184j6.html
Gemerl 2014-08-21
  • 打赏
  • 举报
回复
找到在火狐下不下载的原因。。 var boxs=$("input[name='box'][checked]"); 这种语法在ie中支持 火狐不支持。。 $("input[name='box']:checked");这种就支持火狐和ie
Gemerl 2014-08-21
  • 打赏
  • 举报
回复
另外发现个问题 我这些js好像在火狐 out()这个方法中都没作用。。 这段好像不起作用

$(boxs).each(function(){                 var parent=$(this).parent().parent().children('td');                 var id=$(parent).eq(1).text();                 arr.push(id);             }); 

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧