Здравствуйте, пытаюсь скачать файл с сервера который имеет вид
http://***.ru/grupp/зима(ор) - 31/IMG_20140717_225039.jpg
пользовался и этим
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
public String grabImageFromUrl(String url, String path) {
try {
InputStream is = (InputStream) new URL(url).getContent();
System.out.println(path);
System.out.println(url);
File f = new File(path);
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
try {
byte[] b = new byte[100];
int l = 0;
while ((l = is.read(b)) != -1)
fos.write(b, 0, l);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(fos.getChannel().size());
System.out.println(f.getAbsolutePath());
return f.getAbsolutePath();
} catch (Exception e) {
System.out.println("Exc=" + e);
return null;
}
}
и этим
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
private void downloadFile(String url, String path) {
final ProgressDialog progressDialog = new ProgressDialog(this);
new AsyncTask<String, Integer, File>() {
private Exception m_error = null;
@Override
protected void onPreExecute() {
progressDialog.setMessage("Downloading ...");
progressDialog.setCancelable(false);
progressDialog.setMax(100);
progressDialog
.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
}
@Override
protected File doInBackground(String... params) {
URL url;
HttpURLConnection urlConnection;
InputStream inputStream;
int totalSize;
int downloadedSize;
byte[] buffer;
int bufferLength;
File file = new File(params[1]);
FileOutputStream fos = null;
try {
url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
file.createNewFile();
fos = new FileOutputStream(file);
inputStream = urlConnection.getInputStream();
totalSize = urlConnection.getContentLength();
downloadedSize = 0;
Log.d("soska", totalSize+" "+params[0]);
buffer = new byte[1024];
bufferLength = 0;
// читаем со входа и пишем в выход,
// с каждой итерацией публикуем прогресс
while ((bufferLength = inputStream.read(buffer)) > 0) {
fos.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
publishProgress(downloadedSize, totalSize);
}
fos.close();
inputStream.close();
return file;
} catch (MalformedURLException e) {
e.printStackTrace();
m_error = e;
} catch (IOException e) {
e.printStackTrace();
m_error = e;
}
return null;
}
// обновляем progressDialog
protected void onProgressUpdate(Integer... values) {
progressDialog
.setProgress((int) ((values[0] / (float) values[1]) * 100));
};
@Override
protected void onPostExecute(File file) {
// отображаем сообщение, если возникла ошибка
if (m_error != null) {
m_error.printStackTrace();
return;
}
// закрываем прогресс и удаляем временный файл
progressDialog.hide();
//file.delete();
}
}.execute(url,path);
}
результат один и тот же файл не скачивался.
но если выбрать путь без кириллицы, то есть
http://***.ru/grupp/winter(or) - 31/IMG_20140717_225039.jpg
то загрузка идет.
значит проблема при подключение с русскими буквами..
как исправить??
мне нужно обязательно поддержку русского.
я думаю проблема где-то здесь:
1.
2.
3.
4.
5.
url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
Заранее спасибо.