Doubt with Vraptor Download Images

Asked

Viewed 251 times

2

I have a question about the function of Download Vraptor, I have an IMG on my server and I try to display it in my Mobile Application but when I request the served it does not return me an IMG type response (type = "image/jpg" is not an error in the request when I call the Werb service returns me status 200.

If anyone has any idea could help me.

 @Get
public Download foto(String foto) throws FileNotFoundException {
    File file = new File("C:\\Users\\Gabriel\\Portifolio\\src\\main\\webapp\\WEB-INF\\upload\\"+foto);
    String contentType = "image/jpg";
    String filename = foto;

    return new FileDownload(file, contentType, filename);

}

Code in my Image Download function.

1 answer

0


Good afternoon Darlan Oliveira, I’m new here, but I think I can help you. In my project I made a Controller to manage my images and this is a code I made.

@Open
@Get
@Path("/formaPagamento")  
public Download imagemFormaPagamento(String id) {  
    File file = new File(NomenclaturaArquivo.getImagemFormaPagamento() + id);

    String contentType = "image/png";
    String filename = id + ".png";

    try {
        return new FileDownload(file, contentType, filename);
    } catch (FileNotFoundException e) {
        System.out.println("erro: " + e);
        return null;
    }
}

And I call her on my JSP like this:

<img src="<c:url value='/imagem/formaPagamento?id=${f.id }'/>">

and in my controller I use an annotation to define as image.

@Controller
@Path("/imagem")
public class ImagemController {

The @Open annotation is one I created to let the code be accessed without having to be logged in to the system, so disregard.

I don’t really know if it helps you, but maybe you’re doing something different. These are parts of my code working, step by step. There in the Filename, it is a class that has constants that take my folders from the server. So I find it easier to work and to make future modifications of paths, instead of failing the source code. Of course this goes from programmer to programmer.

I also had enough problems to find my images in the folder.... Usually the JSP call was correct and only my file was not found as it was using linux. I did in the case a main simple class, trying to find this file and returning one (Filenotfoundexception) until finding the correct way to find my image. Oh yes I passed the routine to JSP. So I think it’s easier to test without running on the server.

I don’t know if I helped you, but here’s my contribution.

Browser other questions tagged

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