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 9239495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:59:59+00:00 2026-06-18T07:59:59+00:00

I try to upload/stream a large image to a REST controller that takes the

  • 0

I try to upload/stream a large image to a REST controller that takes the file and stores it in to a database.

@Controller
@RequestMapping("/api/member/picture")
public class MemberPictureResourceController {

  @RequestMapping(value = "", method = RequestMethod.POST)
  @ResponseStatus(HttpStatus.NO_CONTENT)
  public void addMemberPictureResource(@RequestBody InputStream image) {
    // Process and Store image in database
  }
}

This is a non-working example of what I’m trying to achieve (of course, or I guess so InputStream is not working). I want to stream/read the image over the @RequestBody.

I have searched everywhere but can’t find a good example how to achieve this. Most people seem to ask only how to upload images over forms but don’t use REST/RestTemplate to do it. Is there anyone that can help me with this?

I’m thankful for any hint in to the right direction.

Kind regards,
Chris

Solutions

Below here I try to post the solutions that worked for me after the Input from Dirk and Gigadot. At the moment I think both solutions are worth while looking at. At first I try to post a working example with the help I got from Dirk and then I’ll try to create one with the help from Gigadot. I will mark Dirks answer as the correct one as I have been asking explicitly how to upload the file over the @RequestBody. But I’m also curious to test the solution from Gigadot as it is maybe easier and more common to use.

In the below examples I store the files in MongoDB GridFS.

Solution 1 – Example after Dirks recommendation

Controller (with curl command for testing in the comment):

/**
*
* @author charms
* curl -v -H "Content-Type:image/jpeg" -X PUT --data-binary @star.jpg http://localhost:8080/api/cardprovider/logo/12345
*/
@Controller
@RequestMapping("/api/cardprovider/logo/{cardprovider_id}")
public class CardproviderLogoResourceController {

  @Resource(name = "cardproviderLogoService")
  private CardproviderLogoService cardproviderLogoService;

  @RequestMapping(value = "", method = RequestMethod.PUT)
  @ResponseStatus(HttpStatus.NO_CONTENT)
  public void addCardproviderLogo(@PathVariable("cardprovider_id") String cardprovider_id,
      HttpEntity<byte[]> requestEntity) {
    byte[] payload = requestEntity.getBody();
    InputStream logo = new ByteArrayInputStream(payload);
    HttpHeaders headers = requestEntity.getHeaders();

    BasicDBObject metadata = new BasicDBObject();
    metadata.put("cardproviderId", cardprovider_id);
    metadata.put("contentType", headers.getContentType().toString());
    metadata.put("dirShortcut", "cardproviderLogo");
    metadata.put("filePath", "/resources/images/cardproviders/logos/"); 

    cardproviderLogoService.create1(logo, metadata);
  }
}

Service (unfinished but working as a test):

@Service
public class CardproviderLogoService {

  @Autowired
  GridFsOperations gridOperation;

  public Boolean create1(InputStream content, BasicDBObject metadata) {
    Boolean save_state = false;
    try {
      gridOperation.store(content, "demo.jpg", metadata);
      save_state = true;
    } catch (Exception ex) {
      Logger.getLogger(CardproviderLogoService.class.getName())
          .log(Level.SEVERE, "Storage of Logo failed!", ex);
    }
    return save_state;    
  }
}

Solution 2 – Example after Gigadots recommendation

This is described in the Spring manual:
http://static.springsource.org/spring/docs/3.2.1.RELEASE/spring-framework-reference/html/mvc.html#mvc-multipart

This is quite easy and also contains all information by default. I think I’ll go for this solution at least for the binary uploads.

Thanks everyone for posting and for your answers. It’s much appreciated.

  • 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-18T08:00:00+00:00Added an answer on June 18, 2026 at 8:00 am

    As it looks as if you are using spring you could use HttpEntity ( http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/http/HttpEntity.html ).

    Using it, you get something like this (look at the ‘payload’ thing):

    @Controller
    public class ImageServerEndpoint extends AbstractEndpoint {
    
    @Autowired private ImageMetadataFactory metaDataFactory;
    @Autowired private FileService fileService;
    
    @RequestMapping(value="/product/{spn}/image", method=RequestMethod.PUT) 
    public ModelAndView handleImageUpload(
            @PathVariable("spn") String spn,
            HttpEntity<byte[]> requestEntity, 
            HttpServletResponse response) throws IOException {
        byte[] payload = requestEntity.getBody();
        HttpHeaders headers = requestEntity.getHeaders();
    
        try {
            ProductImageMetadata metaData = metaDataFactory.newSpnInstance(spn, headers);
            fileService.store(metaData, payload);
            response.setStatus(HttpStatus.NO_CONTENT.value());
            return null;
        } catch (IOException ex) {
            return internalServerError(response);
        } catch (IllegalArgumentException ex) {
            return badRequest(response, "Content-Type missing or unknown.");
        }
    }
    

    We’re using PUT here because it’s a RESTfull “put an image to a product”. ‘spn’ is the products number, the imagename is created by fileService.store(). Of course you could also POST the image to create the image resource.

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

Sidebar

Related Questions

I am using code below to upload large file to server and noticed that
I try to upload file into dropbox. I use dropbox api https://www.dropbox.com/developers/reference/api#files-POST procedure TDropbox.Upload2;
I get this error when i try to upload an image: OSError at /upload/
I try this command to upload a file (standard.xml) into table book the file
im trying to do a file upload using http post but when I try
I have the following script below where I try to mimic a file upload
I am trying to use Amazon S3 SDK to upload a large file in
I am trying to check if a file is an image before I upload
I have a aspx page that alows a user to upload an image for
I have the following code to upload a pdf file through ftp : try

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.