Reputation: 15490
I can get bytes
from using Play WS
but i need java.io.file
to upload in 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")
}
now how i get java.io.File
?
Upvotes: 0
Views: 403
Reputation: 4463
As @Mikesname suggests, try writing bytes to temp file and then upload. Use promises and async features of play framework. I am sure you can convert this java code to scala as part of your homework:
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;
});
Upvotes: 2