我正在开发一个应用程序,该应用程序将通过键入发件人的 IP:端口,使用 HTTP 服务器(使用 nanoHTTPD)将文件发送到另一台设备。传输工作正常,但我无法接收发送文件的正确文件名(接收者的浏览器将其命名为“默认”,没有任何扩展名)。这是我的 HTTP 服务器代码:
private class WebServer extends NanoHTTPD { public WebServer() { super(8080); } @Override public Response serve(String uri, Method method, Map header, Map parameters, Map files) { //receive my file's path from an intent Intent intent = getIntent(); String filename = intent.getStringExtra(MainActivity.FILENAME); FileInputStream file = null; try { file = new FileInputStream(filename); } catch (FileNotFoundException e) { e.printStackTrace(); } return new NanoHTTPD.Response(Status.OK, "/", file); }
我以为我可以使用 FileInputStream(new File(String path, String name))
修复它,但它仍然无法正常工作,它仍然给我一个带有 0 的“默认”文件名字节文件大小。
任何人都可以告诉我如何从 HTTP 服务器获取正确的文件名吗?希望有人可以在这里帮助我。谢谢!
最佳答案
这取决于您的服务器(脚本)接受的内容,发送时您应该添加带有文件名的 header ,这可以是:
Content-Disposition: attachment; filename="fname.ext"
来自 http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html 的示例“19.5.1 内容处置”
关于android - 如何使用 FileInputStream 设置文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21376293/