ダンロードするクラスをつくりました。
java
1 2import java.io.BufferedOutputStream; 3import java.io.DataInputStream; 4import java.io.DataOutputStream; 5import java.io.FileNotFoundException; 6import java.io.FileOutputStream; 7import java.io.IOException; 8import java.net.HttpURLConnection; 9import java.net.MalformedURLException; 10import java.net.ProtocolException; 11import java.net.URL; 12 13 14public class DownloadAndSave 15{ 16 private String szURL; 17 private String szSaveFilename; 18 19 public DownloadAndSave() { } 20 public void go() 21 { 22 try { 23 URL url = new URL( szURL ); 24 25 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); 26 conn.setAllowUserInteraction(false); 27 conn.setInstanceFollowRedirects(true); 28 conn.setRequestMethod("GET"); 29 conn.connect(); 30 31 int httpStatusCode = conn.getResponseCode(); 32 33 switch(httpStatusCode) 34 { 35 case HttpURLConnection.HTTP_ACCEPTED: 36 System.out.println("ACCEPTED"); break; 37 case HttpURLConnection.HTTP_BAD_GATEWAY: 38 System.out.println("HTTP_BAD_GATEWAY"); break; 39 case HttpURLConnection.HTTP_BAD_METHOD: 40 System.out.println("HTTP_BAD_METHOD"); break; 41 case HttpURLConnection.HTTP_BAD_REQUEST: 42 System.out.println("HTTP_BAD_REQUEST"); break; 43 case HttpURLConnection.HTTP_CLIENT_TIMEOUT: 44 System.out.println("HTTP_CLIENT_TIMEOUT"); break; 45 case HttpURLConnection.HTTP_CONFLICT: 46 System.out.println("HTTP_CONFLICT"); break; 47 case HttpURLConnection.HTTP_CREATED: 48 System.out.println("HTTP_CREATED"); break; 49 case HttpURLConnection.HTTP_ENTITY_TOO_LARGE: 50 System.out.println("HTTP_ENTITY_TOO_LARGE"); break; 51 case HttpURLConnection.HTTP_FORBIDDEN: 52 System.out.println("HTTP_FORBIDDEN"); break; 53 case HttpURLConnection.HTTP_GATEWAY_TIMEOUT: 54 System.out.println("HTTP_GATEWAY_TIMEOUT"); break; 55 case HttpURLConnection.HTTP_GONE: 56 System.out.println("HTTP_GONE"); break; 57 case HttpURLConnection.HTTP_INTERNAL_ERROR: 58 System.out.println("HTTP_INTERNAL_ERROR"); break; 59 case HttpURLConnection.HTTP_LENGTH_REQUIRED: 60 System.out.println("HTTP_LENGTH_REQUIRED"); break; 61 case HttpURLConnection.HTTP_MOVED_PERM: 62 System.out.println("HTTP_MOVED_PERM"); break; 63 case HttpURLConnection.HTTP_MOVED_TEMP: 64 System.out.println("HTTP_MOVED_TEMP"); break; 65 case HttpURLConnection.HTTP_MULT_CHOICE: 66 System.out.println("HTTP_MULT_CHOICE"); break; 67 case HttpURLConnection.HTTP_NO_CONTENT: 68 System.out.println("HTTP_NO_CONTENT"); break; 69 case HttpURLConnection.HTTP_NOT_ACCEPTABLE: 70 System.out.println("HTTP_NOT_ACCEPTABLE"); break; 71 case HttpURLConnection.HTTP_NOT_AUTHORITATIVE: 72 System.out.println("HTTP_NOT_AUTHORITATIVE"); break; 73 case HttpURLConnection.HTTP_NOT_FOUND: 74 System.out.println("HTTP_NOT_FOUND"); break; 75 case HttpURLConnection.HTTP_NOT_IMPLEMENTED: 76 System.out.println("HTTP_NOT_IMPLEMENTED"); break; 77 case HttpURLConnection.HTTP_OK: 78 System.out.println("HTTP_OK"); break; 79 case HttpURLConnection.HTTP_PARTIAL: 80 System.out.println("HTTP_PARTIAL"); break; 81 case HttpURLConnection.HTTP_PAYMENT_REQUIRED: 82 System.out.println("HTTP_PAYMENT_REQUIRED"); break; 83 case HttpURLConnection.HTTP_PRECON_FAILED: 84 System.out.println("HTTP_PRECON_FAILED"); break; 85 case HttpURLConnection.HTTP_PROXY_AUTH: 86 System.out.println("HTTP_PROXY_AUTH"); break; 87 case HttpURLConnection.HTTP_REQ_TOO_LONG: 88 System.out.println("HTTP_REQ_TOO_LONG"); break; 89 case HttpURLConnection.HTTP_RESET: 90 System.out.println("HTTP_RESET"); break; 91 case HttpURLConnection.HTTP_SEE_OTHER: 92 System.out.println("HTTP_SEE_OTHER"); break; 93 case HttpURLConnection.HTTP_UNAUTHORIZED: 94 System.out.println("HTTP_UNAUTHORIZED"); break; 95 case HttpURLConnection.HTTP_UNAVAILABLE: 96 System.out.println("HTTP_UNAVAILABLE"); break; 97 case HttpURLConnection.HTTP_UNSUPPORTED_TYPE: 98 System.out.println("HTTP_UNSUPPORTED_TYPE"); break; 99 case HttpURLConnection.HTTP_USE_PROXY: 100 System.out.println("HTTP_USE_PROXY"); break; 101 case HttpURLConnection.HTTP_VERSION: 102 System.out.println("HTTP_VERSION");break; 103 104 } 105 if( httpStatusCode != HttpURLConnection.HTTP_OK ) { 106 System.out.println("[INFO] HTTP_OK error" ); 107 throw new Exception(); 108 } 109 110 // InputStream 111 DataInputStream dataInStream = new DataInputStream( conn.getInputStream() ); 112 // OutputStream 113 DataOutputStream dataOutStream = new DataOutputStream( new BufferedOutputStream( 114 new FileOutputStream( szSaveFilename ) ) ); 115 116 // Read Data 117 byte[] b = new byte[4096]; 118 int readByte = 0; 119 while( -1 != (readByte = dataInStream.read(b))) { 120 dataOutStream.write(b,0,readByte); 121 } 122 System.out.println(" DownloadAndSave - END -" ); 123 // Close Stream 124 dataInStream.close(); 125 dataOutStream.close(); 126 conn.disconnect(); 127 } catch (FileNotFoundException e) { 128 e.printStackTrace(); 129 } catch (ProtocolException e) { 130 e.printStackTrace(); 131 } catch (MalformedURLException e) { 132 e.printStackTrace(); 133 } catch (IOException e) { 134 e.printStackTrace(); 135 } catch (Exception e) { 136 e.printStackTrace(); 137 } 138 } 139 public void setInfo( String szURL, String szSaveFilename ) 140 { 141 this.szURL = szURL; 142 this.szSaveFilename = szSaveFilename; 143 } 144} 145
java
1 2public class セイバーたんの画像をダウンロードする { 3 4 public static void Step1() 5 { 6 } 7 8 public static void Step2() 9 { 10 } 11 public static void main(String[] args) { 12 // TODO 自動生成されたメソッド・スタブ 13 DownloadAndSave node = new DownloadAndSave(); 14 node.setInfo( "http://google.co.jp", "google.txt" ); 15 node.go(); 16 17 node.setInfo( "http://www.yahoo.co.jp", "yahoo.co.jp" ); 18 node.go(); 19 } 20 21} 22 23 24実行結果
HTTP_OK
DownloadAndSave - END -
HTTP_MOVED_PERM
[INFO] HTTP_OK error
java.lang.Exception
at DownloadAndSave.go(DownloadAndSave.java:107)
at セイバーたんの画像をダウンロードする.main(セイバーたんの画像をダウンロードする.java:18)
main での
DownloadAndSave node = new DownloadAndSave();
node.setInfo( "http://google.co.jp", "google.txt" );
node.go();
node.setInfo( "http://www.yahoo.co.jp", "yahoo.co.jp" ); node.go();
で2回目の node.go(); で必ず失敗します。
エラーの対処方法がわかりません。どうしたらいいでしょうか?
回答2件
あなたの回答
tips
プレビュー