title: How do i upload/stream large images using Spring 3.2 spring-mvc in a restful way
body: <p>I try to upload/stream a large image to a REST controller that takes the file and stores it in to a database.</p>
  
  <pre><code>@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
    }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>I'm thankful for any hint in to the right direction.</p>
  
  <p>Kind regards,
  Chris</p>
  
  <p><strong>Solutions</strong></p>
  
  <p>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.</p>
  
  <p>In the below examples I store the files in MongoDB GridFS.</p>
  
  <p><strong>Solution 1 - Example after Dirks recommendation</strong></p>
  
  <p>Controller (with curl command for testing in the comment):</p>
  
  <pre><code>/**
  *
  * @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&lt;byte[]&gt; 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);
    }
  }
  </code></pre>
  
  <p>Service (unfinished but working as a test):</p>
  
  <pre><code>@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;    
    }
  }
  </code></pre>
  
  <p><strong>Solution 2 - Example after Gigadots recommendation</strong></p>
  
  <p>This is described in the Spring manual:
  <a href="http://static.springsource.org/spring/docs/3.2.1.RELEASE/spring-framework-reference/html/mvc.html#mvc-multipart" rel="noreferrer">http://static.springsource.org/spring/docs/3.2.1.RELEASE/spring-framework-reference/html/mvc.html#mvc-multipart</a></p>
  
  <p>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.</p>
  
  <p>Thanks everyone for posting and for your answers. It's much appreciated.</p>