2016-09-12 1 views
-1

csvファイルをダウンロードしようとしていますが、htmlコード(ヘッダーとフッター)もファイルに送信されています。私はweblogicを使用していますが、Tomcatはうまく動作します。コードは次のとおりです。HttpServletResponse getOutputStream csvファイルでhtmlコードを送信する

public void downloadCSVReport(String csvFile, HttpServletResponse response){ 
     try { 

      response.setHeader("Content-Disposition", "attachment; filename=example.csv"); 
      response.setContentType("text/csv"); 

      ByteArrayInputStream bais = new ByteArrayInputStream(csvFile.toString().getBytes(REPORT_ENCODING)); 
      IOUtils.copy(bais, response.getOutputStream()); 

      response.flushBuffer(); 

     } catch(IOException e){ 
      LOGGER.error(e.getMessage(), e); 
     } 
    } 

ありがとうございます!私の春のWebアプリケーションで

答えて

1

このコードは、ん。ContentLengthが働い設定

public static void putFileInResponse(HttpServletResponse response, File file, String contentType) throws IOException { 
    // Set the headers 
    response.setContentType(contentType);// "application/octet-stream" for example 
    response.setContentLength((int) file.length()); 
    response.addHeader("Content-Disposition", new StringBuilder("attachment; filename=\"").append(file.getName()).append("\"").toString()); 
    response.addHeader("Content-Description", "File Transfer"); 
    response.addHeader("Content-Transfer-Encoding", "binary"); 
    response.addHeader("Expires", "0"); 
    response.addHeader("Cache-Control", "no-cache, must-revalidate"); 
    response.addHeader("Pragma", "public"); 
    response.addHeader("Content-Transfer-Encoding", "binary"); 

    // Write the content of the file in the response 
    try (FileInputStream fis = new FileInputStream(file); OutputStream os = response.getOutputStream()) { 
     IOUtils.copyLarge(fis, os); 
     os.flush(); 
    } 
} 
1

を作品! ContentLengthのために、今やcsvFile.length()は余分なデータがなくなってしまいます。ありがとう!!

関連する問題