English 中文(简体)
Scala and html: download an image (*.jpg, etc) to Hard drive
原标题:

I ve got a Scala program that downloads and parses html. I got the links to the image files form the html, Now I need to transfer those images to my hard drive. I m wondering what the best Scala method I should use.

my connection code:

import java.net._
import java.io._
import _root_.java.io.Reader
import org.xml.sax.InputSource

import scala.xml._

def parse(sUrl:String) = {
    var url = new URL(sUrl)
    var connect = url.openConnection

    var sorce:InputSource = new InputSource
    var neo = new TagSoupFactoryAdapter //load sUrl
    var input = connect.getInputStream

    sorce.setByteStream(input)
    xml = neo.loadXML(sorce)
    input.close
}

My blog

最佳回答

Then you may want to take a look at java2s. Although the solution is in plain Java but you can still modify to Scala syntax to "just use it"

问题回答

An alternative option is to use the system commands which is much cleaner

import sys.process._
import java.net.URL
import java.io.File

object Downloader {
  def start(location: String) : Unit = {
    val url = new URL(location)

    var path = url match {
      case UrlyBurd(protocol, host, port, path) => (if (path == "") "/" else path)
    }

    path = path.substring(path.lastIndexOf("/") + 1)

    url #> new File(path) !!
  }
}

object UrlyBurd {
  def unapply(in: java.net.URL) = Some((
    in.getProtocol, 
    in.getHost, 
    in.getPort,
    in.getPath
  ))
}

One way to achieve that is: collect the URLs of the images and ask for them to the server (open a new connection with the image url and store the bytestream in the hard drive)





相关问题
How to upload and download file from a server using C#

I am developing a webpage in that the user can download their Resume for Edit. So I have a link to download the article. I use the following code for download. DataTable dt = user.getUserDetails(2); ...

Detecting PDF Download

I have recently added functionality for generating pdf reports to a web application. However the reports can be quite large and take some time to show the pdf download dialog. I want to show a spinner ...

Writing file to users giving sporadic error in IE

I have a very interesting issue with only specific IE implementations. I have an ASPX page that is used to write files down to the user, as part of the process the page uses the following code to ...

PHP - List of Excel files to download from Intranet Site?

I have a intranet site running PHP 5 that needs to list a folder containing only Excel files. Ideally the user needs to be able to input some search criteria (ie date) and the files would be filtered ...

Determine total size of SVN directory/trunk

Is there a way to count/calculate the total size of a svn directory if you were to checkout a revision? I have limited internet downloads so I need to know how big something is before I go and ...

Scala and html: download an image (*.jpg, etc) to Hard drive

I ve got a Scala program that downloads and parses html. I got the links to the image files form the html, Now I need to transfer those images to my hard drive. I m wondering what the best Scala ...

Downloading file with wxHTTP?

Im really stumped. Im using the wxHTTP class in wxWidgets to try and download two files. The first request succeeds but the second one fails when wxHTTP->GetInputStream is called. Between downloads, ...

热门标签