• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala URLConnection类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Scala中java.net.URLConnection的典型用法代码示例。如果您正苦于以下问题:Scala URLConnection类的具体用法?Scala URLConnection怎么用?Scala URLConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了URLConnection类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: HttpRequestHandlerBase

//设置package包名称以及导入依赖的类
package mesosphere.marathon.plugin.http

import java.net.{ URL, URLConnection }
import com.google.common.io.Resources


abstract class HttpRequestHandlerBase extends HttpRequestHandler {

  protected[this] def serveResource(path: String, response: HttpResponse): Unit = {
    val content = withResource(path) { url =>
      response.body(mediaMime(url), Resources.toByteArray(url))
      response.status(200)
    }
    content.getOrElse(response.status(404))
  }

  protected[this] def withResource[T](path: String)(fn: URL => T): Option[T] = {
    Option(getClass.getResource(path)).map(fn)
  }

  protected[this] def mediaMime(url: URL): String = {
    url.getPath.split("\\.").lastOption.flatMap(wellKnownMimes.get)
      .orElse(Option(URLConnection.guessContentTypeFromName(url.toString)))
      .getOrElse("application/octet-stream")
  }

  protected[this] val wellKnownMimes = Map (
    "css" -> "text/css",
    "js" -> "application/javascript",
    "eot" -> "application/vnd.ms-fontobject",
    "svg" -> "image/svg+xml",
    "ttf" -> "application/font-ttf"
  )
} 
开发者ID:xiaozai512,项目名称:marathon,代码行数:35,代码来源:HttpRequestHandlerBase.scala


示例2: FilesRepository

//设置package包名称以及导入依赖的类
package repository.storage

import java.io.FileInputStream
import java.net.URLConnection

import com.mongodb.casbah.gridfs.{GridFS, GridFSDBFile}
import conf.connection.HashDB
import domain.storage.FileMeta
import org.bson.types.ObjectId


object FilesRepository {
  val gridfs = GridFS(HashDB.getConnection())

  def getFileType(name: String): String = {
    URLConnection.guessContentTypeFromName(name)
  }

  def save(file: FileInputStream,meta:FileMeta):ObjectId = {
    val fileId = gridfs(file) { f =>
      f.filename = meta.fileName
      f.contentType = meta.contentType
    }

    fileId.get.asInstanceOf[ObjectId]
  }

  def getFileById(id: String): Option[GridFSDBFile] = {
    gridfs.findOne(new ObjectId(id))
  }

  def getFilesByName(fileName: String): Option[GridFSDBFile] = {
    gridfs.findOne(fileName)

  }

  def deleteFileById(id: String) = {
    gridfs.remove(new ObjectId(id))
  }

  def deleteFilesByName(fileName: String) = {
    gridfs.remove(fileName)
  }

} 
开发者ID:Thulebona,项目名称:hashpvtapi,代码行数:46,代码来源:FileRepository.scala


示例3: UTF8Control

//设置package包名称以及导入依赖的类
package org.jmotor.i18n.control

import java.io.InputStreamReader
import java.net.{URL, URLConnection}
import java.util.ResourceBundle.Control
import java.util.{Locale, PropertyResourceBundle, ResourceBundle}


class UTF8Control(suffix: String = "properties") extends Control {

  override def newBundle(baseName: String, locale: Locale, format: String, loader: ClassLoader, reload: Boolean): ResourceBundle = {
    // The below is a copy of the default implementation.
    val bundleName = toBundleName(baseName, locale)
    val resourceName = toResourceName(bundleName, suffix)

    val stream = if (reload) {
      for {
        url: URL ? Option(loader.getResource(resourceName))
        connection: URLConnection ? Option(url.openConnection())
      } yield {
        connection.setUseCaches(false)
        connection.getInputStream
      }
    } else {
      Option(loader.getResourceAsStream(resourceName))
    }

    (for {
      _stream ? stream
    } yield {
      new PropertyResourceBundle(new InputStreamReader(_stream, "UTF-8"))
    }).orNull
  }
} 
开发者ID:aiyanbo,项目名称:scala-i18n,代码行数:35,代码来源:UTF8Control.scala


示例4: FileManager

//设置package包名称以及导入依赖的类
package slide

import java.io.{File, FileOutputStream}
import java.net.{URL, URLConnection}
import java.nio.channels.{Channels, ReadableByteChannel}

class FileManager {

    var currentFile: String = ""
    var numberOfDownloads: Int = 0

    def downloadFile(dlsite: String, path: String): Unit = {
        val url: URL = new URL(dlsite)
        val file: File = new File(path)

        if (isConnected(url)) {
            currentFile = path
            onDownloadStart()

            new Thread(new Runnable {
                override def run(): Unit = {
                    try {
                        val rbc: ReadableByteChannel = Channels.newChannel(url.openStream())
                        val fos: FileOutputStream = new FileOutputStream(file)

                        fos.getChannel.transferFrom(rbc, 0, java.lang.Long.MAX_VALUE)
                        fos.close()

                        numberOfDownloads += 1
                        onDownloadFinished()
                    } catch {
                        case e: Exception =>
                            println("Error: Could not download ADB, please run as Administrator")
                    }
                }
            }).start()
        }
    }

    def isConnected(site: URL): Boolean = {
        try {
            // test connection
            val conn: URLConnection = site.openConnection()
            conn.setConnectTimeout(5000)
            conn.getContent

            true
        } catch {
            case e: Exception => false
        }
    }

    def onDownloadStart(): Unit = {}

    def onDownloadFinished(): Unit = {}

    // var onDownloadStart: () => Unit = null
    // var onDownloadFinished: () => Unit = null
} 
开发者ID:murizaky,项目名称:dasdasd,代码行数:60,代码来源:FileManager.scala



注:本文中的java.net.URLConnection类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala ToResponseMarshallable类代码示例发布时间:2022-05-23
下一篇:
Scala TestActors类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap