gpt4 book ai didi

android - 如何使用 NanoHTTPD 在 Android 中一次下载两个或多个文件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:27 29 4
gpt4 key购买 nike

我在我的 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;
}


}

最佳答案

下方NanoHTTPD当您在 MyWebServer 构造函数中调用 super(..) 时,构造函数会创建一个异步任务。

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);
}

编辑:

通过使用 MyWebServer 类的 2 个实例,您可以监听 2 个参数中每个参数的客户端请求。 (我没有尝试过,但它一定可以工作,我建议您应该在客户端发送多个请求,在服务器端发送一个网络服务器实例)。

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();

}

关于android - 如何使用 NanoHTTPD 在 Android 中一次下载两个或多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42240182/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com