0

I am using RESTful endpoints for some file operations. I would want to post a jar file to my service via REST , I tried below approach but still it fails, I almost tried googling but could not find any solution.

@RestController
public class MyController {
 ...

@RequestMapping(value="/jobs/upload", method=RequestMethod.POST)
public @ResponseBody ResponseEntity<Void> handleFileUpload(HttpEntity<byte[]> requestEntity){
    byte[] payload = requestEntity.getBody();
    InputStream logo = new ByteArrayInputStream(payload);
    HttpHeaders headers = requestEntity.getHeaders();
    return ResponseEntity.ok().build();
 }
...
}

Curl Command : curl -X POST --data-binary @/Users/path/to-jar/test-jar.jar localhost:8008/ctx/jobs/upload

[EDIT] : If I have to achive via --data-binary , how should my code look like?

I am not able to proceed further could anyone please help. I saw lot of solutions on MultiPart but I am not able to fit it in.

chaosguru
  • 1,933
  • 4
  • 30
  • 44
  • Here you have a good tutorial about this issue: http://www.mkyong.com/spring-mvc/spring-mvc-file-upload-example/ – Eduardo Yáñez Parareda Apr 20 '15 at 07:31
  • @eyp : I was looking more into integration with RESTController via Spring. Multipart I have to get into Spring MVC which is not needed for me. – chaosguru Apr 20 '15 at 07:36
  • Your controller is already using Spring MVC (although you may not experience it like that it is using the same components and infrastructure). – M. Deinum Apr 20 '15 at 07:43
  • @M.Deinum : Yeah but my only need is I would want to post as curl with --data-binary. how do i do it? Multipart takes RequestParam which is my last resort actually. – chaosguru Apr 20 '15 at 07:48

2 Answers2

1

You need to configure a servlet for MUltipart:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="200000"/>
</bean>

Then in your REST service

@RequestMapping(value="/jobs/upload", method=RequestMethod.POST)
@ResponseBody
public ResponseEntity<Void> handleFileUpload(@RequestParam("file") MultipartFile multipartFile,
                                             HttpServletRequest request) {
    ...
}
Eduardo Yáñez Parareda
  • 9,126
  • 4
  • 37
  • 50
0

try below solution

@RequestMapping(value = APIURIConstants.GET_JAR, method = RequestMethod.GET)
    public ResponseEntity<byte[]> getJar(

            HttpServletRequest request) {
        ServletContext context = request.getServletContext();
        String fullPath = objPollBookService.getSignatureFileName(electionID);
        System.out.println("Path=" + fullPath);

        byte[] content = null;
        try {
            URL link = new URL(fullPath);
            InputStream in = new BufferedInputStream(link.openStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int n = 0;
            while (-1 != (n = in.read(buf))) {
                out.write(buf, 0, n);
            }
            out.close();
            in.close();
            content = out.toByteArray();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpHeaders headers = new HttpHeaders();
        String mimeType = context.getMimeType(fullPath);
        if (mimeType == null) {
            mimeType = "application/octet-stream";
        }
        headers.setContentType(MediaType.parseMediaType(mimeType));
        String filename = FilenameUtils.getBaseName(fullPath);
        headers.setContentDispositionFormData(filename, filename);
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
        ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(content,
                headers, HttpStatus.OK);
        return response;
    }
Shyam
  • 6,376
  • 1
  • 24
  • 38