struts中使用FormFile文件上传

用贯了spring mvc的注入式文件上传,回到struts中都忘了怎么写,翻了翻老项目,记录下。
struts config中,定义formBean,action中用name指定formBean。

<struts-config>
    <form-beans>
        <form-bean name="fileManagerForm" type="com.dorole.FileManagerForm" />
    </form-beans>
    <action path="..." type="..." parameter="method" name="fileManagerForm">
        <forward name="..." path="..."></forward>
    </action>
</struts-config>

FileManagerForm如下

public class FileManagerForm extends ActionForm {
    private FormFile file;
    public void setFile(FormFile file) {
        this.file = file;
    }
    public FormFile getFile() {
        return file;
    }
}

FileManagerAction如下

public ActionForward upload(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        FileManagerForm fmf = (FileManagerForm) form;
        FormFile formFile = fmf.getFile();
        if (formFile.getFileData().length != 0) {
            ...
        }
        return null;
}

jsp如下

<form action="..." method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="upload" />
</form>

本文链接地址:https://dorole.com/676/

来自:Dorole's Blog

发布者

Steve

编程/摄影

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!: