Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:05:29+00:00 2026-06-03T22:05:29+00:00

I’m searching since one week and couldn’t find a proper solution for my problem.

  • 0

I’m searching since one week and couldn’t find a proper solution for my problem. So I’m asking the experts here.

I’m trying to implement a page with FileUpload to upload and show the images after upload on same page.

my problem is, if I set my ManagedBean to @RequestScope, the handleFileUpload function will not be triggered.
If I set it to @ViewScope, the function will be triggered but the error “Error in streaming dynamic resource.” is shown.

here are my files:

  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 Filter
    Faces Servlet

  2. Bean

    @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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:05:31+00:00Added an answer on June 3, 2026 at 10:05 pm

    @Daniel, blieve me. I’ve searched many days before I post my question here. But I have resolved the Problem by myself.

    here is the solution in case that somebody has the same problem:

        @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;
            }
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am currently running into a problem where an element is coming back from
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.