\lib에 추가
commons-fileupload-1.2.2.jar
commons-io-1.4.jar
struts.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>
<constant name="struts.multipart.maxSize" value="104857600"/>
<!-- 용량제한 100메가 -->
<package name="struts" extends="struts-default">
<action name="FileUploadAction" class="action.FileUploadAction">
<result name="input">/jsp/fileUpload.jsp</result>
<result name="success">/jsp/fileUploadOK.jsp</result>
</action>
</package>
</struts>
//interceptor 설정하면 안되더라고
FileUploadAction 클래스
package action;
import java.io.*;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class FileUploadAction extends ActionSupport
{
private static final String UploadPath = "C:\\temp\\";
private File upload; //원본파일
private File saveFile; //복사 될 파일
private String uploadContentType; //contentType
private String uploadFileName; //파일 이름
private String content=""; //파일 내용
public String getContent()
{
String temp="";
try
{
BufferedReader br = new BufferedReader(new FileReader(upload));
while((temp=br.readLine())!=null)
{
content += temp + "\n";
}
}
catch (Exception e)
{
e.printStackTrace();
}
return content;
}
@Override
public String execute() throws Exception
{
if(upload!=null && upload.exists())
{
saveFile = new File(UploadPath + uploadFileName);
FileUtils.copyFile(upload, saveFile);
}
return SUCCESS;
}
public File getUpload()
{
return upload;
}
public String getUploadContentType()
{
return uploadContentType;
}
public String getUploadFileName()
{
return uploadFileName;
}
public File getSaveFile()
{
return saveFile;
}
public void setUpload(File upload)
{
this.upload = upload;
}
public void setUploadContentType(String uploadContentType)
{
this.uploadContentType = uploadContentType;
}
public void setUploadFileName(String uploadFileName)
{
this.uploadFileName = uploadFileName;
}
public void setSaveFile(File saveFile)
{
this.saveFile = saveFile;
}
}
fileUpload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title></title>
</head>
<body>
<s:form action="FileUploadAction" method="post" enctype="multipart/form-data">
<s:file name="upload" label="File" size="50"/>
<s:submit value="upload"/>
</s:form>
</body>
</html>
fileUploadOK.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title></title>
</head>
<body>
<table border="1">
<tr>
<td>contentType</td><td><s:property value="uploadContentType"/></td>
</tr>
<tr>
<td>FileName</td><td><s:property value="uploadFileName"/></td>
</tr>
<tr>
<td>File</td><td><s:property value="upload"/></td>
</tr>
<tr>
<td>content</td><td><s:property value="content"/></td>
</tr>
</table>
</body>
</html>
댓글 없음:
댓글 쓰기