yanyaocrystal
yanyaocrystal
2015-09-21 08:13

JavaWeb导出Excel,提示格式与文件扩展名不一致

2
  • excel
  • java

下载部分代码如下
我是参考别人的代码,很多地方不懂,希望大牛解答!
<%@ page language="java" contentType = " text/html;charset=utf-8" import="java.util.*,java.io.File,java.io.*,java.net.*,com.model.*,com.controller.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


DownLoad File


<%
request.setCharacterEncoding( "utf-8" );
String targerName = new String(request.getParameter("targerName").getBytes("ISO-8859-1"), "gb2312");
String filepath="C:/Users/HP/Downloads/";
//以下红色部分最主要
String fullPath = filepath + targerName;

response.reset();
response.setContentType("application/octet-stream");
response.addHeader( "Content-Disposition", "attachment;filename=" + new String(targerName.getBytes("gb2312"), "ISO8859-1"));

OutputStream output = null;
FileInputStream fis = null;

try{
File f = new File(fullPath);
output = response.getOutputStream();
fis = new FileInputStream(f);
byte[] b = new byte[(int)f.length()];
int i = 0;
while((i = fis.read(b)) >0){
output.write(b, 0, i);
}
output.flush();
}catch(Exception e){
e.printStackTrace();
}
finally{
if(fis != null){
fis.close();
fis = null;
}
if(output != null){
output.close();
output = null;
out.clear();
out = pageContext.pushBody();
}
}
%>

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答