使用scala下载图像文件

Sri*_*vas 4 scala

我正在尝试下载Latex公式的图像文件.以下是我正在使用的代码

    var out: OutputStream = null;
    var in: InputStream = null;

    try {
      val url = new URL("http://latex.codecogs.com/png.download?$$I=\frac{dQ}{dt}$$")

      val connection = url.openConnection().asInstanceOf[HttpURLConnection]
      connection.setRequestMethod("GET")
      in = connection.getInputStream
      val localfile = "sample2.png"
      out = new BufferedOutputStream(new FileOutputStream(localfile))
      val byteArray = Stream.continually(in.read).takeWhile(-1 !=).map(_.toByte).toArray

      out.write(byteArray)
    } catch {
      case e: Exception => println(e.printStackTrace()) 
    } finally {
      out.close
      in.close
    }
Run Code Online (Sandbox Code Playgroud)

我可以下载,但它没有下载完整的图像,预期的图像大小约为517字节,但它只下载275字节.它可能出了什么问题.附上不完整和完整的图像.请帮我.我使用相同的代码下载超过1MB大小的文件,它正常工作.

图像不完整

预期的图像

the*_*mel 10

您传递的是一个错误的字符串,它"\f"被解释为一个转义序列,并为您提供一个"换页"字符.

更好:

val url = new URL("http://latex.codecogs.com/png.download?$$I=\\frac{dQ}{dt}$$")
Run Code Online (Sandbox Code Playgroud)

要么

val url = new URL("""http://latex.codecogs.com/png.download?$$I=\frac{dQ}{dt}$$""")
Run Code Online (Sandbox Code Playgroud)