File Downloder
با سلام
در برنامه ای که فایل باینری را از سرور دانلود می کنیم قسمتی که کانکشن رو کانکت (connection.connect) می کنیم خطا می دهد و برنامه وارد catch می شود. در صورتی که آدرس url صحیح است و اینترنتم متصل است
public class FileDownloader {
public static void download(String downloadPath, String filePath) {
URL url = null;
try {
url = new URL(downloadPath);
HttpURLConnection connection = null;
if (url != null) {
connection = (HttpURLConnection) url.openConnection();
}
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
FileOutputStream outputStream = null;
outputStream = new FileOutputStream(filePath, true);
InputStream inputStream = null;
inputStream = connection.getInputStream();
byte[] buffer = new byte[8 * 1024];
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
System.out.println();
}
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
public class FileDownloader {
public static void download(String downloadPath, String filePath) {
URL url = null;
try {
url = new URL(downloadPath);
HttpURLConnection connection = null;
if (url != null) {
connection = (HttpURLConnection) url.openConnection();
}
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
FileOutputStream outputStream = null;
outputStream = new FileOutputStream(filePath, true);
InputStream inputStream = null;
inputStream = connection.getInputStream();
byte[] buffer = new byte[8 * 1024];
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
System.out.println();
}
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
package com.example.FileDownload;
import android.util.Log;
import android.widget.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class FileDownloader {
public static void download(String downloadPath, String filePath) {
URL url = null;
String m;
try {
url = new URL(downloadPath);
HttpURLConnection connection = null;
if (!url.equals(null)) {
connection = (HttpURLConnection) url.openConnection();
}
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
FileOutputStream outputStream = new FileOutputStream(filePath, true);
InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[8 * 1024];
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
System.out.println();
}
outputStream.close();
} catch (IOException e) {
m = e.toString();
}
}
}
دیباگش کردم یوآرال مشکلی نداره. فقط در همین مورد از این کلاس استفاده کردم یعنی یک بار.
رو گوشیمم که امتحان می کنم کرش می کنه
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .