Android 使用文件下载管理器下载时获取文件名

Android 使用文件下载管理器下载时获取文件名,android,android-download-manager,Android,Android Download Manager,我正在编写一个简单的代码片段,从本地服务器下载一个文件 Uri uri = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(uri); String filePath= null; filePath = getApplicationContext().getFilesDir().getAbsolutePath(); filePath = this.getFilesDir().getAbso

我正在编写一个简单的代码片段,从本地服务器下载一个文件

Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
String filePath= null;
filePath = getApplicationContext().getFilesDir().getAbsolutePath();
filePath = this.getFilesDir().getAbsolutePath();

HERE-----**//request.setDescription("Downloading GeoJSON").setTitle("filename");**

//request.setDestinationUri(Uri.parse(filePath));
request.setDestinationInExternalPublicDir("Folder", "files");
request.setVisibleInDownloadsUi(true);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setMimeType("application/octet-stream");
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadReference = downloadManager.enqueue(request);
我想在标题中设置文件名,还想用相同的文件名保存文件

我知道我们可以在BroadcastReceiver中提取文件名,但只有在下载完成后才能接收广播

以下是网页服务的代码-

@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Path("/geojson2")
public Response getFile() {
  File file = new File(GEOJSON_FILE_PATH);
  return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
      .header("Content-Disposition", "attachment; filename=" + file.getName()) //optional
      .header("Content-Length", file.length()+"")
      .build();
}
Web服务URL如下所示- http://localhost:8080/NFZ_WS/rest/hello/geojson


因此,我的问题是如何在对URI排队时获取文件名。请告诉我是否需要编辑web服务。

您的DownloadManager目前为您处理GET,除非您使用的是HttpGet、HttpClient类,否则您不必显式执行此操作。下面是一个示例,用于获取从web服务下载的文件的名称

很抱歉,如果你要求下载一个文件,那么我想你已经知道该文件的名称了。请详细说明。请告诉我使用的url是什么样子的。标题中的文件名?什么的标题?@greenapps:url如下所示http://localhost:8080/NFZ_WS/rest/hello/geojson. 我无法从url获取文件名。标题用于通知区域。您可以先执行HEAD语句,然后执行get。或者不使用DownloadManager,而是自己使用http组件。