Trying to download a file from our system using Spring Boot and AngularJS 1. The file downloads successfully but when trying to open it, an error message appears: Error interpreting JPEG image file (Not a JPEG file: starts with 0xef 0xbf). The sample code used is as follows:
DownloadService.java
import com.codahale.metrics.annotation.Timed;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.io.*;
import java.net.URLConnection;
@Component
@RequestMapping("/api/1/download")
@Path("/api/1/download")
@Consumes(MediaType.APPLICATION_JSON)
@Slf4j
public class DownloadResource {
@ApiOperation(value = "downloads selected file", notes = "Returns a file", responseContainer = "FileSystemResource", response = HttpServletResponse.class)
@Path("/downloadFile")
@GET
@Timed
public void downloadFile(@Context HttpServletResponse response) throws IOException {
// Code for downloading file goes here...
}
}
download.js
// AngularJS controller for downloading the file
app.controller('storeReportsCtrl', function($rootScope, $scope, Restangular, $state, $stateParams, $location, $modal, $log, $timeout,$http,$sce) {
$scope.download = function(fileName) {
// Code for downloading file in AngularJS goes here...
}
});
After downloading the file, it seems to be corrupted as the original file size is 18,394 bytes but the downloaded file has a byte length of 33,496.
UPDATE
The same code works fine for text files but not for binary files. Any suggestions on how to correctly download binary files?