1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
	public static InputStream httpGET(String inURL) throws IOException {
		URLConnection conn = null;
		URL url = new URL(inURL);
		conn = url.openConnection();
		return conn.getInputStream();
	}
public static void writeToFile(InputStream input, File inFile) throws IOException {
		// Throw an exception if there is a problem with the parameters
		if (input == null) {
			throw new IOException("Could not write to file due to null InputStream");
		}
		if (inFile.exists()) {
			input.close();
			throw new IOException("Could not write to file because the following file already existed:"
					+ "\n" + inFile.getCanonicalPath());
		}

		// write the file
		BufferedOutputStream output = null;
		try {
			output = new BufferedOutputStream(new FileOutputStream(inFile));
			byte[] buffer = new byte[1024];
			int numRead;
			long numWritten = 0;
			while ((numRead = input.read(buffer)) != -1) {
				output.write(buffer, 0, numRead);
				numWritten += numRead;
			}
		} finally {
			// close the streams
			if (output != null) {
				output.close();
			}
			input.close();
		}
	}