File download support

parent 47a6d0bc
package co.melondev.cubedpay.data;
import com.google.common.base.Joiner;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.Arrays;
public class UploadedImage {
private String id = "";
......@@ -13,6 +23,22 @@ public class UploadedImage {
return url;
}
public void download(File file) throws IOException {
ReadableByteChannel rbc = Channels.newChannel(new URL(url).openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
}
public File downloadToTempFile() throws IOException {
String[] nameSplit = url.substring(url.lastIndexOf("/")).split("\\.");
File file = File.createTempFile(
"cubedpay-" + Joiner.on(".").join(Arrays.copyOfRange(nameSplit, 0, nameSplit.length - 2)),
nameSplit[nameSplit.length - 1]);
download(file);
return file;
}
@Override
public String toString() {
return "UploadedImage{" +
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment