Exeception从连接404响应和文件未找到异常获取inputStream?

时间:2017-08-22 05:55:40

标签: java android

我想尝试从android studio中的简历下载文件,我已尝试使用开放流的代码,但没有任何问题,但在此代码中:

public void startStream2(Context context) {
    try {
        URL url = new URL(file.getFileUrl());
        StrictMode.ThreadPolicy ploicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(ploicy);
        URLConnection connection = url.openConnection();
        int downloaded = 0;
        BufferedOutputStream bout;
        f = new File(downloadPath, file.getName());
        if (f.exists()) {
            downloaded = (int) f.length();
            connection.setRequestProperty("Range", "bytes=" + (f.length()) + "-");
        }
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
        connection.setRequestProperty("Accept","*/*");
        connection.connect();

        int response=((HttpURLConnection)connection).getResponseCode();
        Log.e(TAG, "startStream2: "+ response );
        if (response>399 && response<601){
            InputStreamReader sr;
            sr = new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8");
            StringBuilder builder = new StringBuilder();
            for (int bt = 0; (bt = sr.read()) != -1;) {
                builder.append((char)bt);
            }
            sr.close();
            Log.e(TAG, "startStream2: "+builder.toString());
        }
        InputStream inp=connection.getInputStream();
        BufferedInputStream in = new BufferedInputStream(inp);
        stream2 = (downloaded == 0) ? new FileOutputStream(f) : new FileOutputStream(f, true);
        bout = new BufferedOutputStream(stream2, 1024);
        byte[] data = new byte[1024];
        int x = 0;
        while ((x = in.read(data, 0, 1024)) >= 0) {
            bout.write(data, 0, x);
            downloaded += x;
            int percent = ((int) downloaded * 100) / (int) Size;
            //set percent progress
        }
    }catch (Exception e){
        Log.e(TAG, "startStream2: ",e );
    }
}

错误日志:

  

startStream2:405 startStream2:                                                                            405不允许                                                                                                                                                        

405不允许

                                                                            nginx的                                                                                                                                                        

     

startStream2:                                                                                java.io.FileNotFoundException:                                                                                    在   com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)

我之前测试了网址,从第一次或简历下载时没有任何问题。 我该如何解决? 感谢。

3 个答案:

答案 0 :(得分:0)

删除这些行

connection.setDoInput(true);
connection.setDoOutput(true);

答案 1 :(得分:0)

connection.setDoOutput(true);

问题出在这里。它将HTTP动词从GET更改为POST。你不想发帖。你没有发送任何输出。删除它。

如果响应代码不是200,则不应该获取输入流,否则会引发异常。如果您想要更多信息,可以获取错误流,但此时不是输入流。

您也可以删除

connection.connect();

connection.setDoInput(true);

他们不做任何尚未发生的事情。

答案 2 :(得分:-1)

最后我通过这段代码解决了它:

public void startStream2(Context context) {
try {
    URL url = new URL(file.getFileUrl());
    StrictMode.ThreadPolicy ploicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(ploicy);
    URLConnection connection = url.openConnection();
    int downloaded = 0;
    BufferedOutputStream bout;
    f = new File(downloadPath, file.getName());
    if (f.exists()) {
        downloaded = (int) f.length();
        connection.setRequestProperty("Range", "bytes=" + (f.length()) + "-");
    }
    connection.setDoInput(true);
    connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
    connection.setRequestProperty("Accept","*/*");

    int response=((HttpURLConnection)connection).getResponseCode();
    Log.e(TAG, "startStream2: "+ response );
    if (response>399 && response<601){
        InputStreamReader sr;
        sr = new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8");
        StringBuilder builder = new StringBuilder();
        for (int bt = 0; (bt = sr.read()) != -1;) {
            builder.append((char)bt);
        }
        sr.close();
        Log.e(TAG, "startStream2: "+builder.toString());
    }
    InputStream inp=connection.getInputStream();
    BufferedInputStream in = new BufferedInputStream(inp);
    stream2 = (downloaded == 0) ? new FileOutputStream(f) : new FileOutputStream(f, true);
    bout = new BufferedOutputStream(stream2, 1024);
    byte[] data = new byte[1024];
    int x = 0;
    while ((x = in.read(data, 0, 1024)) >= 0) {
        bout.write(data, 0, x);
        downloaded += x;
        int percent = ((int) downloaded * 100) / (int) Size;
        //set percent progress
    }
}catch (Exception e){
    Log.e(TAG, "startStream2: ",e );
}
}

非常感谢!