我们正在将现有的python实用程序编写到Scala中。实用工具从restapi下载压缩数据作为“.gzip”文件。
Python代码:
response=requests.get(url,stream=True,headers=self.header,proxies=config.PROXIES,timeout=config.TIMEOUT)
with open(file_path, 'wb+') as f:
shutil.copyfileobj(response.raw, f)
在scala我是这样写的:
var out: GZIPOutputStream = new GZIPOutputStream(new FileOutputStream(outputFile))
var writer= new PrintWriter(out)
try {
val inputStrem: InputStream = response.getEntity.getContent
val gzipInputStream = new GZIPInputStream(inputStrem)
for(line <- Source.fromInputStream(gzipInputStream).getLines){
writer.write(line+"\n")
}
} catch {
case e: Exception => throw e
}finally {
if (writer != null) writer.close
}
REST POST和GET请求对于这两个实用程序都是相同的。
我没有在scala中得到任何错误,它成功地执行了,但是创建了不同大小的文件。
任何帮助都将不胜感激。
谢谢您!!