JSF를 사용한 파일 다운로드
h:commandLink actionListener="#{productBean.downloadAction}" styleClass="highLightLink">
<h:outputText value="download"/>
<f:param name="productId" value="#{productBean.id}"/>
</h:commandLink>
Backing bean 디자인 및 코드: 이것은 Action listener 방법입니다. 되돌아오는 값이 없고 javax가 있습니다.faces.event.ActionEvent 매개 변수
public void downloadAction(ActionEvent event) {
try {
String fileName="D://temp//images//products//" + this.id + ".xls";
logger.debug("file name=" + fileName);
ByteArrayOutputStream baos=this.serviceLocator.getFileService().downloadFile(fileName); // Service , ByteArrayOutputStream
HttpServletResponse response=FacesUtils.getServletResponse();
response.setHeader("Content-disposition", "attachment; filename=" + id+ ".xls" ); // (inline),
response.setContentLength(baos.size());
ServletOutputStream sos=response.getOutputStream();
baos.writeTo(sos);
baos.close();
sos.flush();
} catch (IOException ex) {
logger.debug(ex);
}
}
서비스 코드: 이 구현은 이미 존재하는 물리 파일에서 출력 흐름을 얻는 예이다. 서버가 메모리에서 출력 흐름을 생성하는 것도 마찬가지로 처리한다. 예를 들어 Excel 파일을 생성하고 사용자가 다운로드하도록 하는 것이다.
public ByteArrayOutputStream downloadFile(String fileName) throws IOException {
FileInputStream fis=new FileInputStream(fileName);
BufferedInputStream bis=new BufferedInputStream(fis);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
BufferedOutputStream bos=new BufferedOutputStream(baos);
int i;
while((i=bis.read())!=-1) {
bos.write(i);
}
bos.flush();// ,
bis.close();
return baos;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.