1. 스트럿츠의 경우
1.1 스트럿츠1.X 경우:
<스트럿츠1 구조>
아래의 간단한 예를 들어 설명
Input.jsp -> web.xml -> struts-config.xml -> ActionForm.java -> Action.java ->output.jsp
a. Struts1.X에게 폼데이터를 뺏기지 않기 위하여 InputForm.java(폼데이터를 저장할 수 있는 빈즈 역할의 소스)를 사용하지 않고, InputAction.java에서 로직을 수행한다.
b. 스트러츠1의 MultipartRequestHandler.java내용을 직접 수정하여 적용한다
1.2 스트럿츠2.X 경우:
별도로 작성된 필터를 사용하여 스트럿츠2에서 자체적으로 적용되는 필터의 동작을 제어합니다.
//////////////////////////////////////////////////////////////////////////////////
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>DEXTUploadJ Struts2 Sample</display-name>
<filter> <filter-name>struts2</filter-name> <!-- 스트럿츠 필터를 상속받아 새로 만들어진 필터 (새 필터 사용하도록 수정) --> <filter-class>com.devpia.dextuploadj.dispatcher.DEXTUploadJFilterDispatcher</filter-class> <!-- 기존 스트럿츠 필터 (기존 설정을 동작 안 하도록 코멘트 처리) --> <!-- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> --> </filter>
<filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
</web-app>
//////////////////////////////////////////////////////////////////////////////////
DEXTUploadJFilterDispatcher.java
package com.devpia.dextuploadj.dispatcher;
import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.dispatcher.FilterDispatcher;
public class DEXTUploadJFilterDispatcher extends FilterDispatcher { protected HttpServletRequest prepareDispatcherAndWrapRequest( HttpServletRequest request, HttpServletResponse response) throws ServletException { return request; } }
//////////////////////////////////////////////////////////////////////////////////
FileUploadAction.java
import com.opensymphony.xwork2.ActionSupport;
import devpia.dextupload.*;
public class FileUploadAction extends ActionSupport { public String upload() { try{ ServletContext ctx = ServletActionContext.getServletContext(); HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse();
FileUpload fileUpload = new FileUpload(request, response);
String strPath = ctx.getRealPath("/"); fileUpload.setLicenseFilePath(strPath + File.separator + "dextuploadj.config"); fileUpload.UploadStart(strPath);
FileItem item = fileUpload.getFileItem("upfile"); item.Save(strPath, true);
}catch(Exception e){ e.printStackTrace(); return ERROR; }
return SUCCESS; } }
//////////////////////////////////////////////////////////////////////////////////
FileDownloadAction.java
package com.devpia.dextuploadj.struts2;
import java.io.File;
import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport;
import devpia.dextupload.*;
public class FileDownloadAction extends ActionSupport { public String download() { try{ ServletContext ctx = ServletActionContext.getServletContext();; HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse();
String FilePath = request.getParameter("FilePath"); FileDownload down = new FileDownload(request, response); down.Download(FilePath, false, false);
}catch(Exception e){ e.printStackTrace(); return ERROR; }
return NONE; } }
//////////////////////////////////////////////////////////////////////////////////
Struts2 -> 기술포럼 '강좌&팁’참고: 스트럿츠의 업로드 API가 파일 데이터를 먼저 읽어 들여 발생
http://www.devpia.com/MAEUL/Contents/Detail.aspx?BoardID=4053&MAEULNO=518&no=55&page=1
|