如果form里面有file标签,对应的formbean怎么写?

03-04-09 aronfb

如题,项目中有这个需求。

需要提交一个文件到服务器端,那么struts里面formbean中怎么办?

banq
2003-04-10 13:10

JSP页面:
<%@page language="java">
<%@taglib 
    uri="/WEB-INF/struts-html.tld"
   prefix="html">

<html:form action="uploadAction.do" enctype="multipart/form-data">

    Please Input Text: <html:text property="myText">
    Please Input The File You Wish to Upload: <html:file property="myFile">
   
   <html:submit />

</html:form>


ActionForm bean: 



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

public class UploadForm extends ActionForm {

    protected String myText;
    protected FormFile myFile;
    
    public void setMyText(String text) {
        myText = text;
    }
    
    public String getMyText() {
        return myText;
    }
    
    public void setMyFile(FormFile file) {
        myFile = file;
    }
    
    public FormFile getMyFile() {
        return myFile;
    }

}

<p>

Look at the Javadocs for FormFile to see the methods it exposes to manipulate files in file uploading. Also look at the Javadocs for ActionServlet and ActionMapping for the various parameters you can specify to change how files are uploaded.

Basically in your execute method in your action class you would call ((UploadForm) form).getMyFile() to retrieve the FormFile and do what you want with it.

http://jakarta.apache.org/struts/api/org/apache/struts/upload/FormFile.html