struts2] Download
Download
1. web.xml
- 필터등록(위에서 설정한 부분과 동일)
2. struts.properties
- struts환경설정 파일 (위에서 설정한 부분과 동일)
3. struts.xml
- result type에 결과를 stream으로 받기 때문에 stream으로 설정
- param 요소에 mimeType,원 자원의 길이, 컨텐츠헤더값, inputStream이름, 버퍼크기 설정
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC '-//Apache Software Foundation//DTD Struts Configuration 2.0//EN'
'http://struts.apache.org/dtds/struts-2.0.dtd'>
<struts>
<package name="defalut" extends="struts-default" namespace="">
<action name="fileList" class="ex1.FileListAction">
<result>/ex1/fileList.jsp</result>
</action>
<action name="fileDownload" class="ex1.FileDownLoadAction">
<result type="stream">
<param name="contentType">bynary/octet-stream</param> // mimeType
<param name="contentLength">${contentLength}</param> // 원자원의 길이
<param name="contentDisposition">${contentDisposition}</param>
<!-- 파일의 이름을 설정하기 위한 컨텐츠헤더값 지정-->
<param name="inputName">input</param> // inputStream 이름
<param name="bufferSize">4096</param> // 버퍼 크기
</result>
</action>
</package>
</struts>
4. fileList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<h2>파일 목록</h2>
<s:property value="basePath"/>
<s:iterator value="fileList" status="stat">
/*
FileListAction 클래스로부터 받은 basePath를 param에 등록하고, fileName에 파일명을 등록
url에 해당 param을 등록하고 href요소에 url id를 맵핑시켜 사용자가 파일클릭시
fileDownload Action클래스를 수행하게하여 파일 다운로드가 수행되게 된다.
*/
<s:url id="download" action="fileDownload">
<s:param name="basePath" value="basePath"/>
<s:param name="fileName" >
<s:property value="fileList[#stat.index].name"/>
</s:param>
</s:url>
<li><s:a href="%{download}">
<s:property value="fileList[#stat.index].name"/>
</s:a>
</li>
</s:iterator>
5. FileListAction.java
- ActionSupport를 상속받는 action 클래스 등록
- properties파일에서 읽어올시 액션 파일명과 동일하게 작성하여 동일위치에 생성
package ex1;
import java.io.File;
import java.util.ArrayList;
import com.opensymphony.xwork2.ActionSupport;
public class FileListAction extends ActionSupport {
private ArrayList<File> fileList = new ArrayList<File>();
private String basePath;
// 필드에 선언된 setter/getter 메서드 정의
@Override
public String execute() throws Exception {
basePath = getText("path"); //프로퍼티파일에서 읽혀짐
//basePath = "D:/jspwork/struts2_FileUpload/WebContent/fileUpload";
/*
파일들이 저장되어 있는 폴더를 File객체로 생성한다.
이유는 그렇게 해야 그안에 있는 파일들을 가져와서 ArrayList에 저장할 수 있다.
내부의 모든 요소들을 반복하여 파일인지 아닌지를 비교하고 비교후 파일일경우 list에 추가
*/
File dir = new File(basePath);
File[] files = dir.listFiles();
if(files!=null && files.length>0)
for(File f : files)
if(f.isFile())
fileList.add(f);
return SUCCESS;
}
}
6. FileDownLoadAction.java
- 사용자가 파일명클릭시 수행되는 ActionClass(다운로드가 수행되는 ActionClass)
- struts.xml에 등록한것처럼 결과를 steam으로 전달하여 파일다운로드가 이루어짐
package ex1;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import com.opensymphony.xwork2.ActionSupport;
public class FileDownLoadAction extends ActionSupport{
private String basePath,fileName,contentType,contentDisposition;
private InputStream input;
private long contentLength;
// 필드에 선언된 setter/getter 메서드 정의
@Override
public String execute() throws Exception {
/* jsp페이지에서 taglib에 설정된 param name(value값)이 전달됨 */
String path = basePath + System.getProperty("file.separator") + fileName;
File f = new File(path);
setContentLength(f.length());
setContentDisposition("attachment; filename=" + URLEncoder.encode(fileName, "utf-8"));
setInput(new FileInputStream(path));
return SUCCESS;
}
}