无法在Android上下载文件而在PC上下载确定(亚马逊)

时间:2014-05-30 09:31:47

标签: android http download

有一个奇怪的问题。包含Url:https://s3.amazonaws.com/myappdata/msg/171401089927.mp3(不再可用)的文件可在PC及其mp3文件上下载。但是当我尝试在Android FOA上进行DL时,我会得到内容类型" application / xml"而不是" audio / mpeg"当下载开始时,我得到了:

05-30 12:13:44.478: E/PlayerService(28023): java.io.FileNotFoundException: https://s3.amazonaws.com/myappdata/msg/171401089927.mp3
05-30 12:13:44.478: E/PlayerService(28023):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
05-30 12:13:44.478: E/PlayerService(28023):     at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:270)

用于DL的代码:

    /**
     * Download the url stream to a temporary location
     */
    public void downloadAudioIncrement(String mediaUrl) throws IOException {
        Log.i(TAG, "downloadAudioIncrement(): mediaUrl: "+mediaUrl+"\ncacheDir: "+cacheDir);

        URL url = null;

        try {
            url = new URL(mediaUrl);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new IOException("Unable to create InputStream for mediaUrl:" + mediaUrl);
        }

        // this file will represent whole downloaded song
        mp3FileDownloaded = new File(cacheDir, mp3FileName);
        if (!mp3FileDownloaded.exists())
            //FileUtils.makeDirsForFile(mp3FileDownloaded);
            try{
                mp3FileDownloaded.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
        }

        if (!mp3FileDownloaded.canWrite())
            throw new IOException("Can't open temporary file for writing");

        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

        urlConnection.setReadTimeout(1000 * 20);
        urlConnection.setConnectTimeout(1000 * 5);

        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);

        int mp3BytesSize = urlConnection.getContentLength();
        // final String
        // contentLengthStr=urlConnection.getHeaderField("content-length");

        String ctype = urlConnection.getContentType();
        if (ctype == null) {
            ctype = "";
        } else {
            ctype = ctype.toLowerCase(Locale.US);
        }
        // See if we can handle this type
        Log.i(TAG, "Content Type: " + ctype);

        if ( ctype.contains("audio/mpeg") || TextUtils.isEmpty(ctype) ) {

            String temp = urlConnection.getHeaderField(BITRATE_HEADER);
            Log.i(TAG, "Bitrate: " + temp);
            // if (temp != null){
            // bitrate = new Integer(temp).intValue();
            // }
        } else {
            Log.e(TAG, UNSUPPORTED_AUDIO_TYPE+": " + ctype);
//          throw new IOException(UNSUPPORTED_AUDIO_TYPE+": " + ctype);
            // Log.e(TAG, "Or we could not connect to audio");
            // stop();
            // return;
        }
        final InputStream stream = new BufferedInputStream(urlConnection.getInputStream(),8192);
...

在最后显示的代码行(实例化InputStream流)时,引发了提到的IOExeption。还有其他mp3文件存在于同一位置,它们正在下载,没有任何问题,但只有上面提到的url失败。
这里有什么问题?
更新
看来这个问题发生在带有AOS 4.0.4的HTC Rezound上。在其他设备上,使用AOS 2.3.5一切正常。

1 个答案:

答案 0 :(得分:1)

似乎就行了

urlConnection.setDoOutput(true);
由于我没有上传任何数据,因此

是问题的根源。自从我发表评论以来,一切正常。这些FileNotFoundException while getting the InputStream object from HttpURLConnectionAndroid HttpUrlConnection getInputStream throws NullPointerException线程也可能有用。