java 的 url类就是干这事的(专门干这事) 看看java的文档你就知道了

解决方案 »

  1.   

    这是我写控制tomcat重新reload的程序,你研究一下,我想有一点你得注意:就是有没有访问权限控制如果有要实现Authenticator:
    import java.io.BufferedReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.io.StringWriter;
    import java.net.Authenticator;
    import java.net.MalformedURLException;
    import java.net.PasswordAuthentication;
    import java.net.URL;
    import java.text.SimpleDateFormat;
    import java.util.Date;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;/**
     * <p>Title: Tomcat 管理类</p>
     * <p>Description: 提供tomcat管理器功能</p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: Wonders Information Co.,Ltd.</p>
     * @author luzhilin
     * @version 1.0
     */
    public class TomcatManager extends Authenticator implements Runnable {
    private static TomcatManager instance = null; private static final String PROTOCOL = "http://";
    private String serverIp = "localhost";
    private String port = "9090";
    private String path = "/";
    private String username = "admin";
    private String password = "";
    private String remoteHost = "localhost";
    private String urlLink = ""; /**
     * 单态同步加锁
     * @return TomcatManager
     */
    public static synchronized TomcatManager getInstance() {
    if (instance == null) {
    instance = new TomcatManager();
    }
    return instance;
    } //用户调用的重新装载方法
    public synchronized void relod(
    HttpServletRequest request,
    HttpServletResponse response) {
    //提供信使服务
    if (null != request) {
    remoteHost = request.getRemoteHost();
    } //给用户提示页面
    if (null != response) {
    giveUserTips(response);
    } //执行reload操作
    new Thread(instance).start();
    } //重新装载指定app
    private String reloadContext() {
    //安装 Authenticator
    Authenticator.setDefault(instance);
    //连接执行命令
    urlLink =
    PROTOCOL + serverIp + ":" + port + "/manager/reload?path=" + path; return getURLContext();
    } //获得连接指令返回值
    private String getURLContext() {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw); try {
    URL url = new URL(urlLink);
    InputStream content = (InputStream) url.getContent();
    BufferedReader in =
    new BufferedReader(new InputStreamReader(content));
    String line;
    while ((line = in.readLine()) != null) {
    pw.println(line);
    }
    } catch (MalformedURLException e) {
    pw.println("Invalid URL");
    } catch (IOException e) {
    pw.println("Error reading URL");
    } return sw.toString();
    } //复写Authenticator的方法getPasswordAuthentication()
    protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(username, password.toCharArray());
    } //实作Runnable的方法run()
    public void run() {
    long begin = System.currentTimeMillis();
    String rtnMsg = reloadContext();
    if (null != rtnMsg) {
    long end = System.currentTimeMillis(); try {
    //log
    writeLog(urlLink, begin, end, rtnMsg);
    //msg
    if ("OK".equals(rtnMsg.substring(0, 2))) {
    sendMsg(remoteHost, "服务器启动成功,请重新登陆!");
    } else {
    sendMsg(remoteHost, "服务器启动失败,请联系系统管理员!");
    }
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    } //信使服务
    private void sendMsg(String aHost, String aMsg) throws IOException {
    Runtime.getRuntime().exec("net send " + aHost + " " + aMsg);
    } //操作日志
    private void writeLog(
    String url,
    long beginTime,
    long endTime,
    String aMsg)
    throws IOException {
    String currTime = new SimpleDateFormat("HH:mm:ss").format(new Date());
    String currDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    String fileName = "reload_wdhy." + currDate + ".log";
    String pathName = System.getProperty("user.dir") + "\\" + fileName; FileWriter fw = new FileWriter(pathName, true);
    PrintWriter pw = new PrintWriter(fw); pw.println("[" + currTime + "] URL      " + url);
    pw.println("[" + currTime + "] BEGIN    " + beginTime);
    pw.println("[" + currTime + "] EDN      " + endTime);
    pw.println("[" + currTime + "] INTERVAL " + (endTime - beginTime));
    pw.println("[" + currTime + "] MESSAGE  " + aMsg); pw.close();
    fw.close();
    } //给用户的提示界面
    private void giveUserTips(HttpServletResponse response) {
    response.setContentType("text/html;charset=GBK");
    PrintWriter out = null;
    try {
    out = response.getWriter(); out.write("<html>\r\n");
    out.write("<head>\r\n");
    out.write("<title>重要提示</title>\r\n");
    out.write("<style type=\"text/css\">\r\n");
    out.write("<!--\r\n");
    out.write(".style1 {color: #000000}\r\n");
    out.write("-->\r\n");
    out.write("</style>\r\n");
    out.write("</head>\r\n");
    out.write("\r\n");
    out.write("<body bgcolor=\"#E0F5F8\">\r\n");
    out.write("<p>&nbsp;</p>\r\n");
    out.write("<p>&nbsp;</p>\r\n");
    out.write(
    "<table width=\"50%\" border=\"0\" align=\"center\" cellspacing=\"0\" style=\"border:1 solid #999999\">\r\n");
    out.write("  <tr> \r\n");
    out.write(
    "    <td height=\"41\" colspan=\"2\" style=\"border:1 solid #999999\"><div align=\"center\" class=\"style1\"><font size=\"5\" face=\"宋体\">实在抱歉:数据库网络出错</font></div></td>\r\n");
    out.write("  </tr>\r\n");
    out.write("  <tr> \r\n");
    out.write(
    "    <td width=\"23%\" rowspan=\"2\" style=\"border:1 solid #999999\"><div align=\"center\"><font color=\"333333\" size=\"3\" face=\"宋体\">解决方法</font></div></td>\r\n");
    out.write(
    "    <td width=\"77%\" height=\"40\" style=\"border:1 solid #999999\"><font color=\"333333\" size=\"3\" face=\"宋体\"> 1、等待5分钟后,</font><font size=\"3\" face=\"宋体\"><font color=\"#FF0000\">重新登陆</font></font></td>\r\n");
    out.write("  </tr>\r\n");
    out.write("  <tr> \r\n");
    out.write(
    "    <td height=\"40\" style=\"border:1 solid #999999\"><font color=\"333333\" size=\"3\" face=\"宋体\"> 2、等待信使通知,</font><font color=\"#FF0000\" size=\"3\" face=\"宋体\">重新登陆</font></td>\r\n");
    out.write("  </tr>\r\n");
    out.write("</table>\r\n");
    out.write("</body>\r\n");
    out.write("</html>\r\n");
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    } public String getPort() {
    return port;
    } public void setPort(String port) {
    if (null != port && 0 != port.length()) {
    this.port = port;
    }
    } public String getPath() {
    return path;
    } public void setPath(String path) {
    if (null != path && 0 != path.length()) {
    this.path = path;
    }
    } public String getServerIp() {
    return serverIp;
    } public void setServerIp(String serverIp) {
    if (null != serverIp && 0 != serverIp.length()) {
    this.serverIp = serverIp;
    }
    } public String getUsername() {
    return username;
    } public void setUsername(String username) {
    if (null != username && 0 != username.length()) {
    this.username = username;
    }
    } public String getPassword() {
    return password;
    } public void setPassword(String password) {
    if (null != password && 0 != password.length()) {
    this.password = password;
    }
    }
    }写一个main(),测试一下。
      

  2.   

    你去看看urlconnection的这个东西好了,会有点启发的
      

  3.   


    建议:改receive.jsp为receiveServlet.java,这样处理数据很灵活,也很方便。通过URL访问比较简单,代码如下:
    try {
         StringBuffer strFileInfoBuf = new StringBuffer();     String strProcessURL =
                    "http://127.0.0.1/myweb/receive.jsp"
                        + "?FLAG=sendfile&name=java";     //connect server
         URL objURL = new URL(strProcessURL);
         //open connect
         URLConnection objConn = objURL.openConnection();
         objConn.setDoOutput(true); 
         //get server return message
         //BufferedInputStream objInput = new BufferedInputStream(objConn.getInputStream());
         StringBuffer strTempBuf = new StringBuffer();
         byte[] byteArray = new byte[2048];
         int nReadCount = -1;
         BufferedOutputStream objOutput =
                    new BufferedOutputStream(objConn.getOutputStream());
         FileInputStream objFileIn = new FileInputStream("c:\\test.txt");
         while ((nReadCount = objFileIn.read(byteArray)) != -1) {
              objOutput.write(byteArray, 0, nReadCount);
         }
         objOutput.flush();
         objFileIn.close();
         objOutput.close(); } catch (Exception e) {
         e.printStackTrace();
    }