Bsr messieurs je fais l'upload de fichiers jpeg je le mets dans un repertoire de l'application appélé images mon problème est que je n'arrive pas a afficher l'image sur ma page jsp au niveau du composant <h:graphicImage/> j'ai besoin d'aide Svp.
Le code de de ma servlet:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 public void submit() {
 
        // Just to demonstrate what information you can get from the uploaded file.
        System.out.println("File type: " + uploadedFile.getContentType());
        System.out.println("File name: " + uploadedFile.getName());
        System.out.println("File size: " + uploadedFile.getSize() + " bytes");
 
        // Prepare filename prefix and suffix for an unique filename in upload folder.
        String prefix = FilenameUtils.getBaseName(uploadedFile.getName());
        String suffix = FilenameUtils.getExtension(uploadedFile.getName());
 
        // Prepare file and outputstream.
        File file = null;
        OutputStream output = null;
 
        try {
            // Create file with unique name in upload folder and write to it.
            //String stringFileUpload= FacesContext.getCurrentInstance().getExternalContext().getResource("/").toString();
           //URL stringFileUpload=context.getResource("/images ");
            //System.out.println("Afficher url images "+stringFileUpload.toString());
            ServletContext servletContext= (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
            String realPath = servletContext.getRealPath("/");
            file = new File(realPath);
            realPath=file.getParentFile().getParent().toString();
            realPath=realPath.concat("\\web\\images");
            System.out.println("realpath:"+realPath);
            file = File.createTempFile(prefix + "_", "." + suffix, new File(realPath));
            output = new FileOutputStream(file);
            IOUtils.copy(uploadedFile.getInputStream(), output);
            fileName = file.getName();
 
            // Show succes message.
            FacesContext.getCurrentInstance().addMessage("uploadForm", new FacesMessage(
                FacesMessage.SEVERITY_INFO, "File upload succeed!", null));
        } catch (IOException e) {
            // Cleanup.
            if (file != null) file.delete();
 
            // Show error message.
            FacesContext.getCurrentInstance().addMessage("uploadForm", new FacesMessage(
                FacesMessage.SEVERITY_ERROR, "File upload failed with I/O error.", null));
 
            // Always log stacktraces (with a real logger).
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(output);
        }
    }
et le code ma page jsp:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t" %>
<%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
<%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
 
<!DOCTYPE html>
 
<f:view>
    <html lang="en">
        <head>
            <title>File upload test</title>
        </head>
        <body>
            <h:form id="uploadForm" enctype="multipart/form-data">
                <h:panelGrid columns="3">
                    <h:outputLabel for="file" value="Select file" />
                    <t:inputFileUpload id="file" value="#{myBean.uploadedFile}" required="true" />
                    <h:message for="file" style="color: red;" />
 
                    <h:panelGroup />
                    <h:commandButton value="Submit" action="#{myBean.submit}">
                        <a4j:support event="onclick" reRender="outp,im,txt"/>
                    </h:commandButton>
                    <h:message for="uploadForm" infoStyle="color: green;" errorStyle="color: red;" />
                </h:panelGrid>
            </h:form>
                    <a4j:outputPanel id="outp" ajaxRendered="true">
                        <h:outputText id="txt" value="#{myBean.fileName}"/>                        
                        <h:graphicImage value="images/#{myBean.fileName}" rendered="true" id="im"/>
                    </a4j:outputPanel>
 
            <h:outputLink value="file/#{myBean.fileName}" rendered="#{myBean.fileName != null}">
                Download back
            </h:outputLink>
 
        </body>
    </html>
</f:view>