2013-04-06 123 views
0

我正在使用下載管理器的Android方下載來自不同來源的文件。現在我應該爲這個應用程序創建一個服務器端製作http/https文件共享服務器的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(); 
    } 
}; 

正如你可以看到我tryed使用它。如果我把一些文件放入WebProject - > WebContent * 比我可以下載它 *。但我需要訪問服務器計算機中的所有文件。 (我認爲我應該使用一個servlet,它可以處理下載管理器的請求,並可以以某種方式上傳文件,但我不確定它。)Android的下載管理器只能處理HTTP和HTTPS。

我將實現服務器端,但我真的不知道該怎麼做,所以問題是:我如何能實現一個合適的服務器端可以服務於該下載管理器的請求。

謝謝,如果你能幫助你。

+0

問題是什麼? – 2013-04-06 08:13:28

+0

我會實現服務器端,但我真的不知道該怎麼做,所以問題是:我怎樣才能實現一個正確的服務器端,可以服務於下載管理器的請求。 – TAR515 2013-04-06 08:23:50

回答

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

編寫一個web服務。您可以使用像Jersey這樣的框架來處理下載請求。該請求可以包含路徑/查詢參數,這些參數可以幫助服務確定客戶端需要哪個文件。它也可以通過幾種方式提供身份驗證。

至於後端,如果可靠性不是問題,則可以將文件保存到通用磁盤。或者,您可以使用網絡存儲設備保存文件。您也可以將其上傳到第三方,如Amazon S3,並簡單地將文件的下載鏈接提供給客戶端。因此,該服務將用作發現下載鏈接的方式。它不會託管文件本身。

請選擇。

相關問題