Add folder with images on glassfish web server

2

Good afternoon, folks how do I leave a folder on my glassfish server with some images? When I need it I have direct access to this url ..

Has anyone done this yet?

    
asked by anonymous 17.06.2015 / 19:23

2 answers

2

If you want to access a folder outside the resources scope, an interesting practice would be to use Servlets, this will allow you to retrieve an image, or file, out of the Glassfish folder, you can still put some security in that access, creating access restrictions, follow the example below:

@WebServlet(urlPatterns={"/sistema/img-pessoa", "/sistema/cand/img-pessoa"}) //URI's onde seu Servlet vai atender as requests
public class ImgPessoaServlet extends HttpServlet
{
private String imagePath;

public void init() throws ServletException
{}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    File image = new File("");
    String contentType = null;

    String cam = request.getParameter("cam");        

    if(cam != null)
    {
        cam = cam.replace("\", File.separator);
        cam = cam.replace("/", File.separator);

        if (!cam.contains("pessoaf") && !cam.contains("pessoaj"))
        {
            File img = new File(File.separator + "opt" + File.separator + "arquivos" + File.separator + "fotos" + File.separator + "sem-foto.jpg");

            response.reset();
            response.setContentType("image");
            response.setHeader("Content-Length", String.valueOf(img.length()));

            Files.copy(img.toPath(), response.getOutputStream());
            return;
        }

        image = new File(Util.IMAGEM_PESSOA_PATH + cam);            
    }           

    //System.out.println(image.getAbsolutePath());

    if (!image.exists())
    {
        File img = new File(File.separator + "opt" + File.separator + "arquivos" + File.separator + "fotos" + File.separator + "sem-foto.jpg");
        response.reset();
        response.setContentType("image");
        response.setHeader("Content-Length", String.valueOf(img.length()));

        Files.copy(img.toPath(), response.getOutputStream());
        return;
    }

    contentType = getServletContext().getMimeType(image.getName());

    if ((contentType == null) || (!contentType.startsWith("image")))
    {
        response.sendError(404);
        return;
    }

    response.reset();
    response.setContentType(contentType);
    response.setHeader("Content-Length", String.valueOf(image.length()));

    Files.copy(image.toPath(), response.getOutputStream());
}

The call in your form.xhtml would look something like, assuming you were using the primefaces framework:

<p:graphicImage id="fotoAmpliada" url="img-pessoa?cam=#{pessoaFBean.fotoPessoa.caminho}"/>

This is a short implementation that I found on this site: servlet example

    
18.07.2018 / 21:54
0

Ivan, theoretically everything within WebContent will be available to access via URL, I do this with some simple images on my systems, like this:

/home/<Aplicação>/WebContent/resources

I always create a resources folder and place it divided into subfolders according to the purpose of the images or other files (psf, xls etc ..)

    
17.06.2015 / 20:00