日期:2014-05-18  浏览次数:20775 次

从服务器上下载txt文件后,txt文件比源文件多了html的内容。
程序如下:
FileInputStream   fis   =   new   FileInputStream(new   File( "c:\\test.txt "));
//response.setContentType( "applicaton/x-filter ");
response.setHeader( "Content-Disposition ",   "attachment;filename=test.txt " );
response.setHeader( "Content-Length ",   String
.valueOf(fis.available()));
ServletOutputStream   sos   =   response.getOutputStream();
byte[]   buffer   =   new   byte[1024];
int   location   =   0;
while   ((location   =   fis.read(buffer))   !=   -1)   {
sos.write(buffer,   0,location);
}
fis.close();
sos.flush();
sos.close();

c:\\text.txt文件内容为abc,但下载到本地上文件的内容如下:
abc
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN ">


<HTML>
<HEAD>


<META   http-equiv= "Content-Type "   content= "text/html;   charset=UTF-8 ">
<META   name= "GENERATOR "   content= "IBM   Software   Development   Platform ">
<META   http-equiv= "Content-Style-Type "   content= "text/css ">
<LINK   href= "../theme/Master.css "   rel= "stylesheet "   type= "text/css ">
<TITLE> Download.jsp </TITLE>
<LINK   rel= "stylesheet "   type= "text/css "   href= "../theme/stylesheet.css "
title= "Style ">
</HEAD> <BODY> </BODY> </HTML>
请问各位高手,如何解决?如何去掉自动添加的html的部分?


------解决方案--------------------


给大家推荐个

好的技术群  大家一起学习啊

31443944
------解决方案--------------------
顶一下,学习,
------解决方案--------------------
ServletOutputStream sos = response.getOutputStream();
sos.clear()或者什么的
清空一下buffer
------解决方案--------------------
response.setContentType(”APPLICATION/OCTET-STREAM”);

------解决方案--------------------
简单 明了 <a href
------解决方案--------------------
ServletOutputStream sos = response.getOutputStream();
servlet中的当然会是标准的html了

直接用输出流就好了
------解决方案--------------------
楼上的都说明了。我只能帮顶。
------解决方案--------------------
是在jsp中还是servlet里面写的?
------解决方案--------------------
<%@ page contentType= "text/html; charset=gb2312 " %> <%@ page import= "java.io.* " %> <%
String fileName = "kkk.txt ";
InputStream inStream=new FileInputStream( "E:/liaohaiying/cp/kk.txt ");
response.reset();
response.setContentType( "bin ");
response.addHeader( "Content-Disposition ", "attachment; filename= " + fileName);
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) > 0)
response.getOutputStream().write(b,0,len);
inStream.close();
%>
------解决方案--------------------