2017-05-29 69 views
0

我必須嘗試下載文件,這些文件位於我們的system.FOr中,這是我使用的彈簧引導和角度js 1. 文件已下載但未打開消息解釋JPEG圖像文件時出錯(不是JPEG文件:以0xef 0xbf開頭)。 我的示例代碼 -下載的文件已損壞 - 解釋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.byteLength = 33496.

UPDATE

相同的代碼工作正常的文本文件,但二進制文件不起作用。

如何下載正確的文件..

+0

後更新.... –

回答

0

字符串文件路徑=( 「/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"); 
+0

不行!!!還是同樣的問題... –

+0

確保CTA.jpg文件存在 –

+0

\ home \ ashish \ Desktop \ CTA.jpg –