2014-08-30 82 views
2

我在我的應用程序Restlet服務器和如果我在瀏覽器中打開服務器地址,然後服務器應返回文件列表中的某些文件夾,如果用戶點擊文件,那麼它應該下載文件。Android Restlet框架 - 獲取文件列表並下載文件

我看着這個tutorial。第6部分提供靜態文件。 It's此代碼:

public static final String ROOT_URI = "file:///c:/restlet/docs/api/"; 

[...] 

// Create a component 
Component component = new Component(); 
component.getServers().add(Protocol.HTTP, 8182); 
component.getClients().add(Protocol.FILE); 

// Create an application 
Application application = new Application() { 
    @Override 
    public Restlet createInboundRoot() { 
      return new Directory(getContext(), ROOT_URI); 
    } 
}; 

// Attach the application to the component and start it 
component.getDefaultHost().attach(application); 
component.start(); 

,但如果我在URI使用例如file:///sdcard/,那麼如果我在瀏覽器中打開地址,我得到

Not Found

The server has not found anything matching the request URI

You can get technical details here.

Please continue your visit at our home page.

有什麼不對?我如何使用Restlet返回文件列表?

回答

2

我的解決辦法:

mWebServerComponent = new Component(); 
    mWebServerComponent.getClients().add(Protocol.FILE); 
    mWebServerComponent.getServers().add(Protocol.HTTP, 8182); 
    mWebServerComponent.getDefaultHost().attach(new FileApplication()); 
    try { 
     mWebServerComponent.start(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

而且FileApplication類:

public class FileApplication extends Application { 

// for example: public static final String ROOT_URI = 
// "file:///C:/restlet-jee-2.0.6/docs/api/"; 
@Override 
public synchronized Restlet createInboundRoot() { 

    String ROOT_URI = "file:///" 
      + Environment.getExternalStorageDirectory() + "/"; 

    Directory directory = new Directory(getContext(), 
      LocalReference.localizePath(ROOT_URI)); 
    directory.setListingAllowed(true); 
    Router router = new Router(getContext()); 

    router.attach("/files", directory); 

    return router; 
} 
}