response.setContentType(applicationx-msdownload;charset=gb2312);
response.setHeader(Cache-Control, no-cache);
response.addHeader(Content-Disposition, attachment;filename=web.xml);
PrintWriter out = response.getWriter();
设置ContentType头字段的值为浏览器无法使用某种方式打开或激活某个程序来处理的类型.
可以为applicationx-msdownload或者applicationoctet-stream
response.setContentType(applicationx-msdownload;charset=gb2312);
设置Content-Disposition头的值为attachment;filename=文件名
response.addHeader(Content-Disposition, attachment;filename=web.xml);
从response中获得一个ServletOutputStream
ServletOutputStream out = response.getOutputStream();
从服务器上获得一个文件,这里用的是下面的web.xml文件.
String s = getServletContext().getRealPath(WEB-INFweb.xml);
File file = new File(s);
InputStream ips = new FileInputStream(file);
byte[] content = new byte[(int) file.length()];
ips.read(content);
将文件内容写入到输出流中发送给客户端
out.write((xml version=1.0 encoding=GBK ).getBytes());
out.write(DateAndTime.getBytes());
本内容试读结束,登录后可阅读更多
下载后可阅读完整内容,剩余1页未读,立即下载