Primefaces多个FileUpload不显示文件也不触发handleFileUpload

时间:2012-05-09 20:04:49

标签: file-upload primefaces uirepeat

我正在搜索一周,但找不到适合我问题的解决方案。所以我在这里问专家。

我正在尝试使用FileUpload实现一个页面,以便在上传到相同页面后上传并显示图像。

我的问题是,如果我将ManagedBean设置为@RequestScope,则不会触发handleFileUpload函数。 如果我将其设置为@ViewScope,则会触发该函数,但错误“流动态资源中出错”。显示。

这是我的档案:

  1. 的web.xml

    <filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
    <init-param>
        <param-name>thresholdSize</param-name>
        <param-value>2097152</param-value>
    </init-param>
    <init-param>
        <param-name>uploadDirectory</param-name>
        <param-value>E:/uploadedImages</param-value>
    </init-param>
    

        PrimeFaces FileUpload过滤器     面对Servlet

  2. @ManagedBean
    @RequestScoped
    public class ImageManager implements Serializable {
    
    private final static int MAX_UPLOADED_FILES = 5;
    private final static String UPLOADED_FILES_PATH = "E:/uploadedImages";
    
    private final Map<UUID, UploadedFile> uploadedFiles = new HashMap<>();
    
    public List<String> getListImages() {
        final List<String> result = new ArrayList<>();
    
        for (final UUID uuid : uploadedFiles.keySet())
            result.add(uuid.toString());
    
        return result;
    }
    
    public StreamedContent getImage() {
        final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        final String imageId = (String) externalContext.getRequestMap().get("imageId");
    
        if (imageId != null) {
            final UUID imageIndex = UUID.fromString(imageId);
            return new DefaultStreamedContent(new ByteArrayInputStream(uploadedFiles.get(imageIndex).getContents()), "image/jpg");
        }
    
        return null;
    }
    
    public void handleFileUpload(FileUploadEvent event) {
        if (uploadedFiles.size() < MAX_UPLOADED_FILES) {
            final UploadedFile uploadedFile = event.getFile();
            if (uploadedFile != null) {
                uploadedFiles.put(UUID.randomUUID(), uploadedFile);
            }
        }
    }
    
    }
    
  3. XHTML

                  <p:fileUpload fileUploadListener="#{imageManager.handleFileUpload}"  
                            mode="advanced"
                            multiple="true"  
                            sizeLimit="2097152"
                            allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                            uploadLabel="Hochladen"
                            auto="false"
                            cancelLabel="Abbrechen"
                            invalidFileMessage="Die ausgewählte Datei ist kein zugelassene Bilddatei"
                            invalidSizeMessage="Die maximale Bildgröße ist 2MB"
                            label="Datei Auswählen"
                            update="imageList"
                            />
    
    
    
        <ui:repeat value="#{imageManager.listImages}" var="imageId" id="imageList">
            <h:outputText value="#{imageId}" />
            <p:graphicImage value="#{imageManager.image}">
                <f:param id="imageId" name="imageId" value="#{imageId}" />
            </p:graphicImage>
        </ui:repeat>  
    

1 个答案:

答案 0 :(得分:0)

@Daniel,谢谢我。在我发布问题之前,我已经搜索了很多天。但我自己解决了这个问题。

这是解决方案,以防有人遇到同样的问题:

    @Controller
    @Scope(WebApplicationContext.SCOPE_REQUEST)
    public class ImageManager implements Serializable {

        private final static int MAX_UPLOADED_FILES = 5;
        private final static String UPLOADED_FILES_PATH = "E:/uploadedImages";

        private final static Map<UUID, UploadedFile> uploadedFiles = new HashMap<>();

        private final static List<String> listImages = new ArrayList<>();
        private StreamedContent image;

        private static StreamedContent defaultImage = null;
        static {
            try {
                defaultImage = new DefaultStreamedContent(new FileInputStream(new File("E:\\uploadedImages\\t.jpg")), "image/jpg");
            }
            catch (FileNotFoundException e) {
                e.printStackTrace();
            }        
        }

        public List<String> getListImages() {
            return listImages;
        }

        public StreamedContent getImage() {
            final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
            final String imageId = (String) externalContext.getRequestParameterMap().get("imageId");

            if (imageId != null) {
                final UUID imageIndex = UUID.fromString(imageId);
                image = new DefaultStreamedContent(new ByteArrayInputStream(uploadedFiles.get(imageIndex).getContents()), "image/jpg");

                return image;
            }

            return defaultImage;
        }

        public void handleFileUpload(FileUploadEvent event) {
            if (uploadedFiles.size() < MAX_UPLOADED_FILES) {
                final UploadedFile uploadedFile = event.getFile();
                if (uploadedFile != null) {
                    final UUID uuid = UUID.randomUUID();
                    uploadedFiles.put(uuid, uploadedFile);
                    listImages.add(uuid.toString());
                }
            }
        }

        public void setImage(StreamedContent image) {
            this.image = image;
        }

    }