Javascript 下载的文件已损坏-解释JPEG图像文件时出错(不是JPEG文件:以0xef 0xbf开头)

Javascript 下载的文件已损坏-解释JPEG图像文件时出错(不是JPEG文件:以0xef 0xbf开头),javascript,angularjs,rest,spring-boot,download,Javascript,Angularjs,Rest,Spring Boot,Download,我必须尝试下载我们系统中的文件。为此,我使用了spring boot和angular js 1。 文件已下载,但未打开,并显示消息解释JPEG图像文件时出错(不是JPEG文件:以0xef 0xbf开头)。 我的示例代码是- DownloadService.java import com.codahale.metrics.annotation.Timed; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4

我必须尝试下载我们系统中的文件。为此,我使用了spring boot和angular js 1。 文件已下载,但未打开,并显示消息解释JPEG图像文件时出错(不是JPEG文件:以0xef 0xbf开头)。 我的示例代码是-

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 {
  String filePath = ("/home/ashish/Desktop/CTA.jpg");
  File fileName = new File(filePath);
  String mimeType = URLConnection.guessContentTypeFromName(fileName.getName());
  if (mimeType == null) {
   System.out.println("mimetype is not detectable, will take default");
   mimeType = "application/octet-stream";
  }
  response.setContentType(mimeType);
  response.setHeader("Content-Disposition", String.format("inline; filename=\"" + fileName.getName() + "\""));
  response.setContentLength((int) fileName.length());
  InputStream inputStream = new BufferedInputStream(new FileInputStream(fileName));
  FileCopyUtils.copy(inputStream, response.getOutputStream());
  inputStream.close();

 }
}



download.js


app.controller('storeReportsCtrl', function($rootScope, $scope, Restangular, $state, $stateParams, $location, $modal, $log, $timeout,$http,$sce) {
$scope.download = function(fileName) {

$http.get('/api/1/download/downloadFile', {responseType:'arraybuffer'})
             .success(function (response) {
               var a = document.createElement("a");
                   document.body.appendChild(a);
                   a.style = "display: none";
                   var fileName = "ppt2.jpg";
                  // var mimeType = data.mimeType;
                    var blob = new Blob([response], {type: 'image/jpeg'}),
                     url = window.URL.createObjectURL(blob);
                       a.href = url;
                       a.download = fileName;
                       a.click();
                       window.URL.revokeObjectURL(url);
             }).catch(function(error) {
                  console.log(error);
                 });
           }
});
此文件已下载,但已损坏意味着我的原始文件大小为18394字节,response.bytellength=33496

更新

同样的代码对于文本文件也可以,但是对于二进制文件就不行了

如何下载正确的文件..

字符串文件路径=(“/home/ashish/Desktop/CTA.jpg”); 我认为文件路径是个问题,请确保该文件存在

 @GetMapping("/downloadFile")
public void doima(HttpServletResponse response) throws IOException {
    String filePath = ("\\home\\ashish\\Desktop\\CTA.jpg");
    File fileName = new File(filePath);     
    String mimeType = URLConnection.guessContentTypeFromName(fileName.getName());
    System.out.println(mimeType);
    if (mimeType == null) {
        System.out.println("mimetype is not detectable, will take default");
        mimeType = "application/octet-stream";
    }
    response.setContentType(mimeType);
    response.setHeader("Content-Disposition", String.format("inline; filename=\"" + fileName.getName() + "\""));
    response.setContentLength((int) fileName.length());
    InputStream inputStream;
    try {
        inputStream = new BufferedInputStream(new FileInputStream(fileName));
        FileCopyUtils.copy(inputStream, response.getOutputStream());
        inputStream.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
CTA.jpg存在于您的资源路径中,然后您可以尝试 如下

 InputStream in = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("CTA.png");

文件位于ubuntu机器上,所以我认为路径类似于“\\home\\ashish\\Desktop\\CTA.jpg”路径上的/home/ashish/Desktop/CTA.jpg,它给出的文件不存在后期更新。。。。