當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpURLConnection.setIfModifiedSince方法代碼示例

本文整理匯總了Java中java.net.HttpURLConnection.setIfModifiedSince方法的典型用法代碼示例。如果您正苦於以下問題:Java HttpURLConnection.setIfModifiedSince方法的具體用法?Java HttpURLConnection.setIfModifiedSince怎麽用?Java HttpURLConnection.setIfModifiedSince使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.HttpURLConnection的用法示例。


在下文中一共展示了HttpURLConnection.setIfModifiedSince方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: download

import java.net.HttpURLConnection; //導入方法依賴的package包/類
/**
 * Check if there is a new version of the script engine
 * 
 * @throws IOException
 *             if any error occur
 */
private void download() throws IOException {
    boolean is32 = "32".equals( System.getProperty( "sun.arch.data.model" ) );
    String fileName;
    final String os = System.getProperty( "os.name", "" ).toLowerCase();
    if( os.contains( "windows" ) ) {
        fileName = is32 ? "win32" : "win64";
    } else if( os.contains( "mac" ) ) {
        fileName = is32 ? "mac" : "mac64";
    } else if( os.contains( "linux" ) ) {
        fileName = is32 ? "linux-i686" : "linux-x86_64";
    } else {
        throw new IllegalStateException( "Unknown OS: " + os );
    }
    File target = new File( System.getProperty( "java.io.tmpdir" ) + "/SpiderMonkey" );
    URL url = new URL( "https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/jsshell-" + fileName
                    + ".zip" );
    System.out.println( "\tDownload: " + url );
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    if( target.exists() ) {
        System.out.println( "\tUP-TP-DATE" );
        conn.setIfModifiedSince( target.lastModified() );
    }
    InputStream input = conn.getInputStream();
    command = target.getAbsolutePath() + "/js";
    if( conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED ) {
        return;
    }
    ZipInputStream zip = new ZipInputStream( input );
    long lastModfied = conn.getLastModified();
    do {
        ZipEntry entry = zip.getNextEntry();
        if( entry == null ) {
            break;
        }
        if( entry.isDirectory() ) {
            continue;
        }
        File file = new File( target, entry.getName() );
        file.getParentFile().mkdirs();

        Files.copy( zip, file.toPath(), StandardCopyOption.REPLACE_EXISTING );
        file.setLastModified( entry.getTime() );
        if( "js".equals( file.getName() ) ) {
            file.setExecutable( true );
        }
    } while( true );
    target.setLastModified( lastModfied );
}
 
開發者ID:i-net-software,項目名稱:JWebAssembly,代碼行數:55,代碼來源:SpiderMonkey.java

示例2: updateManifestCacheFile

import java.net.HttpURLConnection; //導入方法依賴的package包/類
private boolean updateManifestCacheFile(String name, String localCache, String url, ServletContext context)
	throws IOException
{
	File downloadtmp = File.createTempFile(name, ".part");
	File tmplocation = new File(_filesCacheDir);
	if(!tmplocation.exists())
	{	tmplocation.mkdir(); }
	File cacheFile = new File(tmplocation, localCache);
	long lastUpdatedCache = -1;
	if(cacheFile.exists())
	{
		lastUpdatedCache = cacheFile.lastModified();
		long twentyFourHoursAgo = System.currentTimeMillis() - (24L * 36000L);
		if(lastUpdatedCache > twentyFourHoursAgo)
		{
			context.log("Skipping updating " + name + " catalog");
			return false;
		}
	}
	URL cacheURL = new URL(url);
	HttpURLConnection cacheConnection = (HttpURLConnection) cacheURL.openConnection();
	if(lastUpdatedCache > 0L)
	{	cacheConnection.setIfModifiedSince(lastUpdatedCache); }
	cacheConnection.connect();
	switch(cacheConnection.getResponseCode())
	{
		case HttpURLConnection.HTTP_OK:
			InputStream is = cacheConnection.getInputStream();
			if("application/octet-stream".equals(cacheConnection.getContentType()))
			{
				ZipInputStream zis = new ZipInputStream(is);
				zis.getNextEntry();
				is = zis;
			}
			FileOutputStream os = new FileOutputStream(cacheFile, false);
			byte[] buff = new byte[1024];
			for(int br = is.read(buff); br > 0; br = is.read(buff))
			{	os.write(buff, 0, br); }
			os.close();
			is.close();
			context.log("manifest " + name + " updated");
			break;
		case HttpURLConnection.HTTP_NOT_MODIFIED:
			context.log("manifest " + name + " not modified");
			break;
		default:
			context.log("error updating " + name + ", got return code " + cacheConnection.getResponseCode() +" with message " + cacheConnection.getResponseMessage());
	}
	cacheConnection.disconnect();
	return true;
}
 
開發者ID:zueski,項目名稱:playswith,代碼行數:52,代碼來源:ManifestServlet.java


注:本文中的java.net.HttpURLConnection.setIfModifiedSince方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。