Results 1 to 4 of 4

Thread: Loading image in MVC class

  1. #1
    Join Date
    Mar 2011
    Posts
    9

    Default Loading image in MVC class

    Hello,

    I have Spring MVC application and I'm creating PDF view which I did successfully.

    Now I want to add picture to my PDF view and from one of my class I tried

    Code:
      Image image=Image.getInstance("/resources/img/logo.png");
    but I'm getting FileNotFoundException - C:\resources\img\logo.png (The system cannot find the path specified)

    Can someone please tell me where should I put my image and how to load it from my class.

  2. #2
    Join Date
    Aug 2009
    Location
    Wesley Chapel, FL
    Posts
    35

    Default Loading image in MVC class

    Are you using iText to create your PDF?

    Your code is trying to access the file system where the image does not exist (C:\resources\img\logo.png). If your image existed in the aforementioned location, it should work. You can also utilize a URL to reference an image.

    Code:
    String imageUrl = "http://website.com/myImage.jpg";
    
    Image image = Image.getInstance(new URL(imageUrl));
    document.add(image);

  3. #3
    Join Date
    Mar 2011
    Posts
    9

    Default

    Yes, I'm using iText for PDF and that part is fine.

    My image is in folder webapp/resources/img/logo.png of my MVC application.

    I could use absolute url like you suggested but I want to use relative url so when I change server I don't need to change this url.

    Is there a way to do this?

  4. #4
    Join Date
    Mar 2011
    Posts
    9

    Default Solved

    I solved the problem
    In case anyone needs this, create package for example com.company.images and put your images there.
    Then get your image with
    Code:
    URL imageUrl = getClass().getResource("com/company/images/logo.jpg");
    Image image = Image.getInstance(imageUrl);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •