Add folder with images on the glassfish web server

Asked

Viewed 576 times

2

Good afternoon, folks how do I leave a folder on my glassfish server with some pictures? Oh when I need to direct access that url..

Someone’s already done it?

2 answers

2

if you want to access a folder outside of the scope Resources, an interesting practice would be the use of Servlets, this will allow you to rescue an image, or file, outside of the Glassfish folder, you can still put some security in this access, creating access restrictions, follows 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.xhtml form would look something like, assuming you were using the primeface framework:

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

This is a summary implementation I found on this site: example of Servlet

0

Ivan, theoretically everything that is inside Webcontent will be available to access via URL, I do this with some simple images on my systems, this way:

/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..)

  • Got it, cool.. more in glassish for example the server I’m using, I have to point to this path right? how do I do this?

  • In my case, I have an application running, which mounts the folder when it is active (this is automatic in glassfish), then the URL is http://server:8080/aplicacao/nome-da-pasta. It can be a simple JSF application with nothing just with the folders you want there. (this is the easiest way). You can also try to set something up directly in glasssfish (config), just like we do virtualHosts in apache, but I wouldn’t know how to give you details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.