Android的下载管理器发出HTTP / HTTPS文件共享服务器 [英] Making http/https file sharing server for Android Download Manager

查看:502
本文介绍了Android的下载管理器发出HTTP / HTTPS文件共享服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

的一切都在这里首先是一个简单的code为Android端:

First of all here is the simple code for the Android side:

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();
    }
};

正如你可以看到我trye​​d使用它。如果我把一些文件放到一个 WebProject - >的WebContent *的比我可以下载的*。但我需要访问服务器计算机的所有文件。 (我想,我应该使用servlet可以处理下载管理器的请求,并能以某种方式上传文件,但林不知道这一点。)而Android的下载管理器只能处理 HTTP和HTTPS。

As you can see I tryed to use it. If I put some files into a WebProject -> WebContent *than I can download it*. But I would need to access all files in the server computer. (I think that I should use a servlet which can handle the Download Manager's request and can upload somehow the file, but Im not sure of it.) The Android's Download Manager can handle only HTTP and HTTPS.

我想实现在服务器端,但我真的不知道该怎么做,所以问题是:如何实现一个合适的服务器端可以服务于该下载管理器的要求

I would implement the server side, but I really don't know how to do it, so the question is: How can I implement a right server side which can serve the Download Manager's request.

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

Thank you if you can help you.

推荐答案

我发现我的问题一个简单的解决方案,但我想说,谢谢您的回答!

I found a simple solution for my problem BUT I want to say THANK YOU for your answer!

简单的解决方案:

    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);
    }

这篇关于Android的下载管理器发出HTTP / HTTPS文件共享服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆