我在我的Android应用程序中使用NanoHTTPD作为Web服务器.
我将两个文件名从客户端浏览器传递给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;
}
}
Run Code Online (Sandbox Code Playgroud)
Hos*_*ifi -1
在 Java 中,当您希望函数返回多个值时,必须将这些值嵌入到返回的对象中。因此,您需要定义一个类(例如:),ReturnDownlowds
它可以有两个对象:
public class ReturnDownlowds {
private File downloadFile1;
private File downloadFile2;
Run Code Online (Sandbox Code Playgroud)
// 添加其他字段、构造函数和访问器
}
在您的响应服务器方法中,您需要调用此类的示例,如下所示:
return new ReturnDownlowds(downloadFile1, downloadFile2) ;
Run Code Online (Sandbox Code Playgroud)
我希望这会有所帮助。
归档时间: |
|
查看次数: |
1347 次 |
最近记录: |