import scala.io.Source
object HttpGet {
def main(args: Array[String]): Unit = {
println("begin")
for (line <- Source.fromFile("c:/videoid.txt").getLines) {
try {
val videoid = line trim;
val content = get("http://localhost:9080/services?id=" + videoid)
println(content)
writeToFile("c:/video/BLUETUBE-" + videoid + ".xml", content)
} catch {
case ioe: java.io.IOException => // handle this
case ste: java.net.SocketTimeoutException => // handle this
}
}
}
def writeToFile(path: String, content: String): Unit = {
import java.io.{ File, PrintWriter }
val pw = new PrintWriter(path, "UTF-16")
try pw.write(content) finally pw.close()
}
@throws(classOf[java.io.IOException])
@throws(classOf[java.net.SocketTimeoutException])
def get(url: String,
connectTimeout: Int = 5000,
readTimeout: Int = 5000,
requestMethod: String = "GET") =
{
import java.net.{ URL, HttpURLConnection }
val connection = (new URL(url)).openConnection.asInstanceOf[HttpURLConnection]
connection.setConnectTimeout(connectTimeout)
connection.setReadTimeout(readTimeout)
connection.setRequestMethod(requestMethod)
val inputStream = connection.getInputStream
val content = io.Source.fromInputStream(inputStream).mkString
if (inputStream != null) inputStream.close
content
}
}
http://stackoverflow.com/questions/4604237/how-to-write-to-a-file-in-scala
356

被折叠的 条评论
为什么被折叠?



