网址 - 文件未发现异常异常、发现、网址、文件

2023-09-05 03:32:23 作者:麻烦你收下虚伪的承诺ソ

我试图从特定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();