Upload image after selected item

2

In my application, the user defines a root directory. After defining the root there is a listing of files in the directory. When the file is selected there is another directory where an image with the same file name exists. I want that when selecting the file it will display the image pertaining to the file. I searched the p:graphicImage component but I did not find a good example. I saw it using library="images" but it only recognizes the images that are in the resources / images folder. I thought if I could change the default location of this library pointing to my images folder I could solve the problem. I wonder if this would be the best solution? Or if you help me solve this problem in another way.

At the moment the page looks like this:

<h:form>
    <p:panel header="Informações dos Itens">
        <h:panelGrid columns="2">
            <p:outputLabel value="Diretório Raiz Origem: " for="diretorioRaizOrigem" />
            <p:inputText id="diretorioRaizOrigem" value="#{questItemController.questItem.diretorioRaizOrigem}">
                <p:ajax event="change" process="@this" immediate="true" />
            </p:inputText>
            <p:outputLabel for="itemOrigem" value="Item Origem" />
            <p:autoComplete id="itemOrigem" value="#{questItemController.questItem.nomeItemOrigem}"
                completeMethod="#{questItemController.listarItensDiretorioOrigem}" dropdown="true" var="bean" itemLabel="#{bean}"
                itemValue="#{bean}" effect="bounce" forceSelection="true" minQueryLength="3">
                <p:ajax event="itemSelect" listener="#{questItemController.itemChange()}" update="itemDestino,imagem" process="@this" />
            </p:autoComplete>
            <p:outputLabel value="Diretório Raiz de Destino" for="diretorioRaizDestino" />
            <p:inputText id="diretorioRaizDestino" value="#{questItemController.questItem.diretorioRaizDestino}" />
            <p:outputLabel value="Item Destino" for="itemDestino" />
            <p:inputText id="itemDestino" value="#{questItemController.questItem.nomeItemDestino}" />
            <p:graphicImage id="imagem" value="" rendered="#{questItemController.mostrarImagem}"/>
        </h:panelGrid>
    </p:panel>
</h:form>
    
asked by anonymous 02.02.2017 / 18:45

1 answer

0

You can create a Servlet that reads the image file in a directory according to a parameter in the url and then passes it as a parameter in the graphicImage url;

urlImagem = "/imagem?nome=nome-da-imagem";

<p:graphicImage id="imagem" url="#{questItemController.urlImagem}" rendered="#{questItemController.mostrarImagem}"/>

Servlet:

@WebServlet("/imagem")
public class LogoMarcaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Path path = Paths.get("diretorio/das/imagens/" + request.getParameter("nome"));
        byte[] data = Files.readAllBytes(path);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        out.write(data);
        response.setContentType("image/png"); 
        response.setHeader("Content-Disposition", "inline");
        response.setBufferSize(out.size());    

        out.writeTo(response.getOutputStream());
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

}
    
13.02.2017 / 15:26