Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- paint
- JavaScript
- 이클립스
- JSP
- OGNL
- oracle
- 에러페이지
- HTML
- AWT
- 클래스
- Spring
- 국제화
- 오버로딩
- Graphic
- 어노테이션
- Eclips
- layout
- 배열
- 예외처리
- 메소드
- 기본
- 안드로이드
- struts2
- Android
- 전화걸기
- Java
- 생성자
- Menu
- 메서드
- mybatis
Archives
- Today
- Total
note
struts2 파일 업로드(자카르타 라이브러리) 본문
위에 방법보다 간단한 방법
단일 파일 업로드
파일 위치
WebContent -> tmp
WebContent -> upload
폴더 생성
src ->struts.properties 생성
tmp 폴더 절대경로 접근하기
폴더 선택후 우클릭 Properties 접근
절대 경로 나옴
절대 경로중 역슬러시를 슬러시로 변경하여 사용합니다
upload 폴더 새로고침
struts-ch6.xml
<?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="ch6" namespace="/ch6" extends="struts-default">
<!-- 파일 업로드 -->
<action name="fileUpload" class="com.ch6.action.FileUploadAction">
<interceptor-ref name="prepare" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload" />
<interceptor-ref name="params" />
<interceptor-ref name="workflow" />
<result name="input">multipartForm.jsp</result>
<result name="success">fileUploadSuccess.jsp</result>
</action>
</package>
</struts>
struts.xml
<include file="struts-ch6.xml" />
com.ch6.action/FileUploadAction
package com.ch6.action;
import java.io.File;
import org.apache.commons.io.FileUtils;
import com.ch4.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class FileUploadAction extends ActionSupport implements Preparable,ModelDriven{
//임시디렉토리에 저장되어 있는
//파일을 복사해서 영구 보관할 디렉토리
private final String UPLOAD_DIR="파일 업로드 위치 직접 입력";
//fileUpload 인터셉터가 임시 디렉토리에 저장한 파일 객체
File doc;
//파일 컨텐트 타입
String docContentType;
//전송된 파일의 파일명
String docFileName;
User user;
//영구 보관될 디렉토리에 저장된 파일
File savedFile;
@Override
public String execute() throws Exception {
//fileUpload 인터셉터가 임시 디렉토리에 저장한 파일을 보관할 디렉토리
//(영구 보관 디렉토리)로 복사
//임시 디렉토리의 파일은 액션 실행이 종료된후 fileUpload 인터셉터가 삭제
if(doc != null && doc.exists()){
savedFile = new File(UPLOAD_DIR +"/"+docFileName);
FileUtils.copyFile(doc, savedFile);
}
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 Object getModel() {
return user;
}
@Override
public void prepare() throws Exception {
user = new User();
}
}
ch6/fileUploadSuccess.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>파일 업로드</title>
</head>
<body>
파일 업로드를 성공했습니다<br>
User : ${user}<br/>
임시파일 (삭제되었습니다) : ${doc}<br/>
저장파일 : ${savedFile}
</body>
</html>
ch6/multipartForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>파일 업로드</title>
</head>
<body>
<font color="red">${actionError[0]}</font>
<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="전송">
</form>
</body>
</html>
struts.properties
#파일 업로드 라이브러리 지정 struts.multipart.parse=jakarta #tmp 폴더 지정 struts.multipart.saveDir=파일 업로드 위치 직접 입력 #최대 업로드 사이즈 지정 struts.multipart.maxSize=104857600
struts.properies에 모든 정보를 셋팅할수는 없습니다
#이 주석
파일 업로드시 문제가 생겼을시에 multipartForm.jsp로 다시 리턴
업로드 폴더에 /를 붙이고 docFileName = tmp에 있는 파일을 원래 업로드 폴더로 옮기려는 작업
FileUtils.copyFile(doc, savedFile);tmp 에 있는 파일을 upload폴더로 이동
후처리를 하여 이동하고 tmp에 있는 파일을 삭제
이론적으로 삭제를 하지만 간혹가다가 tmp파일을 못지우는 경우도 있다
파일 최대 용량 줄인뒤 에러 확인
struts.properties
#파일 업로드 라이브러리 지정 struts.multipart.parse=jakarta #tmp 폴더 지정 struts.multipart.saveDir=파일 업로드 위치 직접 입력 #최대 업로드 사이즈 지정 struts.multipart.maxSize=1048
콘솔창에도 에러 확인 가능합니다
다중 업로드
파일 위치
단일 업로드에 사용했던 파일들 재사용
단일 업로드에 사용했던 파일들 재사용
struts.xml
<!-- 다중 파일 업로드 --> <action name="fileUpload2" class="com.ch6.action.FileUploadAction2"> <interceptor-ref name="prepare" /> <interceptor-ref name="modelDriven" /> <interceptor-ref name="fileUpload" /> <interceptor-ref name="params" /> <interceptor-ref name="workflow" /> <result name="input">multipartForm2.jsp</result> <result name="success">fileUploadSuccess2.jsp</result> </action>
FileUploadAction2.java
package com.ch6.action;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import com.ch4.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class FileUploadAction2 extends ActionSupport implements Preparable,ModelDriven{
private final String UPLOAD_DIR ="파일 업로드 위치";
User user;
private List<File> doc;
private List<String> docFileName;
private List<String> docContentType;
private List<File> savedFile;
@Override
public String execute() throws Exception {
savedFile = new ArrayList<File>();
for(int i =0; doc != null && i<doc.size(); i++){
if(doc.get(i) != null && doc.get(i).exists()){
savedFile.add(new File(UPLOAD_DIR+"/"+docFileName.get(i)));
FileUtils.copyFile(doc.get(i), savedFile.get(i));
}
}
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public List<File> getDoc() {
return doc;
}
public void setDoc(List<File> doc) {
this.doc = doc;
}
public List<String> getDocFileName() {
return docFileName;
}
public void setDocFileName(List<String> docFileName) {
this.docFileName = docFileName;
}
public List<String> getDocContentType() {
return docContentType;
}
public void setDocContentType(List<String> docContentType) {
this.docContentType = docContentType;
}
public List<File> getSavedFile() {
return savedFile;
}
public void setSavedFile(List<File> savedFile) {
this.savedFile = savedFile;
}
@Override
public Object getModel() {
return user;
}
@Override
public void prepare() throws Exception {
user= new User();
}
}
fileUploadSuccess2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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=UTF-8">
<title>파일 업로드</title>
</head>
<body>
파일 업로드를 성공했습니다<br>
User : ${user}<br/>
<c:forEach var="tmp_file" items="${doc}">
임시파일 (삭제되었습니다) : ${tmp_file}<br/>
</c:forEach>
<br/>
<c:forEach var="file" items="${savedFile}">
저장파일 : ${file}<br/>
</c:forEach>
</body>
</html>
multipartForm2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>파일 업로드</title>
</head>
<body>
<font color="red">${actionErrors[0]}</font>
<form action="fileUpload2.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/>
파일1 : <input type="file" name="doc"><br/>
파일2 : <input type="file" name="doc"><br/>
<input type="submit" value="전송">
</form>
</body>
</html>
'JSP > Struts2' 카테고리의 다른 글
| struts2 예외발생 에러페이지 (0) | 2012.02.22 |
|---|---|
| Struts2 Servlet (0) | 2012.02.22 |
| struts2 Redirect (0) | 2012.02.22 |
| struts2 chain (0) | 2012.02.22 |
| struts2 커스텀 인터페이스 (0) | 2012.02.22 |
commons-fileupload-1.2.2.jar