jsp实现文件下载时出现的问题,各位大虾帮忙啊!解决100分奉上!
小妹用jsp中实现文件下载,weblogic作服务器,在jbuilder中开发。
程序如下:
<%@ page contentType= "text/html; charset=gb2312 " %> <%@ page import= "java.io.* " %> <html> <head> <title> 文件下载 </title> </head> <%
String czydm=session.getAttribute( "swrydm ").toString();
try{String filename=request.getParameter( "filename ");
response.setContentType( "unknown ");
response.setHeader( "Content-disposition ", "attachment; filename= "+new String(filename.getBytes( "gb2312 "), "iso8859-1 "));
String url= "d:/xtld_upload/ "+czydm+ "/ "+filename;
//ServletOutputStream os = response.getOutputStream();
java.io.OutputStream os=response.getOutputStream();
FileInputStream in = new FileInputStream(url);
byte[] data = new byte[4096];
int temp = 0;
while((temp=in.read(data))> 0){
os.write(data,0,temp);
os.flush();
}
in.close();
os.close();
}
catch(Exception e) {out.print(e.toString()); }%> </html>
下载rar文件和文本文件保存到本地后,打开正常;但是其他类型的文件,如word、excel等下载到本地后,打开为乱码。各位高手多多指点啊,好几天了,快疯了!
或提供一段测试成功的代码,感激不尽!!
[解决办法]
试试字符流把!
[解决办法]
<%@ page contentType= "text/html; charset=gb2312 " %> <%@ page import= "java.io.* "
%> <%
String czydm=session.getAttribute( "swrydm ").toString();
try{String filename=request.getParameter( "filename ");
response.setContentType( "unknown ");
response.setHeader( "Content-disposition ", "attachment; filename= "+new String(filename.getBytes( "gb2312 "), "iso8859-1 "));
String url= "d:/xtld_upload/ "+czydm+ "/ "+filename;
//ServletOutputStream os = response.getOutputStream();
java.io.OutputStream os=response.getOutputStream();
FileInputStream in = new FileInputStream(url);
byte[] data = new byte[4096];
int temp = 0;
while((temp=in.read(data))> 0){
os.write(data,0,temp);
os.flush();
}
in.close();
os.close();
}
catch(Exception e) {out.print(e.toString()); }%>