以下のようなコードでzipファイルを内部ストレージにダウンロードしたいのですがtest.zipファイルの中に文字列が入ってしまいます。
zipファイルはphpで生成されていて
URL url = new URL("https://example.co.jp/download"); // テンポラリファイルの設定 // HTTP接続開始 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("accept-encoding", "gzip"); connection.setRequestMethod("GET"); connection.connect(); int response = connection.getResponseCode(); if( response != HttpURLConnection.HTTP_OK){ //取得コードエラー処理 Log.v("取得コードエラー処理","NG"); return null; } //入力ストリーム(from HTTP) int contentsLength = connection.getContentLength(); InputStream is = connection.getInputStream(); DataInputStream dis = new DataInputStream(is); FileOutputStream fo = mContext.openFileOutput("test.zip", Activity.MODE_PRIVATE); DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(fo)); //データ読み byte[] b = new byte[1024]; int readbyte = 0, totalbyte = 0; while( ( readbyte = dis.read(b)) != -1 ){ dos.write(b,0,readbyte); totalbyte += readbyte; } //ストリームクローズ dis.close(); dos.close();
回答1件
あなたの回答
tips
プレビュー