I am downloading from a URL.
Here is the code I am using:
Code:
public static void downloadWebData(final String url, final String fileName) throws IOException {
Thread t = new Thread(new Runnable() {
public void run() {
HttpConnection connection = null;
InputStream inputStream = null;
OutputStream o = null;
FileConnection fconn = null;
try {
// Initialize Input
connection = (HttpConnection) Connector.open(url, Connector.READ, true);
inputStream = connection.openInputStream();
// Initialize Output
fconn = (FileConnection)Connector.open(fileName, Connector.READ_WRITE);
if(!fconn.exists()) {
fconn.create();
}
o = fconn.openOutputStream();
byte[] responseData = new byte[10000];
int length = 0;
while (-1 != (length = inputStream.read(responseData))) {
o.write(responseData, 0, length);
}
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: "
+ responseCode);
}
}
catch (IOException e) {
e.printStackTrace();
}
catch (final Exception ex) {
System.out.println("Could not download file!");
}
finally {
try {
inputStream.close();
inputStream = null;
connection.close();
connection = null;
o.close();
fconn.close();
}
catch(Exception e){}
}
}
});
t.start();
}
I tried using
setRequestProperty("Content-Length", "byte 0-1000/1001");
before openInputStream so it would only download a part of the file, but it seems to be ignored.