struts2的文件上传要点

来源:互联网 发布:4g网络优化工程师 编辑:程序博客网 时间:2024/05/23 18:29
注意要点


1.表单


   a: 必加 entype="multipart/form-data"    
   b: 必加  method=post     
   c: type=file,且name与<input type="file" name="cusImg">




                <form action="${pageContext.request.contextPath }/customer/addCustomer" enctype="multipart/form-data" method="post">
客户姓名:<input type="text" name="cusName"><br/>
客户照片:<input type="file" name="cusImg"><br/>
客户电话:<input type="text" name="cusPhone"><br/>
<input type="submit" value="提交">
</form>




2.action
  
   a:要配备input视图,error.jsp页面


    用注解


        @Action(value="addCustomer",results={@Result(name="success",location="findAllCustomer",type="redirectAction"),
@Result(name="input",location="/jsp/error.jsp")})-------------------------------------------------------------------------------------------             
public String addCustomer() {
try {
String path = ServletActionContext.getServletContext().getRealPath("/upload");
File file = new File(path,cusImgFileName);//new File(path+cusImgFileName)
FileUtils.copyFile(cusImg, file);


Customer customer = new Customer();
customer.setCusName(cusName);
customer.setCusPhone(cusPhone);
String imgSrc = path+cusImgFileName;
customer.setImgSrc(ServletActionContext.getRequest().getContextPath()+"/upload/"+cusImgFileName);
customerService.addCustomer(customer);




       error.jsp页面中添加jsp页面
 
                   <body>
                  <s:actionerror />
                  <s:fielderror />
                   </body>
           
              




      不用注解
          


             -<struts>


                   <constant value="true" name="struts-devMode"/>




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




-                                      <action name="up" method="upload" class="cn.itcast.upload.UpAction">


                                                   <result name="input">/error.jsp</result>


                                       </action>


                            </package>


              </struts>
         




3.文件大小设置
  
          struts.xml页面中


                            <constant name="struts.multipart.maxSize" value="1000000000"></constant>












-<struts>


       <constant value="true" name="struts.devMode"/>----------------------开发者模式打开,修改配置后不需要从启tomcat


       <constant value="1000000000000" name="struts.multipart.maxSize"/>-----------------------加在这个位置可以规范所有的上上传文件的大小
                   <package name="many" extends="struts-default" namespace="/">
                             <action name="up" method="upload" class="cn.itcast.upload.UploadAction">


                                  <!-- 加上下面这个就可以规定每个action上传文件的大小 -->






-                                             <interceptor-ref name="fileUpload">------------------------也可以加这个拦截器,可以具体控制此action中上传文件的大小,但是上面的constant配置也要加,两个以小的参数为标准


                                                            <param name="maximumSize"/>


                                              </interceptor-ref>


                                              <interceptor-ref name="defaultStack"/>---------------------加了fileUpload拦截器后,还需要引入默认拦截器


                                <!-- <result name="input">/error.jsp</result> -->




</action>


</package>


</struts>


        
原创粉丝点击