작성자 : 김길재
스트럿츠의 폼에서 File을 인식하기 못하기 때문에
org.apache.struts.upload.FormFile 클래스를 사용하여야 합니다.
제가 작업하던중 스트럿츠의 파일 업로드에 사용한 소스입니다.
궁금하신 점은 답글 남겨주시거나 메일 보내주시면 대답해 드리겠습니다. ^^
///////////////// image_insert.jsp/////////////////////////////////////
<%@ page contentType="text/html;charset=EUC-KR" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:form action="/ImageInsert" enctype="multipart/form-data" > <table> <TR bgcolor=FFFFFF> <TD height="30" bgcolor='#E6EBF0'>* 이미지(대)</TD> <TD colspan="2"> <html:file property="image_path[0]" size="30"/> </TD> </TR> <TR bgcolor=FFFFFF> <TD height="30" bgcolor='#E6EBF0'>* 이미지(중)</TD> <TD colspan="2"> <html:file property="image_path[1]" size="30"/> </TD> </TR> <TR bgcolor=FFFFFF> <TD height="30" bgcolor='#E6EBF0'>* 이미지(소)</TD> <TD colspan="2"> <html:file property="image_path[2]" size="30"/> </TD> </TR> </table> </form:html>
///////////////////struts-config.xml///////////////////////////////////
<form-beans> <form-bean name="imageinsertForm" type="ImageInsertForm"/> </form-beans>
<action-mappings> <action path="/ImageInsert" type="ImageInsertAction" name="imageinsertForm" validate="false" input="/image_insert.jsp" />
///////////////////ImageInsertForm.java///////////////////////////////
import org.apache.struts.upload.FormFile; public class ImageInsertForm extends ActionForm {
private FormFile[] image_path = new FormFile[3]; public FormFile[] getImage_path() { return image_path; } /** * @param image_path The image_path to set. */ public void setImage_path(FormFile[] image_path) { this.image_path = image_path; } }
////////////////////ImageInsertAction.java/////////////////////////////
import org.apache.struts.upload.FormFile;
public class GoodsInsertAction extends BaseAction { public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) { FormFile image_path[] = ((ImageInsertForm)form).getImage_path(); ImageDAO imageDAO = new ImageDAO(); imageDAO.imageInsert( image_path ); return (mapping.findForward( "image_insert_success" )); } }
/////////////////////////// GoodsFile.java//////////////////////////// /** * @author Administrator * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class GoodsFile { private String fileName = null; private String tempFileName = null;
public String getFileName() { return fileName; } public String getTempFileName() { return tempFileName; }
public void setFileName(String fileName) { this.fileName = fileName; }
public void setTempFileName(String tempFileName) { this.tempFileName = tempFileName; } }
////////////////////ImageDAO.java///////////////////////////////////
import org.apache.struts.upload.FormFile;
public class ImageDAO {
public void imageInsert( FormFile[] image_path ) { Connection con=null; PreparedStatement pstmt=null; ResultSet rs = null; String sql = ""; GoodsFile goodsFile[] = new GoodsFile[ 3 ]; String path = "D:\\upload";
try { for( int i = 0 ; i < 3 ; i++ ) { goodsFile[ i ] = FileUploadUtil.doFileUpload( image_path[ i ] , gcode , Integer.toString( ( i + 1 ) ) ); }
con = DriverManager.getConnection(ConnectionPoolManager.URL_PREFIX+"oraclejava"); sql = "INSERT INTO IMAGES( IMAGE_PATH1 , IMAGE_PATH2 , IMAGE_PATH3 ) VALUES( :IMAGE_PATH1 , :IMAGE_PATH2 , :IMAGE_PATH3 ); pstmt = con.prepareStatement(sql); pstmt.setString( 1 , goodsFile[ 1 ].getFileName() ); pstmt.setString( 2 , goodsFile[ 2 ].getFileName() ); pstmt.setString( 3 , goodsFile[ 3 ].getFileName() ); pstmt.executeUpdate(); } catch( Exception e ) { return null; } finally { try {
if ( rs != null ) rs.close(); if ( pstmt != null ) pstmt.close(); if ( con != null ) con.close(); } catch ( Exception ignore ) { } } }
///////////////////////FileUploadUtil.java///////////////////////////// ublic class FileUploadUtil { public static GoodsFile doFileUpload(FormFile fileList ) throws FileNotFoundException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream stream = fileList.getInputStream();
//파일을 업로드할 절대 경로를 지정해야 한다. String path = "D:\\upload\\"; OutputStream bos = new FileOutputStream( path + fileList.getFileName() ); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { bos.write(buffer, 0, bytesRead); } bos.close(); stream.close();
GoodsFile boardFile = new GoodsFile(); boardFile.setFileName(fileList.getFileName()); boardFile.setTempFileName(fileList.getFileName() );
return boardFile; } } |