이야 .. 해당하는 URL에 NET_URL
및 LOCAL_URL
상수를 설정, 물론
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class BrowserWidget {
private final String NET_URL = "http://eclipse.org";
private final String LOCAL_URL = "file:///index.html";
private String url = "";
public BrowserWidget() {
Display display = new Display();
final Shell shell = new Shell(display);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
shell.setLayout(gridLayout);
final Browser browser;
try {
browser = new Browser(shell, SWT.NONE);
} catch (SWTError e) {
System.out.println("Could not instantiate Browser: " + e.getMessage());
display.dispose();
return;
}
GridData data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.verticalAlignment = GridData.FILL;
data.horizontalSpan = 3;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
browser.setLayoutData(data);
shell.open();
if(isInternetReachable()) browser.setUrl(NET_URL);
else browser.setUrl(LOCAL_URL);
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private boolean isInternetReachable() {
HttpURLConnection urlConnect = null;
try {
// make a URL to a known source
URL url = new URL(NET_URL);
// open a connection to that source
urlConnect = (HttpURLConnection) url.openConnection();
// trying to retrieve data from the source. If there is no connection, this line will fail
urlConnect.getContent();
} catch (UnknownHostException e) {
return false;
} catch (IOException e) {
return false;
} finally {
// cleanup
if(urlConnect != null) urlConnect.disconnect();
}
return true;
}
public static void main(String[] args) {
new BrowserWidget();
}
}
(나는 또한 브라우저 위젯의 조각을 사용) 전체 코드입니다 :
내 코드는 대체
는
~ NOINTERNETURL ~ '기본'지역의 URL로 (내 SWT 브라우저 삶을 감지 L URL 및 표시 해당 페이지)
~ INTERNETURL ~ 내가 내 서버
~ INTERNETTESTURL ~ 기본적으로 내 서버에서 스크립트의 URL "VAR에 연결할 수 있는지에 가고 싶은 페이지로 HasInternet = true "
<html>
<head>
<meta http-equiv="refresh" content="5;url=~NOINTERNETURL~">
</head>
<body>
<script src="~INTERNETTESTURL~" type="text/javascript"></script>
<script type="text/javascript">
if (typeof HasInternet === 'undefined') {
window.location.href = "~NOINTERNETURL~";
}
else {
window.location.href = "~INTERNETURL~";
}
</script>
<div>
Testing internet connection...
</div>
</body>
</html>