// 服务器路径
final String realPath = f.getLocation();
// 本地路径
final String path = DownloadUtil.getGroupFilePath(realPath);
java.io.File file = new java.io.File(path);
// 文件本地不存在,先下载到本地,再打开文件
if (!file.exists()) {
String romoteUrl = Constants.API_WWW + Constants.API_WWW_SPLIT + f.getLocation();
AsyncHttpClient client = new AsyncHttpClient();
client.get(romoteUrl, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
// called before request is started
}
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
// called when response HTTP status is "200 OK"
if (statusCode == 200) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
java.io.File localFile = null;
try {
localFile = new java.io.File(path);
fos = new FileOutputStream(localFile);
bos = new BufferedOutputStream(fos);
bos.write(response);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
// 根据后缀名来判断打开什么文件
Intent intent = openFiles(path);
if (IntentUtil.isIntentAvailable(context, intent)) {
startActivity(intent);
} else {
showToast("请安装第三方预览软件,如WPS");
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
}
@Override
public void onRetry(int retryNo) {
// called when request is retried
}
});
http://blog.csdn.net/xinzheng_wang/article/details/38925731