java - 如何在java中刷新dns

标签 java windows dns ip flush

我目前正在尝试找到一种通过 java 程序刷新我的 dns 缓存的方法。当我执行代码时,会出现命令提示符,但我不知道如何执行我的代码。

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
  istrm_ = istrm;
  ostrm_ = ostrm;
 }
  public void run() {
  try
  {
      final byte[] buffer = new byte[1024];
      for (int length = 0; (length = istrm_.read(buffer)) != -1; )
      {
          ostrm_.write(buffer, 0, length);
      }
  }
  catch (Exception e)
  {
      e.printStackTrace();
  }
}

private final OutputStream ostrm_;
private final InputStream istrm_;
}
public class FlushCommand {

FlushCommand(InputStream errorStream, PrintStream err) {
    throw new UnsupportedOperationException("Not supported yet."); 
}

}

这是下面的主要方法:

    String command = "cmd /c start cmd.exe";
    {

};
Process p = null;
    try {// Execute command

Process child = Runtime.getRuntime().exec(command);

// Get output stream to write from it
OutputStream out = child.getOutputStream();

out.write("cd C:/ /r/n".getBytes());
out.flush();
out.write("dir /r/n".getBytes());
out.close();
        p = Runtime.getRuntime().exec(command);
    } catch (IOException ex) {
        Logger.getLogger(FlushDNS.class.getName()).log(Level.SEVERE, null, ex);
    }
new Thread((Runnable) new FlushCommand(p.getErrorStream(), System.err)).start();
new Thread((Runnable) new FlushCommnad(p.getInputStream(), System.out)).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());
stdin.println("dir c:\\ /A /Q");
stdin.println("ipconfig/flushdns");
// write any other commands you want here
stdin.close();
int returnCode = 0;
    try {
        returnCode = p.waitFor();
    } catch (InterruptedException ex) {
        Logger.getLogger(FlushDNS.class.getName()).log(Level.SEVERE, null, ex);
    }
System.out.println("Return code = " + returnCode);
}                                        

最佳答案

为什么不从最基本的开始呢?

您的命令只是打开一个命令窗口。如果我没记错的话,以下内容应该刷新您的 dns 并保持未关闭状态(cmd 不会自动关闭)。

String command = "cmd.exe /c start cmd.exe /c start ipconfig /flushdns";
try{
      Runtime.getRuntime.exec(command);
catch(...){}

我必须检查计算机中的旧代码以确保确定,但我建议您从基础开始,然后进行多线程等。

或者,您可以使用进程构建器来创建命令。

关于java - 如何在java中刷新dns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42801368/

相关文章:

ssl - Cnames 和 ssl - 如果两个名称都有 ssl 好吗?

heroku - 如何将 GoDaddy 子域指向 Heroku 应用程序?

java - java中的线程

java - 请求范围的 ApplicationEventListener 无法接收事件

java - 在 SQLite 表中使用复合主键设置 ListView 适配器

java - 使用 spring-thymeleaf 访问文本区域

windows - 批处理文件标记忽略空分隔符

Windows 10 - 如何在没有触摸屏的情况下测试触摸事件?

c++ - 如何在代码块 (Windows) 的 C 项目中包含 curl 库?

dns - 如何查询.local bonjour DNS条目?