File Download Component – Java

Problem Definition

Many a time we need to provide file download feature in our application, and it perfectly makes sense to create a component that can be reused.


This component is created for spring boot web application, although you can customize the code or get the idea for the reusable component.

The Component

FileDownloadHelper this is the main helper class that downloads the file or more technically writes file content on http response.

public class FileDownloadHelper {

    public static ResponseEntity downloadFile(FileResource file, WritableStream stream) {
        Assert.notNull("file and stream parameters are not optional.", file, stream);

        if (!file.exists())
            return ResponseEntity.notFound().build();

        byte[] content = file.getContent();

        if (!FileUtil.verifyChecksum(content, file.getChecksum())) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Verify checksum fail");


        return ResponseEntity.ok().build();


Supporting Implementaion

This allows to wrap underlying complexity of file resource and expose common access pattern.

public interface FileResource {

    String getPath();

    String getChecksum();

    String getContentType();

    boolean exists();

    byte[] getContent();

    InputStream getStream();


This allows to wrap writable resource (ex: servlet response) to hide complexity and expose common access pattern.

public interface WritableStream {

    void setContentType(String type);

    void write(byte[] content);

In Action

Sample code to download file from google cloud storage.


public final class GoogleFileResource implements FileResource {
    private Blob blob;
    private FileMeta meta;

    public GoogleFileResource(Blob blob, FileMeta meta) {
        this.blob = blob;
        this.meta = meta;

    public String getPath() {
        return blob.getName();

    public String getChecksum() {
        return meta.getChecksum();

    public String getContentType() {
        return meta.getType();

    public boolean exists() {
        return blob.exists();

    public byte[] getContent() {
        return blob.getContent();

    public InputStream getStream() {
        return null;



public class ServletWritableStream implements WritableStream {
    private HttpServletResponse response;

    public ServletWritableStream(HttpServletResponse response) {
        this.response = response;

    public void setContentType(String type) {

    public void write(byte[] content) {
        try {
        } catch (IOException e) {
            throw new RuntimeException(e);


Final Piece

public ResponseEntity getFile(@PathVariable String consultationReference,
            HttpServletResponse response) {
        return service.getFile(consultationReference)
                .map(file -> FileDownloadHelper.downloadFile(file, new ServletWritableStream(response)))

Sorting and Cross Join Problem – Spring Data JPA

Affected Version

Spring boot – 1.2.2
Spring data JPA version – 1.7.2
Spring data common – 1.9.2

Problem Statement

When you use custom query using @Query in your spring data JPA repository and pass sort parameter, where sort parameter is a child attribute; the query generated uses cross join instead of left outer join. This gives unexpected result.

Problem Sample

@Query("SELECT e FROM EventApprovalRequest e WHERE e.approvalRequestType = (:approvalRequestType) "
+ "AND e.status = (:status)")
Page getEventsForApproval(
@Param("approvalRequestType") int approvalRequestType, @Param("status") int status, Pageable pageable);


Pre-define left outer join in your query.

@Query("SELECT e FROM EventApprovalRequest e LEFT OUTER JOIN e.event LEFT OUTER JOIN e.event.director WHERE e.approvalRequestType = (:approvalRequestType) "
+ "AND e.status = (:status)")
Page getEventsForApproval(
@Param("approvalRequestType") int approvalRequestType, @Param("status") int status, Pageable pageable);