lingochamp / FileDownloader

Multitask、MultiThread(MultiConnection)、Breakpoint-resume、High-concurrency、Simple to use、Single/NotSingle-process
Apache License 2.0
11.02k stars 2.2k forks source link

下载资源失败 #1301

Open jijiangrui opened 4 years ago

jijiangrui commented 4 years ago

java.net.SocketException: Connection failed with request[{If-Match=[W/"5dd4e52a-4d6"], Range=[bytes=0-], User-Agent=[FileDownloader/1.7.7]}] response[{null=[HTTP/1.1 412 Precondition Failed], Connection=[keep-alive], Content-Length=[179], Content-Type=[text/html], Date=[Fri, 22 Nov 2019 05:56:13 GMT], Server=[openresty/1.15.8.1], X-Android-Received-Millis=[1574402171112], X-Android-Response-Source=[NETWORK 412], X-Android-Selected-Protocol=[http/1.1], X-Android-Sent-Millis=[1574402171034], X-Daa-Tunnel=[hop_count=3], X-NWS-LOG-UUID=[641290673762327146 4c4df2ad367f7a7295763b9c6cadefec], X-NWS-UUID-VERIFY=[f0bf7a188eff632f5e5aee2cf650b558]}] http-state[412] on task[-1495819088--1], which is changed after verify connection, so please try again. 请问这个是什么原因

rantianhua commented 4 years ago

请问使用的是什么版本?

rantianhua commented 4 years ago

这是在实际下载的时候,服务端返回了 412 ,但是实际上 bytes=0- 是一个合法的请求。很可能还是服务端的问题。方便提供下有问题的链接吗?

zhouxiongjie commented 4 years ago

我也遇到了一样的问题 怎么解决呢

zhouxiongjie commented 4 years ago

@rantianhua http://show-simpledfs.slradio.cn/getfile?id=b2dbcfaad33b97f80f0b841a2cfb6e5f6a9c1eae417d7345cd8b66e5ab990ee3353fcb7ea82823d6e37fee13ae29fe7e&type=image/jpeg&MD5=b9428408abd6e840125cf94a46dadaa6 这个是我有问题的下载链接,通过浏览器可以查看图片,IOS、PC上下载都ok,麻烦帮我查下什么原因

jijiangrui commented 4 years ago

跟换请求库为 okHttp 后可以了

jijiangrui commented 4 years ago

@zhouxiongjie 你可以试试看行不

rantianhua commented 4 years ago

@rantianhua http://show-simpledfs.slradio.cn/getfile?id=b2dbcfaad33b97f80f0b841a2cfb6e5f6a9c1eae417d7345cd8b66e5ab990ee3353fcb7ea82823d6e37fee13ae29fe7e&type=image/jpeg&MD5=b9428408abd6e840125cf94a46dadaa6 这个是我有问题的下载链接,通过浏览器可以查看图片,IOS、PC上下载都ok,麻烦帮我查下什么原因

已通过链接复现改问题,有进一步结果后会及时反馈。

rantianhua commented 4 years ago

@zhouxiongjie 经过检查,是因为 FileDownloader 使用弱 Etag 导致服务器没有正确响应。目前的解决方式可以按照 @jijiangrui 所说使用 okHttp 即可。第二种方式是临时去掉 Etag :

class NoEtagFileDownloadUrlConnection extends FileDownloadUrlConnection {

        public NoEtagFileDownloadUrlConnection(String originUrl, Configuration configuration) throws IOException {
            super(originUrl, configuration);
        }

        public NoEtagFileDownloadUrlConnection(URL url, Configuration configuration) throws IOException {
            super(url, configuration);
        }

        public NoEtagFileDownloadUrlConnection(String originUrl) throws IOException {
            super(originUrl);
        }

        @Override
        public void addHeader(String name, String value) {
            if ("If-Match".equals(name)) {
                return;
            }
            super.addHeader(name, value);
        }
    }

在初始化 FileDownloader 的时候:

  FileDownloader.setupOnApplicationOnCreate(this)
                .connectionCreator(new FileDownloadUrlConnection
                        .Creator(new FileDownloadUrlConnection.Configuration()
                        .connectTimeout(15_000) // set connection timeout.
                        .readTimeout(15_000) // set read timeout.
                ) {
                    @Override
                    public FileDownloadConnection create(String originUrl) throws IOException {
                        return new NoEtagFileDownloadUrlConnection(originUrl);
                    }
                })
                .commit();

这样抛弃 Etag 之后文件是可以正常下载的。后续会继续追查 FileDownloader 为什么使用服务器返回的弱 Etag 会导致错误的响应。当然,在上面的例子中,除了粗暴地去掉 Etag ,还可以检查是否是弱 Etag ,只有弱 Etag 才抛弃。