2017-08-17 7 views
0

私は春にxlsxファイルをサーバーからダウンロードしようとしています。ファイルはダウンロードされますが、ファイルが壊れているという警告が表示されます。ここでサーバーからExcel(xlsx)ファイルをダウンロードした後、ファイルが破損して開くことができないのはなぜですか?

は私のコードです:

File file = new File(outputPath+ fileName); 
FileInputStream fis = new FileInputStream(file); 
response.setContentType("application/vnd.openxmlformats- 
officedocument.spreadsheetml.sheet"); 
response.setContentLength((int) file.length());   
response.setHeader("Content-Disposition", "attachment; filename="+fileName); 
FileCopyUtils.copy(fis, response.getOutputStream()); 

JSPページ:

enter image description here

ヘルプは高く評価されています

$("#download").click(function(){ 
    if($(this).data('clicked', true)){ 
    window.location="http://localhost:8080/IRI-AXCO/downloadFile"; 
    } 
} 

ここでは、スナップショットです!おかげ

+0

フルコントローラの方法を表示します。 –

+0

こんにちは私は解決策を得ました、私はCSV形式にコンテンツタイプを変更しました '、あなたの返信のおかげで –

答えて

0
自分が解決策を見つけた

@GetMapping("/downloadFile") 
    public void download(HttpServletRequest request, HttpServletResponse response)throws Exception{ 
     try { 
      fileName=fileName.replace(" ", "_"); 
      File file = new File(outputPath+ fileName); 
      FileInputStream fis = new FileInputStream(file); 
      Path source = Paths.get(outputPath+ fileName); 
      String contentType=Files.probeContentType(source); 
      response.setContentType(contentType); 
      response.setContentLength((int) file.length()); 
      response.setHeader("Content-Disposition", "attachment; filename="+fileName); 
      FileCopyUtils.copy(fis, response.getOutputStream()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      throw e; 
     } 
    } 

上記のコードは、私に結果を与えました!

関連する問題