파일 다운로드에 대해서도 알아보자.


struts.xml

<action name="fileDownload" class="jh.board.action.FileDownloadAction">

<result type="stream">

<param name="contentType">binary/octet-stream</param>

<param name="contentLength">${contentLength}</param>

<param name="contentDisposition">${contentDisposition}</param>

<param name="inputName">inputStream</param>

<param name="bufferSize">4096</param>

</result>

</action>


download.java

private String basePath;

private String fileName;

private String contentType;

private String contentDisposition;

private InputStream inputStream;

private long contentLength;

getter/setter 함수 선언;


public String execute() throws Exception {

String inputPath = basePath + "/" + fileName; // 파일을 저장해놓은 경로와 이름

File f = new File(inputPath); // 파일 경로+이름 을 이용하여 파일 생성

setContentLength( f.length() );

setContentDisposition("attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

setInputStream(new FileInputStream(inputPath));

return SUCCESS;

}


+ Recent posts