网址 - 文件未发现异常 [英] URL - File not found exception

查看:149
本文介绍了网址 - 文件未发现异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从特定URL的形象,但它把我的异常。文件未发现异常。如果我尝试从我的浏览器中打开的网址,我可以看到图像。请帮忙。下面是我的code。谢谢你。

 字符串fileURL =htt​​p://sposter.smartag.my/images/KFC_Voucher.jpg;
字符串文件名=caldophilus.jpg;
网址U =新的URL(fileURL);
HttpURLConnection的C =(HttpURLConnection类)u.openConnection();
c.setRequestMethod(GET);
c.setDoOutput(真正的);
c.connect();
文件根= Environment.getExternalStorageDirectory();
FileOutputStream中F =新的FileOutputStream(新文件(根目录,文件名));
InputStream的X = c.getInputStream();
INT大小= x.available();
BYTE B [] =新的字节[尺寸]
x.read(B);
f.write(B);
f.flush();
f.close();
 

解决方案

我尝试这一点,其做工精细。谢谢你。

 网​​址URL =新的URL(fileURL);
URLConnection的体conexion = url.openConnection();
conexion.connect();
INT lenghtOfFile = conexion.getContentLength();
Log.d(ANDRO_ASYNC的Lenght文件:+ lenghtOfFile);
的InputStream输入=新的BufferedInputStream(url.openStream());
的OutputStream输出=新的FileOutputStream(/ SD卡/ caldophilus.jpg);
字节的数据[] =新的字节[1024];
总长= 0;
而((计数= input.read(数据))!=  -  1){
共有+ =计数;
output.write(数据,0,计数);
}
output.flush();
output.close();
input.close();
 

I'm trying to get a image from particular URL but it throw me an exception. File not found exception. If i try to open the url from my browser, i can see the images. Please help. Below is my code. Thanks.

String fileURL = "http://sposter.smartag.my/images/KFC_Voucher.jpg";
String FILENAME = "caldophilus.jpg";
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
File root = Environment.getExternalStorageDirectory();
FileOutputStream f = new FileOutputStream(new File(root, FILENAME));
InputStream x=c.getInputStream();
int size=x.available();
byte b[]= new byte[size];
x.read(b);
f.write(b);
f.flush();
f.close();

解决方案

i try this and its work fine. Thanks.

URL url = new URL(fileURL);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/caldophilus.jpg");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();

这篇关于网址 - 文件未发现异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆