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

Servlet下载xml文件,不弹窗了
我已经读取到了xml文件内容,代码如下:
realPath = new String(realPath.getBytes("ISO-8859-1"), "GB2312");
filename = URLEncoder.encode(filename, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("ISO-8859-1"), "UTF-8"));
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(new File(realPath));
} catch (DocumentException e) {
e.printStackTrace();
}
response.getWriter().write(document.asXML().toString());
System.out.println(document.asXML().toString());

但是前台没有弹窗提示选择保存路径,要怎么处理这个问题呢?各位指点一下啦!谢谢!
java servlet xml

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

response.setContentType("application/x-msdownload");
            response.addHeader("Content-Disposition", "attachment;filename="
                + new String(filename.getBytes("ISO-8859-1"), "UTF-8"));
            InputStream inputStream = new FileInputStream(new?File(realPath));
            OutputStream outputStream = response.getOutputStream();
            byte[] buffer = new byte[1024];
            int i;
            while ((i = inputStream.read(buffer)) != -1) {
                len += i;
                outputStream.write(buffer, 0, i);
                outputStream.flush();
            }
            response.setContentLength(len);
            outputStream.close();
            inputStream.close();