ie打开的话全都是乱码。谢谢各位大虾
解决方案 »
- 求助:quartz调用service报错
- spring + hibernate + struts2整合报错,请大神帮忙分析下
- 在线坐等 Tomcat突然启动不了怎么解决
- jsp弹出新窗口
- 关于window.open()的问题
- jsp如何实现在页面重定向之前弹出一个对话框?
- Linux下Tomcat配置MySQL数据源和在Windows下的配置有什么差别吗?
- 如何在一个java中把一些字符串传到另一个servlet中,只在后台操作,而不是重定向
- 如何用JSP得到当前浏览器窗口的大小?在线等待ing.....
- jsp的forward的参数直怎么传不过去啊
- 请教,怎么样才能在JSP中导出EXCEL和PDF文件时用EXCEL和Acrobt read打开,而不是嵌入在浏览器中。
- 请问:在jbuilder中开发jsp,有关中文匹配问题请教
import javax.servlet.*;
import javax.servlet.http.*;public class Download extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
try
{
new String(name.getBytes("GBK"), "ISO8859_1")
//这里你可以做些其他的事情
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"你要在保存窗口中显示的保存文件名\"");//对于中文要使用类似new String(name.getBytes("GBK"), "ISO8859_1")的写法
BufferedInputStream in = null;
ServletOutputStream out = null;
FileInputStream stream = null;
try {
out = response.getOutputStream();
stream = new FileInputStream(file); int bytesRead = 0;
final int length = 8192;
byte[] buffer = new byte[length];
while ((bytesRead = stream.read(buffer, 0, length)) != -1) {
// write at server side
out.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
throw new BPDBusiException(
ResourceConst.SCORECARD_ERR_DOWNLOADATTACHMENT_DOWNLOAD);
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
catch (Exception e)
{
System.out.println(e);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
doGet(request, response);
}
}
response.setHeader("Content-Disposition", "attachment; filename=\"你要在保存窗口中显示的保存文件名\"");//对于中文要使用类似new String(name.getBytes("GBK"), "ISO8859_1")的写法能够让你进行下载。
中间那段,可以让你传输二进制文件。