2 つの部分で HttpURLConnection を使用して単純なテキスト ファイルをダウンロードしようとすると、問題に直面しています。
コード:
public class MainActivity extends AppCompatActivity {
final String TAG = "MainActivity";
int file_size;
String urlstring = "http://www.answersthatwork.com/Download_Area/Fun_Page/the_jack_schitt_story.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView tv = (TextView) findViewById(R.id.tv);
ProgressBar pb1 = (ProgressBar) findViewById(R.id.progressBar);
ProgressBar pb2 = (ProgressBar) findViewById(R.id.progressBar2);
pb1.setMax(file_size/2);
pb2.setMax(file_size - (file_size/2));
try {
URL url1 = new URL(urlstring);
URL url2 = new URL(urlstring);
HttpURLConnection dlc1 = (HttpURLConnection) url1.openConnection();
HttpURLConnection dlc2 = (HttpURLConnection) url2.openConnection();
dlc1.setRequestProperty("Range", "bytes=0-" + file_size/2);
dlc2.setRequestProperty("Range", "bytes=" + ((file_size/2) + 1) + "-" + (file_size));
int downloaded1 = 0, downloaded2 = 0;
File wallpaperDirectory = new File("/sdcard/windows/BstSharedFolder");
wallpaperDirectory.mkdirs();
File filepath = new File(wallpaperDirectory, "file.txt");
dlc1.connect();
BufferedInputStream dlc1in = new BufferedInputStream(dlc1.getInputStream());
FileOutputStream fos1= new FileOutputStream(filepath);
BufferedOutputStream bout1 = new BufferedOutputStream(fos1, 1024);
byte[] data1 = new byte[1024];
int x1 = 0;
while ((x1 = dlc1in.read(data1)) != -1) {
bout1.write(data1, 0, x1);
downloaded1 += x1;
pb1.setProgress(downloaded1);
}
bout1.flush();
bout1.close();
dlc1.disconnect();
dlc2.connect();
BufferedInputStream dlc2in = new BufferedInputStream(dlc2.getInputStream());
FileOutputStream fos2= new FileOutputStream(filepath,true);
BufferedOutputStream bout2 = new BufferedOutputStream(fos2, 1024);
byte[] data2 = new byte[1024];
int x2 = 0;
while ((x2 = dlc2in.read(data2)) != -1) {
bout2.write(data2, 0, x2);
downloaded2 += x2;
pb2.setProgress(downloaded2);
}
bout2.flush();
bout2.close();
dlc2.disconnect();
}
catch (Exception e) {
Log.v(TAG, e.toString());
}
}
});
}
これを実行すると、ファイルがダウンロードされます。ファイルの前半は、予想されるファイル テキストです。ファイルの 2 番目の部分には、最初に 2 番目の部分のテキストが少しあり、その後に再び最初の部分のテキストが続きます。
ただし、最初の部分をダウンロードするコードをコメント アウトすると、ファイルには問題なく 2 番目の部分が含まれます。
編集:(質問に詳細を追加)
私が書いたコードは、テキスト ファイルを 2 つの別々の接続で 2 つの部分に分けてダウンロードすることになっていました。最初の接続では、ファイルの前半がダウンロードされ、ファイルに書き込まれます。2 番目の接続は、ファイルの 2 番目の部分をダウンロードして、同じファイルに追加することです。
「最初の部分のダウンローダ」であるはずの以下をコメントアウトすると、残りのコードはファイルの 2 番目の部分を期待どおりにダウンロードして書き込みます。
dlc1.connect();
BufferedInputStream dlc1in = new BufferedInputStream(dlc1.getInputStream());
FileOutputStream fos1= new FileOutputStream(filepath);
BufferedOutputStream bout1 = new BufferedOutputStream(fos1, 1024);
byte[] data1 = new byte[1024];
int x1 = 0;
while ((x1 = dlc1in.read(data1)) != -1) {
bout1.write(data1, 0, x1);
downloaded1 += x1;
pb1.setProgress(downloaded1);
}
bout1.flush();
bout1.close();
dlc1.disconnect();
ただし、コード全体をそのまま使用すると、代わりに最初の部分が 2 回ダウンロードされます。