网上找了好多xutils3下载都是异步的,项目需求没办法只能自己研究了
/**
* 同步GET请求
*/
<T> T getSync(RequestParams entity, Class<T> resultType) throws Throwable;
下载文件或者图片就是resultType这个设置成File.class就可以了
方案如下
//fileUrl 下载地址 下载后的路径
public String downLoadFile(final String fileUrl,final String fileSaveFullPath){
String outputFile = fileSaveFullPath + “/” + getFileName(fileUrl);
try {
File bfile = new File(fileSaveFullPath);
if (!bfile.exists() && !bfile.mkdirs())
return “”;
RequestParams requestParams = new RequestParams(fileUrl);
requestParams.setSaveFilePath(outputFile);
File file =x.http().getSync(requestParams,File.class);
return file.getAbsolutePath();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return “”;
}
//获取文件名称
public String getFileName(String path) {
int separatorIndex = path.lastIndexOf("/");
String fileName = (separatorIndex < 0) ? path : path.substring(separatorIndex + 1, path.length());
if (TextUtils.isEmpty(fileName) || fileName.contains("?")) {
LogFile.writeLog(“获取到的文件名为空,重新命名文件。”);
fileName = “abc” + System.currentTimeMillis();
}
return fileName;
}