如何使用Scala从互联网下载和保存文件?
发布时间:2020-12-16 19:01:31 所属栏目:安全 来源:网络整理
导读:基本上我有一个网址/链接到一个文本文件在线,我正在尝试在本地下载.由于某些原因,创建/下载的文本文件为空.打开任何建议.谢谢! def downloadFile(token: String,fileToDownload: String) { val url = new URL("http://randomwebsite.com/docs?t=" + token +
基本上我有一个网址/链接到一个文本文件在线,我正在尝试在本地下载.由于某些原因,创建/下载的文本文件为空.打开任何建议.谢谢!
def downloadFile(token: String,fileToDownload: String) { val url = new URL("http://randomwebsite.com/docs?t=" + token + "&p=tsr%2F" + fileToDownload) val connection = url.openConnection().asInstanceOf[HttpURLConnection] connection.setRequestMethod("GET") val in: InputStream = connection.getInputStream val fileToDownloadAs = new java.io.File("src/test/resources/testingUpload1.txt") val out: OutputStream = new BufferedOutputStream(new FileOutputStream(fileToDownloadAs)) val byteArray = Stream.continually(in.read).takeWhile(-1 !=).map(_.toByte).toArray out.write(byteArray) } 解决方法
冲洗缓冲区,然后关闭输出流.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容