Strutsファイルのアップロードの例
このチュートリアルでは、Struts
<html:file>
タグを使用してサーバーファイルシステムにファイルをアップロードする方法を学習します。
このStrutsファイルのアップロード例をダウンロードする –
Struts-FileUpload-Example.zip
1.アクションフォーム
Actionフォームで、
org.apache.struts.upload.FormFile
変数を作成して、アップロードされたファイルと、アップロードされたファイルのフォーム検証を保持します。
package com.mkyong.common.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;
public class FileUploadForm extends ActionForm{
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if( getFile().getFileSize()== 0){
errors.add("common.file.err",
new ActionMessage("error.common.file.required"));
return errors;
}
//only allow textfile to upload
if(!"text/plain".equals(getFile().getContentType())){
errors.add("common.file.err.ext",
new ActionMessage("error.common.file.textfile.only"));
return errors;
}
//file size cant larger than 10kb
System.out.println(getFile().getFileSize());
if(getFile().getFileSize() > 10240){//10kb
errors.add("common.file.err.size",
new ActionMessage("error.common.file.size.limit", 10240));
return errors;
}
return errors;
}
}
2.アクション
Actionクラスでは、アップロードされたファイルを取得してサーバーファイルシステムに保存し、新しく作成したファイルの詳細をセッションに保存して後で使用できます。
package com.mkyong.common.action;
import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import com.mkyong.common.form.FileUploadForm;
public class FileUploadAction extends Action{
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
FileUploadForm fileUploadForm = (FileUploadForm)form;
FormFile file = fileUploadForm.getFile();
//Get the servers upload directory real path name
String filePath =
getServlet().getServletContext().getRealPath("/") +"upload";
//create the upload folder if not exists
File folder = new File(filePath);
if(!folder.exists()){
folder.mkdir();
}
String fileName = file.getFileName();
if(!("").equals(fileName)){
System.out.println("Server path:" +filePath);
File newFile = new File(filePath, fileName);
if(!newFile.exists()){
FileOutputStream fos = new FileOutputStream(newFile);
fos.write(file.getFileData());
fos.flush();
fos.close();
}
request.setAttribute("uploadedFilePath",newFile.getAbsoluteFile());
request.setAttribute("uploadedFileName",newFile.getName());
}
return mapping.findForward("success");
}
}
3. JSP
:
-
display.jsp **
File uploaded to :
">
Click here to download it
4. struts-config.xml
一緒にリンクする
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config__1__3.dtd">
<struts-config>
<form-beans>
<form-bean
name="fileUploadForm"
type="com.mkyong.common.form.FileUploadForm"/>
</form-beans>
<action-mappings>
<action
path="/UploadPage"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/fileupload.jsp"/>
<action
path="/Upload"
type="com.mkyong.common.action.FileUploadAction"
name="fileUploadForm"
validate="true"
input="/pages/fileupload.jsp"
>
<forward name="success" path="/pages/display.jsp"/>
</action>
</action-mappings>
<message-resources
parameter="com.mkyong.common.properties.Common"/>
</struts-config>
それをテストする
-
http://localhost:8080/StrutsExample/UploadPage.do
** ファイルを選択し、送信ボタンをクリックします。

-
http://localhost:8080/StrutsExample/Upload.do
** これはdisplay.jspに転送され、アップロードされたファイルの詳細が表示されます。
