我可以获得bytes
使用Play WS
但我需要java.io.file
上传于AWS S3
.
WS.url("http://media1.santabanta.com/full1/Indian%20%20Celebrities%28F%29/Yami%20Gautam/yami-gautam-27a.jpg").get().map { response =>
val in = response.getAHCResponse.getResponseBodyAsBytes
Ok("yami")
}
现在我如何得到java.io.File
?
最佳答案
正如@Mikesname建议的那样,尝试将字节写入临时文件,然后上传。使用 Play 框架的 Promise 和异步功能。我相信你可以将这个 java 代码转换为 scala 作为你作业的一部分:
Promise<File> filePromise = WS.url("http://stackoverflow.com/questions/26967252/how-to-get-java-io-file-by-using-play-ws").get().map((r) -> {
FileOutputStream out = null;
File file = File.createTempFile("file", null);
try {
out = new FileOutputStream(file);
InputStream bodyAsStream = r.getBodyAsStream();
byte[] barr = new byte[8 * 1024];
int read = 0;
while ((read = bodyAsStream.read(barr)) != -1) {
out.write(barr, 0, read);
}
} finally {
if (out != null) {
out.close();
}
}
return file;
});
filePromise.map((file) -> {
// Use amazon service to upload.
return file;
});
关于java - 如何使用play.WS获取java.io.file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26967252/