2017-10-31 5 views
2

RESTEasy 기반 웹 서비스에서 Apache XSSF를 사용하여 생성 된 xlsx 파일을 다운로드하려고합니다.웹 서비스에서 xlsx를 다운로드 할 수 없습니다.

웹 서비스 컨트롤러 :

아래 enter image description here

소스 코드입니다 :

열려면 클릭 할 때 두 번 내가 파일을 다운로드 할 수 있지만,

,이 파일이 openend 할 수 없다고

@GET 
@Path(/download) 
@Produces(MediaType.APPLICATION_OCTET_STREAM) 
public Response downloadFile() { 

    ByteArrayOutputStream baos = myService.processGbiValidationExceptions(); 

    ResponseBuilder response = Response.ok((Object) baos.toByteArray()); 
    response.header("Content-Disposition", "attachment;filename=My_File.xlsx"); 
    return response.build(); 
} 

서비스 :

public ByteArrayOutputStream processGbiValidationExceptions() { 
    XSSFWorkbook workbook = new XSSFWorkbook(); 

    // code to write to workbook 


    // Create stream 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    try { 
     workbook.write(baos); 
    } catch (IOException e) { 
     LOGGER.error("Error occurred while writing workbook to stream", e); 
     throw new IptException(e); 
    } 

    return baos; 
} 

내가 여기 잘못보고있는 단서가 있습니까? 감사합니다.

피씨 : 맥에요!

답변

1

이전에 파일을 다운로드 할 때 사용한 클라이언트 측 코드에 문제가 거의 없었습니다. 자, download.js을 사용하고 있습니다.

import download from 'downloadjs';  
    . 
    . 
    . 
    fetch(FETCH_URL, { 
     method: 'GET', 
     dataType: 'json', 
     credentials: 'include' 
    }).then(response => { 
     if(response.status === 200) { 
      return response.blob(); 
     } 
     },() => { 
     // when error do something 
     } 
    ).then(
     // Following line helps download 
     blob => download(blob, "someexcel.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") 
    ) 

또한, MIME 타입이 xlsx 파일을 다운로드하기 위해 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet로 설정해야합니다 : 아래

코드입니다

@Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") 

건배를

관련 문제