【问题标题】:Android Restlet framework - Get list of the Files and download fileAndroid Restlet 框架 - 获取文件列表并下载文件
【发布时间】:2014-08-30 09:06:13
【问题描述】:

我的应用程序中有 Restlet 服务器,如果我在浏览器中打开服务器地址,则服务器应返回某个文件夹中的文件列表,如果用户单击该文件,则应下载该文件。

我看了这个tutorial。第 6 部分服务静态文件。这是这段代码:

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/,那么如果我在浏览器中打开地址,我会得到 ​​p>

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 返回文件列表?

【问题讨论】:

    标签: java android restlet restlet-2.0


    【解决方案1】:

    我的解决方案:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2022-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      相关资源
      最近更新 更多