【问题标题】:Get Filename when downloading with file DownloadManager使用文件 DownloadManager 下载时获取文件名
【发布时间】:2015-06-19 05:13:36
【问题描述】:

我正在编写一个简单的 sn-p 来从本地服务器下载文件。

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 进行排队时获取文件名。请告诉我是否需要编辑网络服务。

【问题讨论】:

  • 抱歉,如果您要求下载文件,那么您已经知道我想的那个文件的名称。请详细说明。
  • 请说明使用的网址是什么样的。标题中的文件名?什么标题?
  • @greenapps :网址看起来像这样http://localhost:8080/NFZ_WS/rest/hello/geojson。我没有从 url 获取文件名。
  • 通知区域的标题。
  • 您可以先执行 HEAD 语句,然后执行 GET。或者不使用 DownloadManager,而是自己使用 http 组件。

标签: android android-download-manager


【解决方案1】:

您的 DownloadManager 当前为您处理“GET”,除非您使用 HttpGet、HttpClient 类,否则您不必显式执行此操作。这是一个link to implement a HEAD statement,用于获取从 Web 服务下载的文件的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多