ファイルアップロード機能を実現し、アップロードした画像をページに表示する


struts.xml:
<package name="advance" extends="struts-default"
  namespace="/day04">
  <interceptors>
   <interceptor name="some" class="interceptor.SomeInterceptor"/>
  </interceptors>
    <action name="uploadform" >
   <result name="success">/WEB-INF/jsp/day04/uploadform.jsp</result>
  </action>
  <action name="upload" class="day04.UploadAction">
   <interceptor-ref name="fileUpload">
    <param name="maximumSize">1024000</param>
   </interceptor-ref>
   <interceptor-ref name="basicStack" />
   <result name="success">
    /WEB-INF/jsp/day04/uploadimage.jsp
   </result>
  </action>
 </package>

uploadform.jsp
<%@page pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title></title>
	</head>
	<body>
		<h1>
			 
		</h1>
		<s:form action="upload" method="post" theme="xhtml"
			enctype="multipart/form-data">
			<s:file label="File" name="some" />
			<s:submit value=" " />
		</s:form>
		<br />
		<br />
		<br />
	</body>
</html>

uploadimage.jsp
<%@page pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title></title>
	</head>
	<body>
		<h1>
			 
		</h1>
		<img src="../${imagePath}" />
		<br />
		<br />
		<br />
	</body>
</html>

UploadAction.java:
package day04;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.io.IOUtils;

import outman.BaseAction;


public class UploadAction extends BaseAction {
	private File some;
	private String someFileName;
	private String someContentType;
	private String imagePath;
	public String execute() throws Exception{
		System.out.println(some);
		System.out.println(some.length());
		System.out.println(someFileName);
		System.out.println(someContentType);
		String imageName = "file_" + System.currentTimeMillis()
		+ someFileName.substring(someFileName.lastIndexOf("."));
		System.out.println(imageName);
		imagePath = "upload_image/" + imageName;
		String realImagePath = toRealPath(imagePath);
		System.out.println(realImagePath);
		BufferedInputStream is = new BufferedInputStream(new FileInputStream(
				some));
		BufferedOutputStream os = new BufferedOutputStream(
				new FileOutputStream(realImagePath));
		IOUtils.copy(is, os);
		is.close();
		os.close();
		return "success";
	}
	public File getSome() {
		return some;
	}
	public void setSome(File some) {
		this.some = some;
	}
	public String getSomeFileName() {
		return someFileName;
	}
	public void setSomeFileName(String someFileName) {
		this.someFileName = someFileName;
	}
	public String getSomeContentType() {
		return someContentType;
	}
	public void setSomeContentType(String someContentType) {
		this.someContentType = someContentType;
	}
	public String getImagePath() {
		return imagePath;
	}
	public void setImagePath(String imagePath) {
		this.imagePath = imagePath;
	}
}