WEB开发网      濠靛倻鏅悵顖涚附閽樺鐎诲ù婊庡亾缁辨帗鎷呴悩鍨暠濞戞挴鍋撳ù鐘烘閸ㄥ孩绂嶉锝喰﹂柟瀛樺灣濠婃垿鎯冮崟顏嗩伇濞寸姾妫勬慨鈺呭礉濞戝磭骞㈤悹鍥у槻閸ㄥ孩绂嶉敓锟� ---闁挎洩鎷�
开发学院软件开发Java 发送XML文件 阅读

发送XML文件

 2007-12-23 12:35:47 来源:WEB开发网 闁靛棴鎷�闁告垵绻愰惃顒傗偓娑欍仦缂嶏拷濠⒀呭仜閵囧洨鈧稒銇炵紞锟�闁靛棴鎷�  闁稿繗娅曢弫鐐哄级閵婏缚鑸瀣仧濞堟垵顕ラ鐓庤Е
核心提示:import java.io.BufferedInputStream;import java.io.IOException;import java.io.PRintWriter;import java.net.MalformedURLException;import java.net.URL;import java.n
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.PRintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ResourceServlet extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  //get web.xml for display by a servlet
  String file = "/WEB-INF/web.xml";

  URL url = null;
  URLConnection urlConn = null;
  PrintWriter out = null;
  BufferedInputStream buf = null;
  try {
   out = response.getWriter();
   url = getServletContext().getResource(file);
   //set response header
   response.setContentType("text/xml");

   urlConn = url.openConnection();
   //establish connection with URL presenting web.xml
   urlConn.connect();
   buf = new BufferedInputStream(urlConn.getInputStream());
   int readBytes = 0;
   while ((readBytes = buf.read()) != -1)
    out.write(readBytes);
  } catch (MalformedURLException mue) {
   throw new ServletException(mue.getMessage());
  } catch (IOException ioe) {
   throw new ServletException(ioe.getMessage());
  } finally {
   if (out != null)
    out.close();
   if (buf != null)
    buf.close();
  }
 }
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  doGet(request, response);
 }
}

(出处:http://www.cncms.com)


Tags:发送 XML 文件

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接