我在我的Android应用中使用NanoHTTPD作为网络服务器。
我将两个文件名从客户端浏览器传递给NanoHTTPD服务器,我希望一次下载这两个文件,但以下代码只下载file1,而不下载file2。
如何一次下载两个或多个文件?
public class MyWebServer extends NanoHTTPD
{
private final String rootDir;
public MyWebServer(int port, String rootDir)
{
super("192.168.1.4", port);
this.rootDir = rootDir;
}
@Override
public Response serve(IHTTPSession session)
{
Map<String, String> parms = session.getParms();
String filename1=GetFilename(parms);
String filename2=GetFilename(parms);
File file1 = new File(rootDir + filename1);
File file2 = new File(rootDir + filename2);
return downloadFile(file1);
return downloadFile(file2);
}
private Response downloadFile(File file)
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
} catch (FileNotFoundException ex)
{
Logger.getLogger(MyWebServer.class.getName()).log(Level.SEVERE, null, ex);
}
return newFixedLengthResponse(Response.Status.OK, "application/octet-stream", fis, file.getTotalSpace());
}
@Override
public Response newFixedLengthResponse(IStatus status, String mimeType, String message)
{
Response response = super.newFixedLengthResponse(status, mimeType, message);
response.addHeader("Accept-Ranges", "bytes");
return response;
}
}
答案 0 :(得分:4)
当您在MyWebServer构造函数中调用super(..)
时,NanoHTTPD构造函数下面会创建一个异步任务。
public NanoHTTPD(String hostname, int port) {
this.hostname = hostname;
this.myPort = port;
setTempFileManagerFactory(new DefaultTempFileManagerFactory());
setAsyncRunner(new DefaultAsyncRunner());
// creates a default handler that redirects to deprecated serve();
this.httpHandler = new IHandler<IHTTPSession, Response>() {
@Override
public Response handle(IHTTPSession input) {
return NanoHTTPD.this.serve(input);
}
};
}
所以答案必须是每个操作都有一个downloadFile()
。也就是说,您应该调用new MyWebServer()
来启动每个下载任务。
@Override
public Response serve(IHTTPSession session)
{
Map<String, String> parms = session.getParms();
String filename=GetFilename(parms);
File file = new File(rootDir + filename);
return downloadFile(file);
}
修改强>
通过使用2个MyWebServer类实例,你可以收听2个参数中每个参数的客户端请求。 (我没试过这个,但它必须工作,我建议你应该在客户端发送多个请求,在服务器端发送一个webserver实例)。
public class MyWebServer extends NanoHTTPD
{
private final String rootDir;
private final int param_type = 1; //default 1
public MyWebServer(int port, String rootDir, int param)
{
super("192.168.1.4", port);
this.rootDir = rootDir;
this.param_type = param;
}
@Override
public Response serve(IHTTPSession session)
{
Map<String, String> parms = session.getParms();
File file;
if (param_type == 1){
String filename=parms.get("param1");
file = new File(rootDir + filename);
}else if(param_type == 2){
String filename=parms.get("param2");
file = new File(rootDir + filename);
}
return downloadFile(file);
}
private Response downloadFile(File file)
{
FileInputStream fis = null;
try
{
fis = new FileInputStream(file);
} catch (FileNotFoundException ex)
{
Logger.getLogger(MyWebServer.class.getName()).log(Level.SEVERE, null, ex);
}
return newFixedLengthResponse(Response.Status.OK, "application/octet-stream", fis, file.getTotalSpace());
}
@Override
public Response newFixedLengthResponse(IStatus status, String mimeType, String message)
{
Response response = super.newFixedLengthResponse(status, mimeType, message);
response.addHeader("Accept-Ranges", "bytes");
return response;
}
}
在MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyWebServer server1 = new MyWebServer(port, rootDir, 1);
MyWebServer server2 = new MyWebServer(port, rootDir, 2);
server1.start();
server2.start();
}
答案 1 :(得分:-1)
在Java中,如果希望函数返回多个值,则必须将这些值嵌入到返回的对象中。
所以你需要定义一个类(例如:ReturnDownlowds
),它可以有两个对象:
public class ReturnDownlowds {
private File downloadFile1;
private File downloadFile2;
//添加其他字段,构造函数和访问器
}
在您的Response服务器方法中,您需要调用此类的示例,如下所示:
return new ReturnDownlowds(downloadFile1, downloadFile2) ;
我希望这会有所帮助。