Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8983399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:49:56+00:00 2026-06-15T20:49:56+00:00

I have simple JAX-RS server application: @GET @Path(/getImage/{key}) @Produces(image/jpeg) public final BufferedImage getImageResource(@PathParam(key) String

  • 0

I have simple JAX-RS server application:

@GET
@Path("/getImage/{key}")
@Produces("image/jpeg")
public final BufferedImage getImageResource(@PathParam("key") String key) {
  final File file = new File(key); // the key will be "cat.jpeg" e.t.c.

  final BufferedImage image = ImageIO.read(new FileInputStream(file));

  return image;
}

After deploying war file to tomcat server (6.0), i do next:

http://localhost:8080/resource-service/getImage/cat.png

Problems:

1)On new File(key) it’ll go to tomcat root directory. I’d like to have something like default root folder for resources inside project directory around classes.

2)Returning BufferedImage doesn’t allowed as i understand, i get 500 error message:

HTTP Status 500 - Could not find MessageBodyWriter for response object of type: java.awt.image.BufferedImage of media type: image/jpeg

I’d appreciate any advice, links.
Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:49:57+00:00Added an answer on June 15, 2026 at 8:49 pm

    In order to get access to the files in your WAR package you should use ServletContext. First, inject it first into your class as a variable:

    @Context
    ServletContext context;
    

    and then find files using:

    final File file = new File(this.context.getRealPath(key));
    

    It’s not possible to return a BufferedImage (and map it automatically to image/jpeg), but in your example you don’t need to do it. Just return a JAX-RS response:

    @GET
    @Path("/getImage/{key}")
    @Produces("image/jpeg")
    public final Response get(@PathParam("key") String key) {
        return Response.ok()
            .entity(this.context.getResourceAsStream(key))
            .type("image/jpeg")
            .build();
    }
    

    Similar question: Dynamically create image from JAX-RS servlet

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have pretty simple JAX-RS WS: @Stateless @Path(/foo) public class FooFacadeBean { @GET @Produces(MediaType.TEXT_HTML)
i'm new in JAX-WS and i've created a test sample like: SERVER: @Path(/login) public
I have a very simple JAX-WS + JAXB SOAP service that I am, by
I have simple client/server SSL code which worked fine on Python 3.2. However, I
I'm trying to create and run simple example of JAX-RS using @Produces , @Consumes
I have a simple XML file <servers> <server> <id>Server0</id> </server> <server> <id>Server1</id> </server> <server>
I have simple console application in C++ written in Qt Creator 2.4.1 on Kubuntu
I have simple RoR application with devise. Here is the output ot rake routes
I have simple VCL Forms application which on start show on taskbar button if
I have simple html code that plays YouTube video after click on the image:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.