如何将文件写入应用程序的资源/图像文件夹? [英] How to write a file to resource/images folder of the app?

查看:176
本文介绍了如何将文件写入应用程序的资源/图像文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传一张图片并将其存储在服务器上,然后用h:graphicImage显示它。我想将其存储在应用程序的资源/图像中。我正在使用glassfish 4.现在,文件转到domain1\generated\jsp\FileUpload。谢谢

我的表格

 < h:form id = formenctype =multipart / form-data> 
< h:messages />
< h:panelGrid columns =2>
< h:outputText value =File:/>
< h:inputFile id =filevalue =#{uploadPage.uploadedFile}/>
< / h:panelGrid>
< br />< br />
< h:commandButton value =上传文件action =#{uploadPage.uploadFile}/>
< / h:表格>

我的bean

  @Named 
@ViewScoped
public class UploadPage {
private part uploadedFile;
$ b public void uploadFile(){
File file = File.createTempFile(somefilename-,.jpg,new File(C:\\var\\ webapp\\images));
uploadedFile.write(file.getAbsolutePath());


$ b $ / code $ / $ p

解决方案


我想将其存储在应用程序的资源/图片中

不,请不要。 WAR部署空间不是作为永久文件存储位置。所有这些上传的文件都会在重新部署Web应用程序时丢失,原因很简单,因为它们不包含在原始的WAR中。在一个非常密切相关的问题上也可以看到一个详尽的解释:上传的图片只有在刷新页面后才可用。b / b>
$ b





现在,该文件转到domain1\generated\jsp\FileUpload。


因为你在 Part#write()中指定了一个相对路径。它相对于你无法控制的当前工作目录。请参阅详细解释此相关答案: getResourceAsStream()与FileInputStream 。你需要指定一个绝对路径,换句话说,用 / 开始路径。




考虑到您使用的是Glassfish,在上传的图片只有在刷新页面后才能使用。简而言之:
$ b


  1. 创建一个 / var / webapp / images 夹。请注意,此路径只是示例,完全免费供您选择。另外请注意,当您使用Windows时,使用 C:\ 磁盘时,该路径相当于 C:\var\webapp \images

     路径

  2. file = Files.createTempFile(Paths.get(/ var / webapp / images),somefilename-,.jpg,);

    (InputStream input = uploadedFile.getInputStream()){
    Files.copy(input,file,StandardCopyOption.REPLACE_EXISTING);
    }

    imageFileName = file.getFileName()。toString();
    // ...

(注意:正在使用Files#createTempFile()自动生成一个唯一的文件名,否则当新上传的文件(重合)具有完全相同的文件名时,以前上传的文件将被覆盖。告诉GlassFish在 / var / webapp / images 上注册一个虚拟主机,以便所有文件都可用 http://example.com/images 通过将以下条目添加到 /WEB-INF/glassfish-web.xml webapp:

< property name =alternatedocroot_1value =from = images / * dir = / var / webapp/>

(注意: alternatedocroot_1 必须如果有多个,请将其命名为 alternatedocroot_2 等;还要注意 / images 部分确实不应该被包含在 dir 属性中,这不是一个错字) 现在你可以按如下方式显示它:

< h:graphicImage value = /images/#{bean.imageFileName}/>

(注意:使用属性,而不是名称属性)



I would like to upload an image and store it on the server, and later to show it with h:graphicImage? I would like to store it in "resources/images" of the app. I am using glassfish 4. Right now, the file goes to "domain1\generated\jsp\FileUpload". Thank you

My form

<h:form id="form" enctype="multipart/form-data">
        <h:messages/>
        <h:panelGrid columns="2">
            <h:outputText value="File:"/>
            <h:inputFile id="file" value="#{uploadPage.uploadedFile}"/>
        </h:panelGrid>
        <br/><br/>
        <h:commandButton value="Upload File" action="#{uploadPage.uploadFile}"/>
</h:form>

My bean

@Named
@ViewScoped
public class UploadPage {       
    private Part uploadedFile; 

    public void uploadFile(){
       File file = File.createTempFile("somefilename-", ".jpg", new File("C:\\var\\webapp\\images"));
    uploadedFile.write(file.getAbsolutePath());

    }
}

解决方案

I would like to store it in "resources/images" of the app

No, please don't. The WAR deploy space isn't intented as permanent file storage location. All of those uploaded files would get lost whenever you redeploy the webapp for the very simple reason that they are not contained in the original WAR. See for an elaborate explanation also this answer on a very closely related question: Uploaded image only available after refreshing the page.


Right now, the file goes to "domain1\generated\jsp\FileUpload".

Because you specified a relative path in Part#write(). It becomes relative to the current working directory which you have no control over. See for an elaborate explanation also this related answer: getResourceAsStream() vs FileInputStream. You need to specify an absolute path, in other words, start the path with /.


Given that you're using Glassfish, the answer in Uploaded image only available after refreshing the page should also do it for you. In a nutshell:

  1. Create a /var/webapp/images folder. Note that this path is just examplary and fully free to your choice. Also note that when you're using Windows with a C:\ disk, then this path is equivalent to C:\var\webapp\images.

  2. Save the uploaded file in there.

    Path file = Files.createTempFile(Paths.get("/var/webapp/images"), "somefilename-", ".jpg", );
    
    try (InputStream input = uploadedFile.getInputStream()) {
        Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);
    }
    
    imageFileName = file.getFileName().toString();
    // ...
    

    (note: Files#createTempFile() is being used to autogenerate an unique filename, otherwise a previously uploaded file would get overwritten when the new uploaded file (by coincidence) has exactly the same filename)

  3. Tell GlassFish to register a virtual host on /var/webapp/images so that all files are available on http://example.com/images by adding the following entry to /WEB-INF/glassfish-web.xml of the webapp:

    <property name="alternatedocroot_1" value="from=/images/* dir=/var/webapp" />
    

    (note: alternatedocroot_1 must be exactly like that, keep it unmodified, if you have multiple, name it alternatedocroot_2, etc; also note that the /images part should indeed not be included in dir attribute, this is not a typo)

  4. Now you can display it as follows:

    <h:graphicImage value="/images/#{bean.imageFileName}" />
    

    (note: use value attribute, not name attribute)

这篇关于如何将文件写入应用程序的资源/图像文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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