View Single Post
Old 05-29-2008, 07:14 AM   #3
Massif
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 8330
PIN: N/A
Carrier: Telus
Posts: 26
Default

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.

Last edited by Massif; 05-29-2008 at 08:26 AM..
Offline   Reply With Quote