Struts2でファイルアップロードをする方法

1.一つファイルアップロード
package com.ljcft.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction1 extends ActionSupport implements Serializable {

private File image;
private String imageFileName;
private String imageContentType;

public File getImage() {
return image;
}
public void setImage(File image) {
this.image = image;
}
public String getImageFileName() {
return imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getImageContentType() {
return imageContentType;
}
public void setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}
public String execute(){
System.out.println(imageContentType);
try {

// System.out.println(imageFileName);
ServletContext sc = ServletActionContext.getServletContext();
String storePath = sc.getRealPath(“/files");
// OutputStream out = new FileOutputStream(storePath+"\\"+imageFileName);
// InputStream in = new FileInputStream(image);
// byte b[] = new byte[1024];
// int len = -1;
// while((len=in.read(b))!=-1){
// out.write(b, 0, len);
// }
// out.close();
// in.close();
FileUtils.copyFile(image, new File(storePath,imageFileName));
ActionContext.getContext().put(“message", “ファイルアップ成功!");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}
2.複数ファイルアップロード
package cn.ljcft.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction2 extends ActionSupport implements Serializable {

private File[] images;
private String[] imagesFileName;
private String[] imagesContentType;

public File[] getImages() {
return images;
}

public void setImages(File[] images) {
this.images = images;
}

public String[] getImagesFileName() {
return imagesFileName;
}
public void setImagesFileName(String[] imagesFileName) {
this.imagesFileName = imagesFileName;
}
public String[] getImagesContentType() {
return imagesContentType;
}
public void setImagesContentType(String[] imagesContentType) {
this.imagesContentType = imagesContentType;
}
public String execute(){
try {
if(images!=null&&images.length>0){
ServletContext sc = ServletActionContext.getServletContext();
String storePath = sc.getRealPath(“/files");
for(int i=0;i<images.length;i++)
FileUtils.copyFile(images[i], new File(storePath,imagesFileName[i]));
}
ActionContext.getContext().put(“message", “ファイルアップロード成功!");
return SUCCESS;
} catch (Exception e) {
e.printStackTrace();
return ERROR;
}
}
}

3.jsp画面
<body>
<form action="${pageContext.request.contextPath}/upload/upload2.action" method="post" enctype="multipart/form-data">
ファイル1:<input type="file" name="images"/><br/>
ファイル2:<input type="file" name="images"/><br/>
<input type="submit" value="アップロード"/>
</form>
</body>

4.struts.xml設定
<constant name="struts.multipart.maxSize" value="52428800″></constant>
<package name="upload" namespace="/upload" extends="mypackage">
<action name="upload1″ class="cn.ljcft.action.UploadAction1″ method="execute">
<result name="success">/success.jsp</result>
</action>
<action name="upload2″ class="com.ljcft.action.UploadAction2″ method="execute">
<result name="success">/success.jsp</result>
</action>
</package>

Java

Posted by arkgame