java txt文件下载求助!!!
以下是我的源代码,用的servlet,前台JS发起AJAX请求,就执行这个servlet,请问为什么我的这段代码没有弹出下载框呢?运行之后也没有任何反应,求大神解答!!!包里没财富值了,见谅
?
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException,
IOException {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
String fileName = "test.txt";
File file = new File("D:\\file\\log.txt");
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition","attachment; filename="+ fileName);
response.setContentLength((int)file.length());
// Use Buffered Stream for reading/writing.
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead = 0;
// Simple read/write loop.
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
bos.flush();
}catch(Exception e){
e.printStackTrace();
} finally {
if(bis != null){
try{
bis.close();
}
catch(IOException e){
e.printStackTrace();
}
bis=null;
}
if(bos != null){
try{
bos.close();
}catch(IOException e){
e.printStackTrace();
}
bos=null;
}
}
}
------解决方案--------------------前台怎么写的?
不用ajax,直接用url请求这个servlet试下先
------解决方案--------------------用ajax直接访问倒没见过,在js中用window.open('这个servlet的地址'),开个新窗口下载