1

在尝试使用 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());
            }
        }
    });
}

现在,在运行它时,会下载一个文件。文件的前半部分是预期的文件文本。文件的第二部分在开头有一点第二部分文本,然后是第一部分文本。

但是,如果我注释掉下载第一部分的代码,该文件将包含第二部分而不会出现问题。

编辑:(为问题添加更多细节)

我编写的代码应该通过两个单独的连接将文本文件分成两部分下载。第一个连接将下载文件的前半部分并将其写入文件。第二个连接是下载文件的第二部分并将其附加到同一个文件中。

如果我注释掉以下应该是“第一部分下载器”的内容,其余代码将按预期下载并写入文件的第二部分。

            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();

但是整个代码将把第一部分下载两次。

4

0 回答 0