gpt4 book ai didi

java - Android:java.io.FileNotFoundException即使可以在浏览器上查看文件

转载 作者:行者123 更新时间:2023-12-01 14:16:53 30 4
gpt4 key购买 nike

我正在尝试在我的应用程序中下载图像文件,但即使我可以在浏览器上查看图像,我仍然收到“java.io.FileNotFoundException”。这是假设下载文件的代码。您可以在浏览器上测试链接

new Thread(new Runnable() {

@Override
public void run() {

String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
String sample = "https://yande.re/image/617a7bff242f2ec56ccfc84df53604fc/yande.re%20263544%20chibi%20shinomiya_himawari%20vividred_operation.jpg";
try
{
URL url = new URL(sample);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.connect();


File SDCardRoot = new File(Environment.getExternalStorageDirectory(),"Yandere/Wallpapers");
SDCardRoot.mkdirs();
String filename="downloadedFile.jpg";
Log.i("Local filename:",""+filename);
File file = new File(SDCardRoot,filename);
if(file.createNewFile())
{
file.createNewFile();
}
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 )
{
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
}
fileOutput.close();
if(downloadedSize==totalSize) filepath=file.getPath();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
filepath=null;
e.printStackTrace();
}}
}).start();

错误:

 08-05 16:36:28.032: W/System.err(17064): java.io.FileNotFoundException: https://yande.re/image/617a7bff242f2ec56ccfc84df53604fc/yande.re%20263544%20chibi%20shinomiya_himawari%20vividred_operation.jpg
08-05 16:36:28.032: W/System.err(17064): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:186)
08-05 16:36:28.032: W/System.err(17064): at libcore.net.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:271)
08-05 16:36:28.032: W/System.err(17064): at angel.util.Downloader$1.run(Downloader.java:52)
08-05 16:36:28.032: W/System.err(17064): at java.lang.Thread.run(Thread.java:856)

最佳答案

关于java - Android:java.io.FileNotFoundException即使可以在浏览器上查看文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18053341/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com