JSF FileUpload目录 [英] JSF FileUpload Directory

查看:140
本文介绍了JSF FileUpload目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. 将图片保存到我的webApps资源目录中,它在xhtml页面。但不幸的是没有得到webApps的目录来保存它。如何做到这一点?

  2. 我把图片保存在我的webapp外面(如示例所示)。但后来我不得不引用一个外部的图像到XHTML页面,我也不知道该怎么办..


    你可以告诉我如何解决其中的一种方法:-)

    解决方案


    保存在我的webApps的资源目录中的图片,只需在xhtml页面引用它。但不幸的是没有得到webApps的目录来保存它。怎么做?


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

     字符串relativeWebPath =/ upload; 
    字符串absoluteDiskPath = externalContext.getRealPath(relativeWebPath);
    文件file = File.createTempFile(前缀+_,。+后缀,新文件(absoluteDiskPath));
    // ...

    但不推荐!每当您将文件写入公共webcontent时,每当您重新部署webapp或甚至重新启动服务器时,它们都会丢失。这不是永久存储。检查如何以正确的方式保存上传的文件在JSF 。 / p>


    我把图片保存到我的webapp外部(如示例所示)。但后来我不得不引用一个外部的图像到XHTML页面,我也不知道该怎么做。


    路径作为服务器的新上下文(如何做到这一点取决于servletcontainer make / version,或者创建一个servlet,它只需通过 InputStream > FileInputStream ,并将其写入响应的 OutputStream 。另见使用< h:graphicImage>或< img>标签从webapps / webcontext / deploy文件夹外部加载图片。


    Hello I implemented BalusC's Fileupload example and it works just fine :-D My question is related to the File direcory. In the example the directory lies on c:\upload. My intention is to change my webApps header-image via fileupload. To achieve that I have to different ways in my mind:

    1. 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?

    2. 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..

    Can you please show me how to solve one of those ways :-)

    解决方案

    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?

    Technically, you can use ExternalContext#getRealPath() to convert a relative web path to an absolute disk file system path.

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

    But this is not recommended! Whenever you write files to public webcontent, they will all get lost whenever you redeploy the webapp or even when you restart the server. It's not intented as permanent storage. Check How to save uploaded file in JSF for the right way.

    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.

    Either add the path as new context of the server (how to do this depends on the servletcontainer make/version, or create a servlet which just gets an InputStream of it by FileInputStream and writes it to the OutputStream of the response. See also Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag.

    这篇关于JSF FileUpload目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆