使用 URLConnection 将文件下载到 SD 卡期间获取 InputStream 时出现 FileNotFoundException

发布于 2024-10-27 19:55:21 字数 1088 浏览 4 评论 0原文

我正在开发具有如下工作流程的应用程序:

1.解析主页(使用 HttpURLConnection、connect()、执行操作、disconnect()、使用数据),

2.单击我制作的链接后,解析文章或下载文件基于链接。

第一步工作完美&在文章的情况下也是第二个,但是当我尝试从特定网址下载文件并将其保存到 SD 卡时,我在这一行中得到了 fileNotFoundException:

  InputStream inputStream = urlConnection.getInputStream();

我的执行指南(下载文件)的代码在这里进行了解释 http://goo.gl/GTBOP 并且我根据此操作的需要在清单中设置了权限,并且在任何下载操作之前我检查了 MEDIA_MOUNTED & MEDIA_MOUNTED_READ_ONLY 状态,看起来很合适。

所以..我在 stackOverflow 和其他地方读到了很多关于这个问题的内容,并且尝试了不同的建议,例如添加这些:

urlConnection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
urlConnection.setRequestProperty("Accept","*/*");

System.setProperty("http.keepAlive", "false");

但没有成功。我仍然被 fileNotFoundException 困扰。我试图从网页捕获响应代码,它显示 404..?!

奇怪的是,如果我硬编码到文件的链接以便从任何其他站点下载,它会毫无问题地下载文件。因此,为了清楚起见,我选择了指向 fileurl 变量的单击链接,但在下一步中,我只是说 fileurl="somesite/somefile.ext" 一切都很顺利。.从网络浏览器中,我可以下载任何在我的应用程序中造成麻烦的文件,所以我我很困惑问题出在哪里..

请问有什么建议吗..? 10q

I'm working on application which has workflow like this:

1.parsing home page (using HttpURLConnection, connect(), do stuff, disconnect(), use data),

2.after click on links I made, parsing article or download file based on link.

First step works perfect & 2nd too in case of article, but when I'm trying to download file from specific url and save it to SD card, I got fileNotFoundException in this line:

  InputStream inputStream = urlConnection.getInputStream();

My guide for doing it (downloading file) was code explained here http://goo.gl/GTBOP and I set permissions in manifest as required for this action, plus before any download operation I checked MEDIA_MOUNTED & MEDIA_MOUNTED_READ_ONLY state and it seems like that fits nice.

So..I read a lot about this problem on stackOverflow and other places and I tried different suggestions like adding these:

urlConnection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
urlConnection.setRequestProperty("Accept","*/*");

or

System.setProperty("http.keepAlive", "false");

but with no success. I'm still stucked with fileNotFoundException. I tried to catch response code from webpage and it says 404.. ?!

It's strange that if I hardcode link to file for downloading from any other site it downloads file without any troubles. So to make it clear I pickup clicked link to fileurl variable but in a next step I just say fileurl="somesite/somefile.ext" everything goes smooth.. From web browser I can download any file that make troubles in my application so I'm confused where is the problem..

Any suggestion please..? 10q

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风轻花落早 2024-11-03 19:55:21

好吧.. 经过这么长时间没有答案.. :( 最后我自己找到了解决方案,所以我在这里发帖,因为希望有人能找到它的用途..

在 URL 中我注意到很多 %20 所以在我使用之前我用 URI.encode(fileURL) 对其进行编码
正如 android 开发页面

瞧.. 一切都很好.. :) 感谢大家为社区做出的贡献! ;)干杯

Oki.. after so much time no answer to this.. :( Finally I found solution on my own so I'm posting here cause somebody will hopefully find use of it..

in URL i noticed a lot %20 so before I used it I encode it with URI.encode(fileURL)
as it described on android dev pages

and voila.. everything was ok.. :) Thanks everybody for contributing community! ;) Cheers

深爱成瘾 2024-11-03 19:55:21

404 或未找到错误消息是
HTTP 标准响应代码
表明客户能够
与服务器通信,但
服务器找不到什么
已请求。

所以这意味着唯一的事情 - 你的文件 URL 指向一个不存在的资源。这怎么可能呢?我不知道。使用调试/日志记录来找出破坏 URL 的位置。

The 404 or Not Found error message is
a HTTP standard response code
indicating that the client was able to
communicate with the server, but the
server could not find what was
requested.

So this means the only thing - your file URL was pointing to a non-existent resource. How it can be possible? I don't know. Use debugging/logging to find out where you break the url.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文