当前位置: 代码迷 >> Java Web开发 >> 这个提示如何避免
  详细解决方案

这个提示如何避免

热度:172   发布时间:2016-04-16 21:58:51.0
这个提示怎么避免?
我做的是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);
  相关解决方案