简体   繁体   English

是否有一种很好的,安全的,快速的方法将InputStream写入Scala中的文件?

[英]Is there a nice, safe, quick way to write an InputStream to a File in Scala?

具体来说,我将文件上传保存到Lift Web应用程序中的本地文件。

With Java 7 or later you can use Files from the new File I/O : 在Java 7或更高版本,您可以使用Files 从新文件I / O

Files.copy(from, to)

where from and to can be Path s or InputStream s. 其中fromto可以是Path s或InputStream This way, you can even use it to conveniently extract resources from applications packed in a jar. 这样,您甚至可以使用它方便地从jar中打包的应用程序中提取资源。

If it's a text file, and you want to limit yourself to Scala and Java, then using scala.io.Source to do the reading is probably the fastest--it's not built in, but easy to write: 如果它是一个文本文件,并且你想限制自己使用Scala和Java,那么使用scala.io.Source进行读取可能是最快的 - 它不是内置的,但易于编写:

def inputToFile(is: java.io.InputStream, f: java.io.File) {
  val in = scala.io.Source.fromInputStream(is)
  val out = new java.io.PrintWriter(f)
  try { in.getLines().foreach(out.println(_)) }
  finally { out.close }
}

But if you need other libraries anyway, you can make your life even easier by using them (as Michel illustrates). 但是如果你还需要其他图书馆,你可以通过使用它们来让你的生活更轻松(正如米歇尔所说)。

(PS--in Scala 2.7, getLines should not have a () after it.) (PS - 在Scala 2.7中, getLines不应该有一个() 。)

(PPS--in old versions of Scala, getLines did not remove the newline, so you need to print instead of println .) (PPS - 在旧版本的Scala中, getLines没有删除换行符,因此您需要print而不是println 。)

I don't know about any Scala specific API, but since Scala is fully compatible to Java you can use any other library like Apache Commons IO and Apache Commons FileUpload . 我不知道任何Scala特定的API,但由于Scala与Java完全兼容,您可以使用任何其他库,如Apache Commons IOApache Commons FileUpload

Here is some example code (untested): 以下是一些示例代码(未经测试):

//using Commons IO:
val is = ... //input stream you want to write to a file
val os = new FileOutputStream("out.txt")
org.apache.commons.io.IOUtils.copy(is, os)
os.close()

//using Commons FileUpload
import javax.servlet.http.HttpServletRequest
import org.apache.commons.fileupload.{FileItemFactory, FileItem}
import apache.commons.fileupload.disk.DiskFileItemFactory
import org.apache.commons.fileupload.servlet.ServletFileUpload
val request: HttpServletRequest = ... //your HTTP request
val factory: FileItemFactory = new DiskFileItemFactory()
val upload = new ServletFileUpload(factory)
val items = upload.parseRequest(request).asInstanceOf[java.util.List[FileItem]]
for (item <- items) item.write(new File(item.getName))

The inputToFile method given above doesn't work well with binary files like .pdf files. 上面给出的inputToFile方法不适用于像.pdf文件这样的二进制文件。 It throws a runtime exception while attempting to decode the file into string. 尝试将文件解码为字符串时,它会引发运行时异常。 What worked for me was this: 对我有用的是:

def inputStreamToFile(inputStream: java.io.InputStream, file: java.io.File) = {
    val fos = new java.io.FileOutputStream(file)
    fos.write(
      Stream.continually(inputStream.read).takeWhile(-1 !=).map(_.toByte).toArray
    )
    fos.close()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM