FileOutputStream() 可以采用相对路径作为参数吗
我正在创建一个 FileOutputStream 对象。它在其构造函数中接受文件或字符串作为参数。
我的问题是,我可以给它一个相对 URL 作为文件位置的参数吗?它似乎不起作用,但我正在尝试弄清楚这是否可能(如果不可能,我将停止尝试) 。
如果不可能,我如何(从 servlet)获取当前位置的绝对路径(在文件系统上,而不是逻辑 URL),以便将其传递给构造函数。
我的部分问题是我的开发盒是 Windows,但我会将其发布到 Unix 盒,因此路径不能相同,即在 Windows C:/.... 和 unix /usr/... 上
I am creating a FileOutputStream object. It takes a file or String as an argument in its constructor.
My question is, can I give it a relative URL as an argument for the location of a file, it doesn't seem to work, but I am trying to work out if this is possible at all (if not I will stop trying).
If it is not possible, how can I (from a servlet) get the absolute path (on the filesystem, not the logical URL) to the current location in such a way that I can pass that to the constructor.
Part of my problem is that my dev box is Windows but I will publish this to a Unix box, so the paths cannot be the same i.e. on Windows C:/.... and on unix /usr/...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ServletContext.getRealPath(relativePath)
ServletContext.getRealPath(relativePath)
这应该适用于相对路径,但要注意当前目录是什么。例如,如果您使用 Eclipse,则当前目录是项目目录(而不是包含类的目录)。
That should work with a relative path, yet be careful about what the current directory is. For instance if you are using Eclipse the current directory is the project directory (and not the directory containing the classes).
来自文件 javadoc:
编辑:
Bozho 是对的,我没有读到这是一个 servlet。我尊重他的回答:
ServletContext.getRealPath(relativePath)
From the File javadoc:
EDIT:
Bozho is right, I didn't read that this was a servlet. I defer to his answer:
ServletContext.getRealPath(relativePath)