从android url下载docx文件
我想从url下载文件<代码>http://opengov.dev.ifabrika.ru/upload/435/IF_docx-你可以试试,它很管用。下面是我的代码:从android url下载docx文件,android,http,android-asynctask,android-download-manager,Android,Http,Android Asynctask,Android Download Manager,我想从url下载文件http://opengov.dev.ifabrika.ru/upload/435/IF_docx-你可以试试,它很管用。下面是我的代码: new DownloadFileFromURL().execute("http://opengov.dev.ifabrika.ru/upload/435/IF_Заявка_Программист PHP_2013-09-03.docx"); DownloadFileFromUrl是 class DownloadFileFromUR
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();
}
}
}
class DownloadFileFromURL扩展异步任务{
私人对话;
公共静态最终整数进度条类型=0;
/**
*启动后台线程前显示进度条对话框
*/
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(PostInfoActivity.this);
setMessage(“正在下载文件,请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_水平);
pDialog.setCancelable(真);
pDialog.show();
}
/**
*在后台线程中下载文件
*/
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
试一试{
URL=新URL(f_URL[0]);
URLConnection conconnection=url.openConnection();
conconnect.connect();
//这将非常有用,以便您可以显示一个典型的0-100%
//进度条
int lenghtOfFile=conconnect.getContentLength();
//下载该文件
InputStream输入=新的BufferedInputStream(url.openStream(),
8192);
//输出流
OutputStream输出=新文件OutputStream(环境
.getExternalStorageDirectory().toString()
+“/test.docx”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布进度。。。。
//在此之后,将调用onProgressUpdate
出版进度(“+(int)((总计*100)/长度文档));
//将数据写入文件
输出.写入(数据,0,计数);
}
//冲洗输出
output.flush();
//合流
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
/**
*更新进度条
*/
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
*完成后台任务后,关闭“进度”对话框
* *
*/
@凌驾
受保护的void onPostExecute(字符串文件\u url){
//下载文件后关闭对话框
如果(pDialog!=null){
pDialog.disclose();
}
}
}
在此之后,在我的根文件夹中看到了一个新的test.docx文件,但它的大小是26字节,我无法打开它。发生这种情况是因为您的url是Unicode格式的,所以您必须首先对其进行编码,然后尝试下载
URLEncoder.encode(Url, "UTF-8")
它适合我。您可以通过从您的后台调用此方法来尝试使用此方法
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;
}
}
我认为问题在于您只使用了URLConnection类而不是HttpUrlConnection类。请参阅下面的链接
嘿,我已经检查了你在编码后得到的链接…这里的问题是+字符替换了空格/IF拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞。。。您也可以在编码之前这样做,即用%20替换空白…因此最后的链接应该像/IFаааПаааааааааааааааааааааааааааааа.new DownloadFileFromURL().execute(“URLEncoder.encode(“IF拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞拞:是的,问题在于编码url必须与以下类似:opengov.dev.ifabrika.ru/upload/435/IF\u%D0%D0%D1%8F%D0%B2%D0%BA%D0%B0\u0%D0%9F%D1%80%D0%be%D0%B3%D1%80%D0%B0%D0%D0%BC%D0%D0%B8%D1%81%D1%82%20PHP\u 2013-09-03.docx