Struts2:如何使用拦截器上传文件? [英] Struts2 : How to upload file using intercepter?

查看:128
本文介绍了Struts2:如何使用拦截器上传文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Struts2上传文件(图像).但是,我的文件,文件内容和文件名类型具有null值! 我尝试搜索此问题,但没有任何结果.

I try to upload a file (image) using Struts2. But, my file,file content and filename type have a null value ! I tried searching for this problem but without any result.

这就是我想要做的:

jsp:

 <s:form id="registerSubmit" method="post" enctype="multipart/form-data" theme="bootstrap">

<s:textfield name="tel" cssClass="form-control" label="tel :"></s:textfield>

<label for="myFile">Upload your file</label>
<input type="file" name="file" />

<button type="submit" id="submit" > Enregistrer</button>
</s:form>

操作:

 public class Gestion extends ActionSupport implements  SessionAware, ModelDriven{

        private Visit c;
        private Service Service;
        private Long tel;
        private File file;
        private String fileContentType;
        private String fileFileName;

            public String addvisit(){

                c = new visit();
                Service = new ServiceImpl();            
                c.setTel(tel);
                System.out.println(fileContentType); //null
                System.out.println(fileFileName); //null
                byte[] bFile = null;
                if(file != null)
                    bFile = new byte[(int) file.length()]; 
                c.setPhoto(bFile); // null

                Service.add(c);



        return "success";

            }
     //setters and getters

    }

struts.xml

<struts>
 <constant name="struts.devMode" value="true" />
   <constant name="struts.multipart.maxSize" value="10000000" /> 

    <package name="login" extends="struts-default" namespace="/">

    <action name="addvisit" class="action.Gestion"
            method="addvisit">
               <interceptor-ref name="fileUpload">
                <param name="maximumSize">2097152</param>
                <param name="allowedTypes">
                    image/png,image/gif,image/jpeg,image/pjpeg
                </param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            <result name="success" type="json" >
                    <param name="root">map</param>
            </result>
        </action>
</package>
</struts>

推荐答案

您没有在表单中的任何位置指向操作.

You are not pointing to your Action anywhere in your form.

我还建议您尽可能使用(自封闭的)Struts标签:

I also suggest you to use (self closed) Struts Tags when possible:

<s:form action="addvisit" id="registerSubmit" method="post" 
        enctype="multipart/form-data" theme="bootstrap">
    <s:textfield name="tel" cssClass="form-control" label="tel :" />
    <s:file name="file" label="Upload your file" />
    <s:submit id="submit" value="Enregistrer" />
</s:form>

也就是说,您的错误是与ModelDriven相关的99%.要么删除ModelDriven接口实现,要么将堆栈配置为在ModelDriven之后带有 ,就像在默认堆栈中一样(实际上您使用的是FileUpload拦截器的两倍):

That said, your error is 99% related to ModelDriven. Either remove the ModelDriven interface implementation, or configure the stack to have the FileUpload Interceptor after the ModelDriven one, like in the default stack (you are actually using two times the FileUpload interceptor):

<action name="addvisit" class="action.Gestion" method="addvisit">

    <interceptor-ref name="defaultStack">
        <param name="fileUpload.maximumSize">2097152</param>
        <param name="fileUpload.allowedTypes">
            image/png,image/gif,image/jpeg,image/pjpeg
        </param>            
    </interceptor-ref>

    <result name="success" type="json" >
            <param name="root">map</param>
    </result>
</action>

这篇关于Struts2:如何使用拦截器上传文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆