本文整理汇总了Java中sun.net.www.protocol.file.FileURLConnection类的典型用法代码示例。如果您正苦于以下问题:Java FileURLConnection类的具体用法?Java FileURLConnection怎么用?Java FileURLConnection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FileURLConnection类属于sun.net.www.protocol.file包,在下文中一共展示了FileURLConnection类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import sun.net.www.protocol.file.FileURLConnection; //导入依赖的package包/类
public static void main(String[] args) throws IOException {
File jarFile = new File(TEST_JAR_PATH);
URL jarFileURL = jarFile.toURI().toURL();
String jarEntryUrlString = "jar:" + jarFileURL.toString() + "!/org/apache/markt/leaks/test.txt";
URL jarEntryUrl = new URL(jarEntryUrlString);
// Disable caching by default for JAR URLs
URLConnection.setDefaultUseCaches("JAR", false);
// Check caching for a URL type other than JAR
// File is the simplest
FileURLConnection fileUrlConn = (FileURLConnection) jarFileURL.openConnection();
System.out.println(fileUrlConn.getUseCaches());
fileUrlConn.connect();
System.out.println(fileUrlConn.getUseCaches());
fileUrlConn.getInputStream().close();
// Check JARs
JarURLConnection jarEntryUrlConn = (JarURLConnection) jarEntryUrl.openConnection();
System.out.println(jarEntryUrlConn.getUseCaches());
jarEntryUrlConn.connect();
System.out.println(jarEntryUrlConn.getUseCaches());
jarEntryUrlConn.getInputStream().close();
}
示例2: loadPackageMatches
import sun.net.www.protocol.file.FileURLConnection; //导入依赖的package包/类
private <T> void loadPackageMatches(Class<T> mask, Set<Class<T>> plugins)
throws IOException {
String pkg = mask.getPackage().getName();
String pkgPath = pkg.replace('.', '/');
Enumeration<URL> e = getResources(pkgPath);
while (e.hasMoreElements()) {
URL url = e.nextElement();
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection) {
processJAR(mask, plugins, (JarURLConnection) connection);
}
else if (connection instanceof FileURLConnection) {
String filePath = URLDecoder.decode(url.getPath(), "UTF-8");
String[] fileList = new File(filePath).list();
if (fileList != null) {
for (String file : fileList) {
String fileName = new File(pkgPath, file).getPath();
addIfMatch(mask, plugins, fileName);
}
}
}
}
}
示例3: ping
import sun.net.www.protocol.file.FileURLConnection; //导入依赖的package包/类
/**
* Found on http://stackoverflow.com/questions/3584210/preferred-java-way-to-ping-a-http-url-for-availability
* Pings a HTTP URL. This effectively sends a HEAD request and returns <code>true</code> if the response code is in
* the 200-399 range.
* @param url The HTTP URL to be pinged.
* @param timeout The timeout in millis for both the connection timeout and the response read timeout. Note that
* the total timeout is effectively two times the given timeout.
* @return <code>true</code> if the given HTTP URL has returned response code 200-399 on a HEAD request within the
* given timeout, otherwise <code>false</code>.
*/
public static boolean ping(String url, int timeout) {
try {
URLConnection conn = new URL(url).openConnection();
if (conn instanceof FileURLConnection) {
return true;
}
HttpURLConnection connection = (HttpURLConnection) conn;
connection.setConnectTimeout(timeout);
connection.setReadTimeout(timeout);
connection.setRequestMethod("HEAD");
int responseCode = connection.getResponseCode();
return (200 <= responseCode && responseCode <= 399);
} catch (IOException exception) {
return false;
}
}
示例4: copyResourcesRecursively
import sun.net.www.protocol.file.FileURLConnection; //导入依赖的package包/类
public static void copyResourcesRecursively(URL originUrl, File destination) throws Exception {
URLConnection urlConnection = originUrl.openConnection();
if (urlConnection instanceof JarURLConnection) {
copyJarResourcesRecursively(destination, (JarURLConnection) urlConnection);
} else if (urlConnection instanceof FileURLConnection) {
FileUtils.copyDirectory(new File(originUrl.getPath()), destination);
} else {
die("Unsupported URL type: " + urlConnection);
}
}
示例5: copyDirectory
import sun.net.www.protocol.file.FileURLConnection; //导入依赖的package包/类
public static void copyDirectory(URL originUrl, File destination) throws IOException {
URLConnection urlConnection = originUrl.openConnection();
if (urlConnection instanceof JarURLConnection) {
copyJarDirectory(destination, (JarURLConnection) urlConnection);
} else if (urlConnection instanceof FileURLConnection) {
FileUtils.copyDirectory(new File(originUrl.getPath()), destination);
} else {
throw new IOException("URLConnection[" + urlConnection.getClass().getSimpleName() +
"] is not a recognized/implemented connection type.");
}
}
示例6: getUrlScanner
import sun.net.www.protocol.file.FileURLConnection; //导入依赖的package包/类
/**
* Obtains an appropriate scanning algorithm given the type of the passed in URL.
*
* @param url The URL for which a scanner is needed
* @return an appropriate {@link ScanAlgorithm} instance for this URL type
* @throws ClassNotFoundException if there is no known implementation of {@link ScanAlgorithm} for this URL type
* @throws IOException if it was not possible to open a connection to the URL or decode its path
*/
public ScanAlgorithm getUrlScanner(URL url) throws ClassNotFoundException, IOException {
URLConnection connection = url.openConnection();
if (connection instanceof JarURLConnection)
return new ArchiveScanAlgorithm(classLoader, ((JarURLConnection) connection).getJarFile());
else if (connection instanceof FileURLConnection)
return new DirectoryScanAlgorithm(classLoader, new File(URLDecoder.decode(url.getPath(), "UTF-8")));
else if (connection instanceof LevelAssetsURLConnection)
return new LevelAssetsScanAlgorithm(classLoader, ((LevelAssetsURLConnection) connection).getContainer());
else
throw new ClassNotFoundException("Cannot get archive scanner for URL \"" + url.getPath() + "\"");
}
示例7: find
import sun.net.www.protocol.file.FileURLConnection; //导入依赖的package包/类
public static Macro find(String name) throws WarpScriptException {
Macro macro = (Macro) macros.get(name);
//
// The macro is not (yet) known, we will attempt to load it from the
// classpath
//
if (null == macro) {
String rsc = name + WarpScriptMacroRepository.WARPSCRIPT_FILE_EXTENSION;
URL url = WarpScriptMacroLibrary.class.getClassLoader().getResource(rsc);
if (null != url) {
try {
URLConnection conn = url.openConnection();
if (conn instanceof JarURLConnection) {
//
// This case is when the requested macro is in a jar
//
final JarURLConnection connection = (JarURLConnection) url.openConnection();
final URL fileurl = connection.getJarFileURL();
File f = new File(fileurl.toURI());
addJar(f.getAbsolutePath(), rsc);
macro = (Macro) macros.get(name);
} else if (conn instanceof FileURLConnection) {
//
// This case is when the requested macro is in the classpath but not in a jar.
// In this case we do not cache the parsed macro, allowing for dynamic modification.
//
String urlstr = url.toString();
File root = new File(urlstr.substring(0, urlstr.length() - name.length() - WarpScriptMacroRepository.WARPSCRIPT_FILE_EXTENSION.length()));
macro = loadMacro(root, conn.getInputStream());
}
} catch (URISyntaxException use) {
throw new WarpScriptException("Error while loading '" + name + "'", use);
} catch (IOException ioe) {
throw new WarpScriptException("Error while loading '" + name + "'", ioe);
}
}
}
return macro;
}