views:

217

answers:

2

Ive 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. Im 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

A: 

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)

GClaramunt
Got that part, if you can give some example code id be gratefull
I don't have the code.Saving into a file should be almost the same code you have but instead of loadXML, you write into an file.To get all the URL images, you select all the image tags and extract the source attribute and call the save method above for each one.
GClaramunt
+1  A: 

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"

Ekkmanz
Perfect! thanks. fortunately im using maven, so im just putting it in a .java file for now.