我从webstie https://www.mkyong.com/struts/struts-file-upload-example/引用了关于strust上传文件的文章,但我不明白一些问题。我希望得到帮助。 为什么要使用Action Form类? 我明白这是javabean? 和代码
Private FormFile file;
Public FormFile getFile () {
Return file;
}
Public void setFile (FormFile file) {
This.file = file;
}
为要调用的文件创建getter setter方法???
我不明白这个命令:
FileUploadForm fileUploadForm = (FileUploadForm) form;
FormFile file = fileUploadForm.getFile ();
任何人都可以帮我解释上面的命令行。
Fos.flush ();
据我所知,它用于删除FileOutStream上的数据并准备读取数据。期待帮助
行动表格
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;
}
}
动作
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");
}
}
答案 0 :(得分:0)
任何人都可以帮我解释上面的命令行。
内联
/**
* This would case the ActionForm object named form to
* FileUploadForm which is its sub-class's object
*/
FileUploadForm fileUploadForm = (FileUploadForm) form;
/**
* This uses the getter for the field `private FormFile file;`
* in the FileUploadForm class definition to create a FormFile object
*/
FormFile file = fileUploadForm.getFile ();
此外,共享代码中fos.flush()
上方的行:
fos.write(file.getFileData());
// This writes to output stream using the file data from the FormFile object we got previously