I use NanoHTTPD as web server in my Android app.
I pass two file names from client browser to NanoHTTPD server, and I hope to download the two files at a time, but the following code only download the file1, and the file2 isn't downloaded.
How to download two or multiple files at a time?
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;
}
}
Below NanoHTTPD constructor creates an async task when you call super(..)
in the MyWebServer constructor.
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);
}
};
}
So the answer must be to have one downloadFile()
for each operation. That is to say you should call new MyWebServer()
to start each download task.
@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);
}
EDIT:
By using 2 instance of MyWebServer class, u can listen to client requests for each of 2 params. (I didn't try this but it must work and I recommend you should send multiple requests on the client side and one webserver instance on the server side).
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;
}
}
In 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With