public class Internet {
public static boolean verificarConexao() {
try {
java.net.URL google = new java.net.URL("http://www.google.com");
java.net.URLConnection conn = google.openConnection();
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) conn;
httpConn.connect();
int x = httpConn.getResponseCode();
if (x == 200) {
return true;
}
} catch (MalformedURLException ex) {
Logger.getLogger(Internet.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (IOException ex) {
Logger.getLogger(Internet.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return false;
}
public static boolean verificarConexao(String url) {
try {
java.net.URL teste = new java.net.URL(url);
java.net.URLConnection conn = teste.openConnection();
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) conn;
httpConn.connect();
int x = httpConn.getResponseCode();
if (x == 200) {
return true;
}
} catch (MalformedURLException ex) {
Logger.getLogger(Internet.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (IOException ex) {
Logger.getLogger(Internet.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
return false;
}
}
sábado, 2 de maio de 2015
Classe para exibir a Memória RAM do Sistema
public class MemoriaRAM {
private static com.sun.management.OperatingSystemMXBean mxbean;
static {
mxbean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
}
public String getStringRAM(){
String ram = getMemoriaLivreBytes() + "#";
ram += getMemoriaLivreKBytes() + "#";
ram += getMemoriaLivreMBytes() + "#";
ram += getMemoriaLivreGBytes();
return ram;
}
public static long getMemoriaTotalBytes(){
return mxbean.getTotalPhysicalMemorySize();
}
public static long getMemoriaTotalKBytes(){
return mxbean.getTotalPhysicalMemorySize()/1024;
}
public static long getMemoriaTotalMBytes(){
return Math.round(mxbean.getTotalPhysicalMemorySize()/1024/1024);
}
public static long getMemoriaTotalGBytes(){
return Math.round(mxbean.getTotalPhysicalMemorySize()/1024/1024/1024);
}
public static long getMemoriaLivreBytes(){
return mxbean.getFreePhysicalMemorySize();
}
public static long getMemoriaLivreKBytes(){
return mxbean.getFreePhysicalMemorySize()/1024;
}
public static long getMemoriaLivreMBytes(){
return mxbean.getFreePhysicalMemorySize()/1024/1024;
}
public static long getMemoriaLivreGBytes(){
return mxbean.getFreePhysicalMemorySize()/1024/1024/1024;
}
public static OperatingSystemMXBean getMxbean() {
return mxbean;
}
}
private static com.sun.management.OperatingSystemMXBean mxbean;
static {
mxbean = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
}
public String getStringRAM(){
String ram = getMemoriaLivreBytes() + "#";
ram += getMemoriaLivreKBytes() + "#";
ram += getMemoriaLivreMBytes() + "#";
ram += getMemoriaLivreGBytes();
return ram;
}
public static long getMemoriaTotalBytes(){
return mxbean.getTotalPhysicalMemorySize();
}
public static long getMemoriaTotalKBytes(){
return mxbean.getTotalPhysicalMemorySize()/1024;
}
public static long getMemoriaTotalMBytes(){
return Math.round(mxbean.getTotalPhysicalMemorySize()/1024/1024);
}
public static long getMemoriaTotalGBytes(){
return Math.round(mxbean.getTotalPhysicalMemorySize()/1024/1024/1024);
}
public static long getMemoriaLivreBytes(){
return mxbean.getFreePhysicalMemorySize();
}
public static long getMemoriaLivreKBytes(){
return mxbean.getFreePhysicalMemorySize()/1024;
}
public static long getMemoriaLivreMBytes(){
return mxbean.getFreePhysicalMemorySize()/1024/1024;
}
public static long getMemoriaLivreGBytes(){
return mxbean.getFreePhysicalMemorySize()/1024/1024/1024;
}
public static OperatingSystemMXBean getMxbean() {
return mxbean;
}
}
Assinar:
Comentários (Atom)