먼저 enctype="multipart/form-data 인 form 객체를 생성하고
action은 설정에 맞게 적당히 작성합니다.
fileForm.jsp
---------------------------------------------------------------------------------
<form name="fileform" method="post" action="/uploadFile.do" enctype="multipart/form-data">
Select File: <input type="file" name="file"> </br>
<input type="submit" value="Upload File">
</form>
-------------------------------------------------------------------------------
web.xml에서 설정한 struts-config.xml 에서
<action> 태그와 <form-bean> 태그를 작성합니다.
struts-config.xml
-----------------------------------------------------------------------------------
<form-bean name="fileForm" type="form.FormFile">
<form-property name="file" type="org.apache.struts.upload.FormFile"/>
</form-bean>
<action path = "/uploadFile"
name = "fileForm"
type = "action.UploadFileAction"
forward = "/notice/listFile.jsp"/>
-------------------------------------------------------------------------------
ActionForm을 상속받은 FileForm객체를 작성합니다.
FileForm.java
-----------------------------------------------------------------------------
package form;
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
public class FileForm extends ActionForm {
public FileForm(){}
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
--------------------------------------------------------------------------------
파일업로드 처리가 이루어지는
Action 을 상속받은 객체를 작성합니다.
UploadFileAction .java
---------------------------------------------------------------------------------
package action;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import form.FileForm;
public class UploadFileAction extends Action {
public UploadFileAction() {
// TODO Auto-generated constructor stub
}
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res)
{
try
{
System.out.println("===================UploadFileAction START==========");
FileForm fileform = (FileForm)form;
//--------------파일 처리하는 부분 시작--------------------------//
FormFile file = fileform.getFile();
String contentType = file.getContentType();
String fileName = file.getFileName();
int fileSize = file.getFileSize();
byte[] fileData = file.getFileData();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
//파일을 업로드할 절대 경로를 지정해야 한다.
String path = "D:\\upload\\";
OutputStream bos = new FileOutputStream( path + file.getFileName() );
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1)
{
bos.write(buffer, 0, bytesRead);
}
baos.close();
bos.close();
stream.close();
file.destroy();
//--------------파일 처리하는 부분 끝--------------------------//
System.out.println("===================UploadFileAction END==========");
}
catch(Exception e){e.printStackTrace();}
return mapping.findForward("uploadFile");
}
}
출처 : http://cafe.naver.com/2007jsp.cafe?iframe_url=/ArticleRead.nhn%3Farticleid=128&
'JAVA' 카테고리의 다른 글
안드로이드 배경 음악 재생하기 (1) | 2012.07.29 |
---|---|
struts 한글 깨짐 (0) | 2012.07.29 |
자바에서 OS 알아내기 (0) | 2012.07.29 |
java.sql.SQLException: 데이터 크기가 해당 유형의 최대 크기보다 큽니다 (0) | 2012.07.29 |
cannot cast from ActionForm to *form (0) | 2012.07.29 |