0

在 Android端使用下载管理器从不同来源下载文件。现在我应该为这个应用程序创建一个服务器端

首先这里是Android端的简单代码:

private DownloadManager mgr = null;
private long lastDownload = -1L;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mgr = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    registerReceiver(onNotificationClick, new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED));
}

@Override
public void onDestroy() {
    super.onDestroy();

    unregisterReceiver(onComplete);
    unregisterReceiver(onNotificationClick);

}

public void startDownload(View v) {
    // Uri uri = Uri.parse("http://commonsware.com/misc/test.mp4");
    Uri uri = Uri.parse("http://xxx.xxx.xxx.xxx:8080/FileUpload/asd.mp3");

    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();

    lastDownload = mgr.enqueue(new DownloadManager.Request(uri)
            .setAllowedNetworkTypes(
                    DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
            .setAllowedOverRoaming(false).setTitle("Demo")
            .setDescription("Something useful. No, really.")
            .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "test.mp4"));

    v.setEnabled(false);
    findViewById(R.id.query).setEnabled(true);
}

public void queryStatus(View v) {
    Cursor c = mgr.query(new DownloadManager.Query().setFilterById(lastDownload));

    if (c == null) {
        Toast.makeText(this, "Download not found!", Toast.LENGTH_LONG).show();
    } else {
        c.moveToFirst();

        Log.d(getClass().getName(),
                "COLUMN_ID: " + c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID)));
        Log.d(getClass().getName(),
                "COLUMN_BYTES_DOWNLOADED_SO_FAR: "
                        + c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)));
        Log.d(getClass().getName(),
                "COLUMN_LAST_MODIFIED_TIMESTAMP: "
                        + c.getLong(c.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP)));
        Log.d(getClass().getName(),
                "COLUMN_LOCAL_URI: " + c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
        Log.d(getClass().getName(),
                "COLUMN_STATUS: " + c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)));
        Log.d(getClass().getName(),
                "COLUMN_REASON: " + c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON)));

        Toast.makeText(this, statusMessage(c), Toast.LENGTH_LONG).show();
    }
}

public void viewLog(View v) {
    startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
}

private String statusMessage(Cursor c) {
    String msg = "???";

    switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
    case DownloadManager.STATUS_FAILED:
        msg = "Download failed!";
        break;

    case DownloadManager.STATUS_PAUSED:
        msg = "Download paused!";
        break;

    case DownloadManager.STATUS_PENDING:
        msg = "Download pending!";
        break;

    case DownloadManager.STATUS_RUNNING:
        msg = "Download in progress!";
        break;

    case DownloadManager.STATUS_SUCCESSFUL:
        msg = "Download complete!";
        break;

    default:
        msg = "Download is nowhere in sight";
        break;
    }

    return (msg);
}

BroadcastReceiver onComplete = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        findViewById(R.id.start).setEnabled(true);
    }
};

BroadcastReceiver onNotificationClick = new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {
        Toast.makeText(ctxt, "Ummmm...hi!", Toast.LENGTH_LONG).show();
    }
};

如您所见,我尝试使用它。如果我将一些文件放入WebProject -> WebContent *中,我就可以下载它*. 但我需要访问服务器计算机中的所有文件。(我认为我应该使用可以处理下载管理器请求并可以上传文件的 servlet,但我不确定。)Android 的下载管理器只能处理HTTP 和 HTTPS。

我会实现服务器端,但我真的不知道怎么做,所以问题是:我怎样才能实现可以满足下载管理器请求的正确服务器端。

如果你能帮助到你,谢谢。

4

2 回答 2

0

编写一个网络服务。您可以使用Jersey之类的框架来处理下载请求。请求可以包含路径/查询参数,这些参数可以帮助服务确定客户端想要哪个文件。它还可以通过多种方式提供身份验证。

至于后端,如果可靠性不是问题,您可以将文件保存到公共磁盘。或者,您可以使用网络存储设备保存文件。您也可以将其上传到Amazon S3等第三方,只需将文件的下载链接提供给客户端即可。因此,该服务将充当发现下载链接的方式。它本身不会托管文件。

任你选。

于 2013-04-06T08:40:42.783 回答
0

我为我的问题找到了一个简单的解决方案,但我想说谢谢你的回答!

简单的解决方案:

    File f = new File("d:\\....");
    int length = 0;
    ServletOutputStream op = response.getOutputStream();
    ServletContext context = getServletConfig().getServletContext();
    String mimetype = context.getMimeType("d:\\Zene\\WakaWaka.mp3");

    //
    // Set the response and go!
    //
    //
    response.setContentType((mimetype != null) ? mimetype : "application/octet-stream");
    response.setContentLength((int) f.length());
    response.setHeader("Content-Disposition", "attachment; filename=\"" + "seven-eurotura.cd1.avi" + "\"");

    //
    // Stream to the requester.
    //
    byte[] bbuf = new byte[100];
    DataInputStream in = new DataInputStream(new FileInputStream(f));

    while ((in != null) && ((length = in.read(bbuf)) != -1)) {
        op.write(bbuf, 0, length);
    }
于 2013-04-07T21:14:16.460 回答