我想从网址下载文件。 http://opengov.dev.ifabrika.ru/upload/435/IF_Заявка_Программист PHP_2013-09-03.docx
- 你可以尝试一下,它有效。我的代码是下一个:
new DownloadFileFromURL().execute("http://opengov.dev.ifabrika.ru/upload/435/IF_Заявка_Программист PHP_2013-09-03.docx");
DownloadFileFromUrl
class DownloadFileFromURL extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
public static final int progress_bar_type = 0;
/**
* Before starting background thread Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(PostInfoActivity.this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Downloading file in background thread
*/
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100%
// progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream
OutputStream output = new FileOutputStream(Environment
.getExternalStorageDirectory().toString()
+ "/test.docx");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task Dismiss the progress dialog
* *
*/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
if (pDialog != null) {
pDialog.dismiss();
}
}
}
在此之后,在我的根文件夹中看到了一个新的test.docx文件,但它的大小是26字节,我无法打开它。
答案 0 :(得分:1)
发生这种情况是因为您的网址是无线编码形式,因此您必须先对其进行编码,然后尝试下载
URLEncoder.encode(Url, "UTF-8")
它对我有用。
答案 1 :(得分:0)
您可以通过从doInBackground
调用此方法来尝试此方法private void downloader(String urlstr) {
HttpURLConnection c = null;
FileInputStream fis = null;
FileOutputStream fbo = null;
File file = null, file1;
File outputFile = null;
InputStream is = null;
URL url = null;
try {
outputFile = new File(Environment
.getExternalStorageDirectory().toString()
+ "/test.docx");
if(outputFile.exists())
Log.e("File delete",outputFile.delete()+"");
fbo = new FileOutputStream(outputFile, false);
// connect with server where remote file is stored to download it
url = new URL(urlstr);
c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.setConnectTimeout(0);
c.connect();
is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fbo.write(buffer, 0, len1);
Log.e("length", len1+"----");
}
fbo.flush();
} catch (Exception e) {
} finally {
if (c != null)
c.disconnect();
if (fbo != null)
try {
fbo.close();
} catch (IOException e) {
e.printStackTrace();
}
if (is != null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
file = null;
outputFile = null;
}
}
答案 2 :(得分:0)
我认为问题是你只使用URLConnection类而不是HttpUrlConnection类。请参阅以下链接 Download a file with Android, and showing the progress in a ProgressDialog
答案 3 :(得分:0)
嘿我已经检查了你编码后得到的链接......这里的问题是+字符替换了空格http://opengov.dev.ifabrika.ru/upload/435 /IF_Заявка_Программист+ PHP_2013-09-03.docx ..... TO解决此问题用%20替换你的+字符然后尝试....这肯定会有效......你也可以在编码之前这样做,即用%20替换你的空白区...所以最后的链接应该像 http://opengov.dev.ifabrika.ru/upload/435 /IF_Заявка_Программист%20PHP_2013-09-03.docx