我正在尝试将一些图像存储在Glassfish 4备用doc根目录中。我们的想法是允许管理员使用Prime Faces图像切换保存将在索引页面上显示的图像。这基本上是一个自动幻灯片。我尝试使用备用文档根目录,因为任何上传的图片可能无法在使用Glassfich重新部署的应用程序中存活
我的glassfish-web.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
<context-root>realtyfreedom</context-root>
<property description="Static Images" name="alternatedocroot_1" value="from=/static/images/* dir=/private/var/realtyfreedom" />
<class-loader delegate="true"/>
<jsp-config>
<property name="keepgenerated" value="true">
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
</jsp-config>
</glassfish-web-app>
我的命名bean看起来像这样:
package net.realtyfreedom.beans;
import java.io.Serializable;
import java.util.Arrays;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.*;
import javax.inject.Named;
import net.realtyfreedom.utils.SlideRetriever;
//import org.apache.log4j.*;
import java.util.logging.Logger;
@Named
@SessionScoped
public class SlideList implements Serializable
{
private final String[] images = new String[100];
private final String facesContext = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
//private final String imagePath = facesContext + "/images/";
private final String imagePath = "/realtyfreedom/static/images/index.xhtml";
private final Logger logger = Logger.getLogger(this.getClass().getName());
public String getFacesContext()
{
logger.info("log4j: Faces Context is: " + facesContext);
return imagePath;
}
public String[] getImages() {
SlideRetriever sr = new SlideRetriever();
return sr.getImages();
}
public String getImageNames() {
SlideRetriever sr = new SlideRetriever();
sr.init(imagePath);
logger.info("App log array: sr: sr list" + Arrays.toString(sr.getImages()));
String[] imageList = sr.getImages();
//logger.("info: image files: " + imageList.length + " " + Arrays.toString(imageList));
return Arrays.toString(imageList);
}
}
这是检索文件数组的实用程序类
package net.realtyfreedom.utils;
import java.io.File;
import java.io.Serializable;
import java.util.Arrays;
import javax.faces.context.FacesContext;
import java.util.logging.Logger;
public class SlideRetriever implements Serializable
{
private String[] images = new String[100];
private final String facesContext = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/images/slideshow");
//private final String imagePath = facesContext + "/resources/images/slideshow";
private String imagePath = "";
private final Logger logger = Logger.getLogger(this.getClass().getName());
public SlideRetriever()
{
}
public void init(String ip)
{
imagePath = ip;
try
{
File folder = new File(imagePath);
logger.info("App: Folder path is: " + imagePath);
logger.info("App: Canonical Path : " + folder.getCanonicalPath());
images = folder.list();
} catch (Exception ex)
{
System.out.println(ex.getStackTrace());
}
}
public String[] getImages()
{
logger.info("Debug: " + Arrays.toString(images));
return images;
}
}
结果是SlideRetriever类的init()方法中的字符串数组图像返回null。但是,当我在控制台应用程序中运行此代码时,我会得到一个文件名列表。此外,我可以使用图像路径以及static / images目录中的现有文件,我可以在浏览器中显示图像。
我在Glassfish 3上看到了一个类似的问题,但我没有看到Glassfish 4的这个问题。在Glassfish 3中,我认为最终的答案是开发人员将图像写入数据库。如果可能的话,我想避免这种情况。任何帮助将不胜感激。抱歉代码的粗略状态。