Уппс, действительно "вырезал" лишнее, sorry! Вот так работает:
| Код | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;
public class DownLoadPicture { public static final String OUT_FOLDER = "C:\\Downloads\\";
public static void main (String[] args) { doIt(); } public static void doIt() { BufferedOutputStream out = null; // The output stream (for storing the contents of the file) BufferedInputStream in = null; // The input stream (for reading the contents of the file URL url; HttpURLConnection con; int block = 4096; // размер буфера byte[] buf = new byte[block]; // буфер ввода/вывода int count = 0; // The number of bytes read in one iteration boolean notEmpty; notEmpty = false; try { url = new URL("http://pic.autoscout24.de/images/9/2/4/46838429_2.jpg"); // Open a connection con = (HttpURLConnection)url.openConnection(); // Cast для HTTP протокола, для других должен быть другой count = 0; // The number of bytes read in one iteration in = new BufferedInputStream(con.getInputStream()); // Получение входного потока while((count = in.read(buf, 0, block)) != -1) { if (!notEmpty) { notEmpty = true; out = new BufferedOutputStream(new FileOutputStream(OUT_FOLDER+"123.jpg")); // Output file } out.write(buf, 0, count); // Вот здесь } } catch (FileNotFoundException e) { } catch (MalformedURLException e) { } catch (IOException e) { } finally { if (out != null) { try { out.close(); } catch (IOException e) { }
} if (in != null) { try { in.close(); } catch (IOException e) { } } } } }
| |