我在我的应用程序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/
,那么如果我在浏览器中打开地址,我会得到
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返回文件列表?
答案 0 :(得分: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;
}
}