【java】URLからファイルをダウンロードする
InputStream in = null; OutputStream out = null; // InputStreamからOutputStreamに出力 try { // URLオブジェクト作成 URL url = new URL("URL"); // URLからInputStreamオブジェクトを取得(入力) in = url.openStream(); // 出力先ファイル OutputStream(出力) out = new FileOutputStream("OUT"); byte[] buf = new byte[1024]; int len = 0; // 終わるまで書き込み while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.flush(); } catch (Exception e) { } finally { // ストリームをクローズする if (out != null) { try { out.close(); } catch (IOException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } }