我正在尝试将文件从 Java Spring 后端下载到我的 Xamarin Forms 应用程序。我正在使用本机 Android 管理器。当我开始下载时,我在 Files 中看到了新文件,很快就开始下载,最终达到 100%。之后,文件立即进入排队状态。一段时间后,它说下载不成功。当我读取状态时,原因码是 1008,这意味着 ERROR_CANNOT_RESUME。但是,如果下载达到 100%,还有什么要恢复的呢?这只发生在 Android 9 上,我尝试了 7 和 8.1,它没有任何错误。
Xamarin 代码:
public long download(string url, string filePath, string token) {
var manager = (Android.App.DownloadManager)Android.App.Application.Context.GetSystemService(Context.DownloadService);
var source = Android.Net.Uri.Parse(url);
var request = new DownloadManager.Request(source);
request.AllowScanningByMediaScanner();
request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
//request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, source.LastPathSegment);
request.AddRequestHeader("Authorization", "Bearer " + token);
request.AddRequestHeader("Content-type", "application/octet-stream");
long downloadId = manager.Enqueue(request);
return downloadId;
}
Java后端代码:
@RequestMapping(path = "/download/{idBook}/{idDevice}", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void download(@PathVariable int idBook, @PathVariable String idDevice, HttpServletRequest request, HttpServletResponse response) throws Exception
{
String rangestring = request.getHeader("Range");
//System.out.println(rangestring);
if(rangestring != null)
{
String[] strings = (rangestring.substring(6)).split("-");
}
Book book = myBookRepository.getOne(idBook);
String fileName = fileUtil.getFilePath(idBook);
MultipartFileSender.fromFile(new File(fileName))
.with(request)
.with(response)
.serveResource();
}
MultipartFileSender 取自这里