Hi All,
I'm using the source code provided by oliverp in this thread, http://echo.nextapp.com/site/node/5839. Thanks!
Following is my implementation of the downloading of files to client machine from a url.
SourceLoc will contains the URL, eg : http://urserver.com/file.psd
private void downloadSourceFile(ActionEvent e) {
URL url;
URLConnection uc;
InputStream input;
try {
url = new URL(nw.getSourceLoc());
uc = url.openConnection();
input = uc.getInputStream();
String[] fileArray = nw.getSourceLoc().split("/");
String filename = fileArray[fileArray.length - 1];
input.available();
getApplication()
.enqueueCommand(
new DownloadCommand(new FileDownloadProvider(uc.getContentType(),
filename, input)));
} catch (MalformedURLException err) {
err.printStackTrace();
} catch (IOException err) {
err.printStackTrace();
}
}
public static class FileDownloadProvider implements DownloadProvider {
private static final int DEFAULT_BUFFER_SIZE = 1024 * 1024;
private final String contentType;
private final String filename;
private final InputStream contentStream;
public FileDownloadProvider(String contentType, String filename,
InputStream contentStream) {
this.contentType = contentType;
this.filename = filename;
this.contentStream = contentStream;
}
public String getContentType() {
return contentType;
}
public String getFileName() {
return filename;
}
public long getSize() {
try {
return contentStream.available();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public void writeFile(OutputStream outStream) throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int count = 0;
int n = 0;
while (-1 != (n = contentStream.read(buffer))) {
outStream.write(buffer, 0, n);
count += n;
}
contentStream.close();
}
@Override
public String getContentDisposition() {
// TODO Auto-generated method stub
return "attachment";
}
@Override
public Status getStatus() {
// TODO Auto-generated method stub
return null;
}
}
It works great, but however the size of the download file is limited to 9.8kb no matter how i change the coding and buffers.
The file for downloads can be as big as 2gb, so i will need a better way of implementing this.
May i know is it my implementation is wrong?
Thanks for your time.
Regards,
Chee Han