Vin*_*lya 5 filenames httpresponse http-headers nanohttpd
我已经使用NanoHttpd在本地网络上成功实现了文件传输.但是,我无法在NanoHttpd Response中发送文件名.收到的文件具有如下默认名称:localhost_8080.我尝试在响应标头中附加文件名Content-disposition,但我的文件传输失败了.我究竟做错了什么?这是我的实现:
private class WebServer extends NanoHTTPD {
String MIME_TYPE;
File file;
public WebServer() {
super(PORT);
}
@Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
try {
file=new File(fileToStream);
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
MIME_TYPE= URLConnection.guessContentTypeFromName(file.getName());
} catch (IOException ioe) {
Log.w("Httpd", ioe.toString());
}
NanoHTTPD.Response res=new NanoHTTPD.Response(Status.OK, MIME_TYPE, bis);
res.addHeader("Content-Disposition: attachment; filename=", file.getName());
return res;
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
您需要指定响应,MIME类型和要发送的字节流.之后,您只需添加一个带有文件文件名的标头,因为它是一个http方法.这是一个解决问题的示例代码
@Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
NanoHTTPD.Response res = new NanoHTTPD.Response(Response.Status.OK, "application/vnd.android.package-archive", fis);
res.addHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"");
return res;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3073 次 |
| 最近记录: |