我在我的Android應用程序中使用NanoHTTPD作爲Web服務器。如何在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;
}
}
通話2次MyWebServer的與文件名1和文件名2個 – Pavya
謝謝!但我希望1次調用MyWebServer並下載兩個文件 – HelloCW
return downloadFile(file1);因爲這個返回downloadFile(file2);它永遠不會叫 – Pavya