블로그 이미지
헤이장

Notice

Recent Post

Recent Comment

Recent Trackback

calendar

1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
  • total
  • today
  • yesterday
2009. 7. 27. 12:58 Web/Framework

 struts.xml
   <action name="fileUpload" class="action.UploadAction">
   <interceptor-ref name="prepare" />
   <interceptor-ref name="modelDriven" />
   <interceptor-ref name="fileUpload" />
   <interceptor-ref name="params" />
   <interceptor-ref name="workflow" />
   <result>list.jsp</result>
  </action>

 struts.properties
// 멀티파트 파서. cos, pell, jakarta를 지정할 수 있다
struts.multipart.parser = jakarta
// 임시 저장 디렉토리. 디폴트로 javax.servlet.context.tempdir를 사용
struts.multipart.saveDir = /tmp
// 업로드 최대크기
struts.multipart.maxSize = 100000000

 Form.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
</head>
<body>
<form action="fileUpload.action" method="post" enctype="multipart/form-data">
이름 : <input type="text" name="name"/><br/>
나이 : <input type="text" name="age"/><br/>
이메일 : <input type="text" name="email"/><br/>
사진 : <input type="file" name="doc"/><br/>
<input type="submit" value="send"/>
</form>

</body>
</html>


 UploadAction.java

package action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("serial")
public class FileUploadAction extends ActionSupport implements Preparable, ModelDriven<User>{
 private final String UPLOAD_DIR = "e:/upload/"; // 저장할 디렉토리
// doc 파일 객체로부터 ContentType 과 FileName 을 얻어준다.
// ContentType과 FileName은 반드시 doc(파일객체명)로 시작해야한다
 File doc;
 String docContentType;
 String docFileName;
 User user; // 파일 객체 외의 정보도 받을 수 있다
 File savedFile;
 
 public String execute() throws Exception {
  if (doc != null && doc.exists()) {
   savedFile = new File(UPLOAD_DIR + docFileName); // 저장될 경로
   FileUtils.copyFile(doc, savedFile); // commons.io 로 파일 업로드 처리
  }
  return SUCCESS;
 }

 // SETTER
 public void setDoc(File doc) {
  this.doc = doc;
 }

 public void setDocContentType(String docContentType) {
  this.docContentType = docContentType;
 }

 public void setDocFileName(String docFileName) {
  this.docFileName = docFileName;
 }

 // GETTER
 public File getDoc() {
  return doc;
 }

 public User getUser() {
  return user;
 }

 public File getSavedFile() {
  return savedFile;
 }

 @Override
 public void prepare() throws Exception {
  user = new User();
 }

 @Override
 public User getModel() {
  return user;
 } 
}


posted by 헤이장