gpt4 book ai didi

java - JSF 文件上传目录

转载 作者:行者123 更新时间:2023-12-01 15:57:28 24 4
gpt4 key购买 nike

你好,我实现了 BalusC 的 Fileupload example它工作得很好:-D我的问题与文件目录有关。在示例中,目录位于 c:\upload 。我的目的是通过文件上传更改我的 webApps 标题图像。为了实现这一目标,我必须采取不同的方式:

  1. 将图片保存在我的 webApps 的资源目录中,并在 xhtml 页面中简单地引用它。但不幸的是,没有获取 webApps 的目录来将其保存在那里。如何做到这一点?

  2. 我将图片保存在我的网络应用程序之外(如示例所示)。但随后我必须将外部图像引用到 xhtml 页面中,我也不知道该怎么做..

您能否告诉我如何解决其中一种方法:-)

最佳答案

Save the picture in my webApps' resources directory and the simply reference it at xhtml page. But unfortunately don't get the webApps's directory to save it there. How to do so?

技术上,您可以使用 ExternalContext#getRealPath() 将相对 Web 路径转换为绝对磁盘文件系统路径。

String relativeWebPath = "/upload";
String absoluteDiskPath = externalContext.getRealPath(relativeWebPath);
File file = File.createTempFile(prefix + "_", "." + suffix, new File(absoluteDiskPath));
// ...

但不建议这样做!每当您将文件写入公共(public)网络内容时,只要您重新部署网络应用程序,甚至重新启动服务器,它们都会丢失。它的目的不是永久存储。检查 How to save uploaded file in JSF 是否正确。

I save the picture outside my webapp (as shown in the example). But then I have to reference an external image into xhtml pages, which I also don't know how to do.

要么将路径添加为服务器的新上下文(如何执行此操作取决于 servletcontainer make/version,要么创建一个 servlet,该 servlet 仅通过 InputStream 获取其中的 FileInputStream 并将其写入响应的 OutputStream。请参阅还有Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag

关于java - JSF 文件上传目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4817600/

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