ITeye博客

jsp中出现对话框提示保存来自一个网页的内容的主要代码

icarusliu · 2008-10-09 15:36 · web · Java · .net · XML · JSP · Markdown

<%@page import="java.io.*,java.net.*"%><%
 URL url = new URL(
   "http://localhost:8080/axis2/services/GHMLService/getByAuthorName?authorName=liuqi");
 URLConnection conn = url.openConnection();
 
 conn.setDoOutput(true);
 InputStream is = conn.getInputStream();
 
 java.io.BufferedInputStream bis = null;
 java.io.BufferedOutputStream bos = null;
 try {
  String filename = "data1.xml";
  filename = new String(filename.getBytes("iso8859-1"), "gb2312");
  response.setContentType("application/x-msdownload");
  response.setHeader("Content-disposition","attachment; filename="+new String(filename.getBytes("gb2312"),"iso8859-1"));

  bis = new java.io.BufferedInputStream(is);
  bos = new java.io.BufferedOutputStream(response.getOutputStream());
  
  byte[] buff = new byte[2048];
  int bytesRead;
  while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
   bos.write(buff, 0, bytesRead);
  }
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  if (bis != null)
   bis.close();
  if (bos != null)
   bos.close();
 }
%>

 

此为download.jsp网页的内容,注意该网页中应该不含有多余的空格

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。