実際に存在するzipのurlを設定するとFileNotFoundExceptionが出ます。
他のzipのurlでは問題なく動いています。
ただ、今回のzipと前回のzipの違いをあげるとすれば、
今回のものは複数のtextファイルがzipにまとまって入っていることです。
どのようにしてurlからzipを取得することができるようになりますでしょうか?
java
1@Override 2 protected Void doInBackground(URL... urls) { 3 final URL url = urls[0]; 4 5 6 // 文字コード 7 Charset charset = Charset.forName("MS932"); 8 File path = this.mainActivity.getExternalFilesDir(null); // ☆☆☆追加☆☆☆ 9 10 11 URLConnection connection = null; 12 13 try { 14 connection = url.openConnection(); 15 } catch (IOException e) { 16 e.printStackTrace(); 17 } 18 19 20 String redirect = connection.getHeaderField("Location"); 21 22 if (redirect != null) { 23 try { 24 connection = new URL(redirect).openConnection(); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 30 31 InputStream a = null; 32 try { 33 a = connection.getInputStream(); //←ここでエラー(java.io.FileNotFoundException: http://www.xxxx.com/member/datazip/Paci/2020/PACI200822.zip) 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 38 //zipのurlから取得する方法 39 ZipInputStream zis = new ZipInputStream(a); 40 41 42 ZipEntry zipentry = null; 43 // zipの中のファイルがあるだけ繰り返す 44 // 展開後のファイルサイズ、ファイル名に注意 45 int j = 0; 46 while (true) { 47 try { 48 if (!((zipentry = zis.getNextEntry()) != null)) break; 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 53 try (FileOutputStream fos = new FileOutputStream(path + "/" + zipentry.getName()); // 出力 https://techbooster.org/android/application/1629/ 54 BufferedOutputStream bos = new BufferedOutputStream(fos); 55 ) { 56 byte[] data = new byte[1024]; // 1KB 調整可 57 int count = 0; 58 int i = 0; 59 while ((count = zis.read(data)) != -1) { 60 bos.write(data, 0, count); 61 //toastMake(j + "-" + i + "(" + zipentry.getName() + "):" + count, 0, -200); 62 63 i++; 64 } 65 j++; 66 i = 0; 67 } catch (FileNotFoundException e) { 68 e.printStackTrace(); 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 } 73 return null; 74 } 75
java
1W/System.err: java.io.FileNotFoundException: http://www.xxxx.com/member/datazip/Paci/2020/PACI200822.zip 2W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:255) 3 at com.example.pdf.AsyncHttpRequest.doInBackground(AsyncHttpRequest.java:63) 4 at com.example.pdf.AsyncHttpRequest.doInBackground(AsyncHttpRequest.java:19) 5 at android.os.AsyncTask$3.call(AsyncTask.java:378) 6 at java.util.concurrent.FutureTask.run(FutureTask.java:266) 7 at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:289) 8 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 9 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 10 at java.lang.Thread.run(Thread.java:919)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。