用JSP实现文件(excel和text)的下载的代码?详细的......
解决方案 »
- 请问jquery如何用ajax与struts2交互
- 新手求解!!!!!!!!!!
- jsp 自定义标签的问题
- JS代码里怎么加编码方式,在线急用
- 创建sessionFactory失败,java.lang.UnsupportedClassVersionError:/hibernate/cfg/SecondPass (Unsupported major.minor version 49.0)
- 关于resin的配置
- 怎么把字符串"c:\abc"用函数转换成c:\\abc
- 大神们,求解,关于插入数据库出现的问题
- 谁知道哪里有免费支持jsp的空间?能用一个月就行。 找了半天,没一个是可用的。
- 如何按我需要的格式显示从数据库中抓出的时间
- 请教一个常用的功能,怎样在操作完成后回到提交申请的页面?
- JS的问题
//-------下载Excel------------
File file=null;
InputStream ins=null;
String fileName=null;
int fileSize=0;
file=new File(path+"\\Excel\\"+excelName);
ins=new FileInputStream(file);
fileName=file.getName();
fileName=new String(fileName.getBytes(),"iso-8859-1");
fileSize=(int)file.length();
response.setContentType("application/x-msdownload");
String str = "attachment; filename="+fileName; //文件名
response.setHeader("Content-Disposition", str);
response.setContentLength(fileSize);//大小
ServletOutputStream sos=response.getOutputStream();
byte[] data = new byte[2048];
int len = 0;
while((len=ins.read(data)) >0)
{
sos.write(data,0,len);
}
ins.close();
sos.close();
file.delete(); 放到servlet中 ..下载excel
{
//这里你可以做些其他的事情
response.setContentType("application/vnd.ms-excel"); // Word 对应的就是 application/msword
response.setHeader("Content-disposition","attachment; filename=abc.xls");
//这里的abc是保存时的默认文件名
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// bis = new BufferedInputStream(new FileInputStream(getServletContext().getRealPath("" + filename)));
bis = new BufferedInputStream(new FileInputStream("D:\\23.xls"));
bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048];
int bytesRead; while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
} } catch(final IOException e) {
System.out.println ( "出现IOException." + e );
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
return; }
catch (Exception e)
{
System.out.println(e);
}
代码如下:
OutputStream o=response.getOutputStream();
byte b[]=new byte[500];
File fileLoad=new File("e:/test.txt");
response.setContentType("application/octet-stream");
response.setHeader("content-disposition","attachment; filename=text.txt");
long fileLength=fileLoad.length();
String length1=String.valueOf(fileLength);
response.setHeader("Content_Length",length1);
FileInputStream in=new FileInputStream(fileLoad);
int n;
while((n=in.read(b))!=-1){
o.write(b,0,n);
}
in.close();
out.clear();
out = pageContext.pushBody();
2 下载excel文件
<%@page language="java" import="java.sql.*,java.io.*,java.net.URLEncoder,java.util.*" pageEncoding="gb2312"%>
<%
OutputStream o=response.getOutputStream();
byte b[]=new byte[1024];
File fileLoad=new File("e:/dzl.xls");
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("content-disposition","attachment; filename=text.xls");
long fileLength=fileLoad.length();
String length1=String.valueOf(fileLength);
response.setHeader("Content_Length",length1);
FileInputStream in=new FileInputStream(fileLoad);
int n;
while((n=in.read(b))!=-1){
o.write(b,0,n);
}
in.close();
out.clear();
out = pageContext.pushBody();
%>
在web.xml下配置:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
//配置excel下载
<mime-mapping>
<extension>xls</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>
//配置word下载
<mime-mapping>
<extension>doc</extension>
<mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
</web-app>