Struts 2によるファイルアップロード
4449 ワード
Struts 2のデフォルトはJakartaのCommon-FileUploadファイルアップロードフレームワークを採用しているので、アプリケーションに2つのjarパッケージを追加する必要があります:commons-io-1.4.JArとcommons-fileupload-1.2.1.jar.
ファイルのアップロードページを次のように仮定します.
Actionでは、ファイルドメインの情報をカプセル化するには、3つのプロパティが必要です.
タイプはFileのxxx属性であり、そのファイルドメインに対応するファイル内容をカプセル化する. タイプはStringのxxxFileNameプロパティで、ファイルドメインに対応するファイルのファイル名をカプセル化します. タイプはStringのxxxContentTypeプロパティで、このファイルドメインに対応するファイルのファイルタイプがカプセル化されています.
UploadActionコードは次のとおりです.
そしてstrutsでxmlファイルで構成するには、次の手順に従います.
ファイルのアップロードページを次のように仮定します.
<s:form action="upload.action" method="post" enctype="multipart/form-data">
<s:textfield label=" " name="title"/>
<s:file label=" " name="upload"></s:file>
<s:submit value=" "/>
</s:form>
Actionでは、ファイルドメインの情報をカプセル化するには、3つのプロパティが必要です.
UploadActionコードは次のとおりです.
package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
//
private String title;
//
private File upload;
//
private String uploadContentType;
//
private String uploadFileName;
// ,
private String savePath;
//
// private String allowTypes;
@Override
public String execute() throws Exception {
//
// String filterResult = filterType(getAllowTypes().split(","));
// if(filterResult != null){
// ActionContext.getContext().put("typeError"," ");
// return filterResult; //filterResult input
// }
if(getUpload() == null) {
ActionContext.getContext().put("typeError"," ");
return INPUT;
}
// savePath
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
//
FileInputStream fis = new FileInputStream(getUpload());
//
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) > 0) {
fos.write(buffer,0,len);
}
// TODO Auto-generated method stub
return SUCCESS;
}
// public String getAllowTypes() {
// return allowTypes;
// }
//
// public void setAllowTypes(String allowTypes) {
// this.allowTypes = allowTypes;
// }
public String getSavePath() throws Exception {
//
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
/**
*
* @param types
* @return , null, input
*/
public String filterType(String[] types) {
//
String fileType = getUploadContentType();
// ,
for(String type : types) {
if(type.equals(fileType))
return null;
}
return INPUT;
}
}
そしてstrutsでxmlファイルで構成するには、次の手順に従います.
<action name="upload" class="com.test.action.UploadAction">
<!-- fileUpload -->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
<param name="maximumSize">102400</param>
</interceptor-ref>
<!-- , Struts -->
<interceptor-ref name="defaultStack"/>
<param name="savePath">/upload</param>
<!--
<param name="allowTypes">image/bmp,image/png,image/gif,image/jpeg</param>
-->
<result>success.jsp</result>
<result name="input">upload.jsp</result>
</action>