user2582318
user2582318

Reputation: 1647

Show local disk images in the JSP (images out of web context)

i did the servlet to make the images path accessible in the .JSP page

public void nova() {
        System.out.println("Nova Certidão");
        certidoes = new File("C:\\imagens\\").listFiles();
        List<String> paths= new ArrayList<String>();
        for (File arquivo : certidoes) {
        paths.add(arquivo.getPath());
        }
        result.include("files", caminhos);
    }

now in the page i have access to the files path in the disk in the arraylist files

in the jsp i have this to display the image:

<c:forEach items="${files}" var="files">
${file} - 
<img src="/getImage/${file}" height="42" width="42"> 
<br>
</c:forEach>

and my getImage is this:

@Get("/getImage/{path}")
public void getImage(String path) {
    //here will come all the way to send the stream of the image if i receive the path
    System.out.println(path);

}

but its not working because it create a strange request image path:

it create:

/getImage/C:/imagens/image1.jpg

so its not working, if i manually type: /getImage/image1, i correctly display the system out, i think its not working for the file paths because the / (i think its obvious) but i have no idea how to solve this

Upvotes: 0

Views: 1514

Answers (1)

LHA
LHA

Reputation: 9655

You need to add your images into a sub directory of Web application root.

I assume that your application root is {web_apps}/yourapp. Under this directory, you will have a few sub-directorys such as WEB-INF, styles, images.

Put all your images into /yourapp/images/ (this folder is accessible from public).

In JSP

<img src="${pageContext.request.contextPath}/images/${file}" height="42" width="42" /> 

UPDATED

Since images are in a fixed folder that not in Web application root so we can't use the solution described above.

Here is the right solution:

  1. Convert path of image ( absolute paht/relative path) to VALID URL variable using DES/AES encryption - The output can be Base16, Base64 encoding.

  2. Convert based16, Base64 encoding path back to Path of the image in public void getImage function

    @Get("/getImage/{path}")     
    public void getImage(String path) {
    
        // path is in based16/Base64 encoding
    
        String pathOfImage = functionToConvertEncodedPathToRealPath ( path)
    
    }
    

Upvotes: 1

Related Questions