复制图像文件时IndexOutBoundException

时间:2018-01-31 04:39:24

标签: scala apache-spark

  def saveImages(imageURL : String, destination: String) : String = {
    val file = new URLDataSource(new java.net.URL(imageURL))
    val inputStream = file.getInputStream

    val rootPath : String = "/tmp/"
    val saveFilePath : File = new File(rootPath + destination)
    if(!saveFilePath.exists()) {
      val outputStream: OutputStream = new FileOutputStream(saveFilePath);
      val b: Array[Byte] = new Array[Byte](1024)
      var length: Int = 0
      while ((length = inputStream.read(b)) != -1) {
        outputStream.write(b, 0, length)  //java.lang.IndexOutOfBoundsException is thrown!
      }
      inputStream.close()
      outputStream.close()
    }
    return  saveFilePath.toString;
  }

我使用上面的代码将图片文件中的图片文件复制到/tmp/位置,获取IndexOutOfBoundException。如何避免在这里得到例外?

0 个答案:

没有答案