72

インターネットからアプリのバイナリ ファイル (ビデオ) をダウンロードする際に問題が発生しています。Quicktime では、直接ダウンロードすると問題なく動作しますが、私のアプリではどういうわけかめちゃくちゃになります (テキスト エディターではまったく同じように見えますが)。以下に例を示します。

    URL u = new URL("http://www.path.to/a.mp4?video");
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();
    FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));


    InputStream in = c.getInputStream();

    byte[] buffer = new byte[1024];
    int len1 = 0;
    while ( (len1 = in.read(buffer)) > 0 ) {
         f.write(buffer);
    }
    f.close();
4

6 に答える 6

93

それが唯一の問題かどうかはわかりませんが、そこには古典的な Java の不具合があります。したがって、読み取りは 1024 バイト未満になる可能性がありますが、書き込みは常に正確に 1024 バイトを書き出します。これには、前のループ反復からのバイトが含まれる可能性があります。

次のように修正します。

 while ( (len1 = in.read(buffer)) > 0 ) {
         f.write(buffer,0, len1);
 }

おそらく、Android での 3G のネットワークの遅延が大きいか、パケット サイズが小さいことが、影響を悪化させているのでしょうか?

于 2009-02-23T04:45:10.313 に答える
29
new DefaultHttpClient().execute(new HttpGet("http://www.path.to/a.mp4?video"))
        .getEntity().writeTo(
                new FileOutputStream(new File(root,"Video.mp4")));
于 2011-05-12T15:04:30.133 に答える
16

1 つの問題は、バッファーの読み取りです。入力ストリームのすべての読み取りが正確に 1024 の倍数でない場合、不正なデータがコピーされます。使用する:

byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) != -1 ) {
  f.write(buffer,0, len1);
}
于 2009-02-23T04:43:42.830 に答える
14
 public class download extends Activity {

     private static String fileName = "file.3gp";
     private static final String MY_URL = "Your download url goes here";

     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        try {
            URL url = new URL(MY_URL);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            String PATH = Environment.getExternalStorageDirectory()
                + "/download/";
            Log.d("Abhan", "PATH: " + PATH);
            File file = new File(PATH);
            if(!file.exists()) {
               file.mkdirs();
            }
            File outputFile = new File(file, fileName);
            FileOutputStream fos = new FileOutputStream(outputFile);
            InputStream is = c.getInputStream();
            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.flush();
            fos.close();
            is.close();
        } catch (IOException e) {
            Log.e("Abhan", "Error: " + e);
        }
        Log.i("Abhan", "Check Your File.");
    } 
}
于 2010-12-10T06:13:42.483 に答える
3

このスレッドに関する以前のフィードバックに基づいてコードを修正しました。Eclipse と複数の大きなファイルを使用してテストしました。正常に動作しています。これをコピーして環境に貼り付け、http パスとファイルをダウンロードする場所を変更するだけです。

try {
    //this is the file you want to download from the remote server
    String path ="http://localhost:8080/somefile.zip";
    //this is the name of the local file you will create
    String targetFileName
        boolean eof = false;
    URL u = new URL(path);
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();
    FileOutputStream f = new FileOutputStream(new File("c:\\junk\\"+targetFileName));
        InputStream in = c.getInputStream();
        byte[] buffer = new byte[1024];
        int len1 = 0;
        while ( (len1 = in.read(buffer)) > 0 ) {
        f.write(buffer,0, len1);
                 }
    f.close();
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

幸運を祈ります

于 2011-04-20T17:59:31.573 に答える
2

Apache のコピー メソッド ( Apache Commons IO ) を使用するだけです。Java を使用する利点です。

IOUtils.copy(is, os);

最終ブロックでストリームを閉じることを忘れないでください:

try{
      ...
} finally {
  IOUtils.closeQuietly(is);
  IOUtils.closeQuietly(os);
}
于 2011-04-20T10:41:03.010 に答える