1

I am new to Servlets and Spring Framework. I try to get media files from directory through Rest Service.

For videos/mp4 I couldn't find anything.

For audio I did this: Writing mp3 file to response output stream

For images I did this:

@RequestMapping("/tmp/{uuid}")
@ResponseBody
public ResponseEntity<InputStreamResource> getTmp(@PathVariable("uuid") String uuid)
        throws IOException {

    Path path = Paths.get("/media/psmaster/HDD/TUC-IPS/" + uuid);
    String contentType = Files.probeContentType(path);
    FileSystemResource file = new FileSystemResource("/media/psmaster/HDD/TUC-IPS/" + uuid);
    return ResponseEntity
            .ok()
            .contentLength(file.contentLength())
            .contentType(
                    MediaType.parseMediaType(contentType))
            .body(new InputStreamResource(file.getInputStream()));
}

Can someone please help to figure out the problem?

pik4
  • 1,283
  • 3
  • 21
  • 56
  • I would like to get anyone type of file with one Rest Service if it is possible. – pik4 Sep 27 '15 at 20:49
  • *"the problem"*? You didn't mention any. – kryger Sep 27 '15 at 20:56
  • org.apache.catalina.connector.ClientAbortException: java.io.IOException: Broken pipe at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:393) at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:426) at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:342) at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:295) at org.apache.catalina.connector.CoyoteOutputStream.close(CoyoteOutputStream.java:... – pik4 Sep 27 '15 at 21:16
  • I don't know exactly the problem. – pik4 Sep 27 '15 at 21:20
  • Include all relevant information directly in the question (use the edit button), not in the comments. `ClientAbortException` suggests the consumer of the data is the culprit; there are numerous questions about this particular exception, use the search bar on the top. – kryger Sep 28 '15 at 09:41

3 Answers3

1

If you are using Spring 4.2 you can use StreamingResponseBody, Have a look at this post

shazin
  • 21,379
  • 3
  • 54
  • 71
0

You can also give Spring Content a look. It allows you to build content services very quickly and easily using similar programming techniques to Spring Data. You can also pair it with Spring Data to additionally store and search metadata for your videos. By defining a single interface and including the appropriate Spring Content dependency in your project you can create a set of REST endpoints that allow you to manage the full lifecycle of a video including streaming.

Paul Warren
  • 2,411
  • 1
  • 15
  • 22
0

You can write media using streams and HttpServletResponse:

@RequestMapping(value = "/image/{imgName}", method = RequestMethod.GET)
public void getImageAsByteArray(@PathVariable String imgName , HttpServletResponse response) throws IOException {
    InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/" + imgName);
    response.setContentType(MediaType.IMAGE_JPEG_VALUE);
    IOUtils.copy(in, response.getOutputStream());
}

The example above serves an image file.

Hope this helps

Martín Zaragoza
  • 1,717
  • 8
  • 19