`

struts2文件上传

    博客分类:
  • J2EE
阅读更多

新建一个web项目

从官方网站上下载的 struts-2.2.1.1例子(app)struts2-blank解压。

上传文件主要的jar:commons-fileupload-1.2.1.jar

 

将struts2-blank项目中的lib ,web.xml,struts.xml复制到自己的新建的项目中

 

修改struts.xml

 

    <!-- 使用缺省的struts的配置文件,struts2的支持jar包: struts2-core-2.0.11.2.jar中 -->
         <include file="struts-default.xml"/>
    <!-- Struts2常量配置
        struts.devMode:开发模式,修改配置信息可以不必重新部署服务器
        struts.i18n.encoding:国际化编码
        struts.ognl.allowStaticMethodAccess:允许ognl访问静态方法 -->
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.ognl.allowStaticMethodAccess" value="true" />
    <constant name="struts.multipart.maxSize" value="10701096"/> 


     <package name="example" namespace="/" extends="struts-default">
        <action name="fileupload" class="FileUpload">
            <result>/index.jsp</result>
        </action>
    </package>

 

 

 

Java代码
  1. public   class FileUpload{  
  2.     private  File image;            //与jsp表单中的名称对应   
  3.     private  String imageFileName;  //FileName为固定格式   
  4.     private  String imageContentType ; //ContentType为固定格式  
  5.     public  String execute()  throws  Exception{  
  6. System.out.println("imageFileName = " +imageFileName);     
  7. System.out.println("imageContentType = " +imageContentType);  
  8.         //获取服务器的根路径realpath   
  9.         String realpath = ServletActionContext.getServletContext().getRealPath("/images" );  
  10. System.out.println(realpath);  
  11.         if (image!= null ){  
  12.             File savefile = new  File( new  File(realpath), imageFileName);  
  13.             if (!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();  
  14.             FileUtils.copyFile(image, savefile);  
  15.             ActionContext.getContext().put("message" "上传成功" );  
  16.         }else {  
  17.             ActionContext.getContext().put("message" "上传失败" );  
  18.         }  
  19.         return   "success" ;  
  20.     } 
  21.     public  String getImageContentType() {  
  22.         return  imageContentType;  
  23.     }  
  24.     public   void  setImageContentType(String imageContentType) {  
  25.         this .imageContentType = imageContentType;  
  26.     }  
  27.     public  String getImageFileName() {  
  28.         return  imageFileName;  
  29.     }  
  30.     public   void  setImageFileName(String imageFileName) {  
  31.         this .imageFileName = imageFileName;  
  32.     }  
  33.     public  File getImage() {  
  34.         return  image;  
  35.     }  
  36.     public   void  setImage(File image) {  
  37.         this .image = image;  
  38.     } 
  39. }

 

jsp页面代码
  1. <form action= "${pageContext.request.contextPath}/ fileupload.action"  enctype= "multipart/form-data"  method= "post" >  
  2.     文件:<input type="file"  name= "image" >  
  3.     <input type="submit"  value= "上传" />  
  4.   </form>


@@多文件上传
    在上面的基础上略加改动

Java代码
  1.    public   class FileUpload{  
  2.     private  File[] image;  
  3.     private  String[] imageFileName;  
  4.     private  String[] imageContentType ;  
  5.     
  6.     public  String execute()  throws  Exception{  
  7.         String realpath = ServletActionContext.getServletContext().getRealPath("/images" );  
  8.         System.out.println(realpath);  
  9.         if (image!= null ){  
  10.             File savedir = new  File(realpath);  
  11.             if (!savedir.exists())  
  12.                {   
  13.                   savedir.mkdirs();  
  14.                }  
  15.         System.out.println("image.length = " +image.length);  
  16.             for ( int  i =  0  ; i<image.length ; i++){  
  17.                   
  18. System.out.println("imageContentType[" +i+ "] = " +imageContentType[i]);  
  19.                 File savefile = new  File(savedir, imageFileName[i]);  
  20.                 FileUtils.copyFile(image[i], savefile);  
  21.             }  
  22.             ActionContext.getContext().put("message" "上传成功" );  
  23.         }  
  24.         return   "success" ;  
  25.     } 
  26.     public File[] getImage() {
  27.         return image;
  28.     }
  29.     public void setImage(File[] image) {
  30.         this.image = image;
  31.     }
  32.     public String[] getImageFileName() {
  33.         return imageFileName;
  34.     }
  35.     public void setImageFileName(String[] imageFileName) {
  36.         this.imageFileName = imageFileName;
  37.     }
  38.     public String[] getImageContentType() {
  39.         return imageContentType;
  40.     }
  41.     public void setImageContentType(String[] imageContentType) {
  42.         this.imageContentType = imageContentType;
  43.     }
  44. }

jsp页面代码代码
  1. <form action= "${pageContext.request.contextPath}/fileupload.action"  enctype= "multipart/form-data"  method= "post" >  
  2.     文件1 :<input type= "file"  name= "image" ><br/>  
  3.     文件2 :<input type= "file"  name= "image" ><br/>  
  4.     文件3 :<input type= "file"  name= "image" ><br/>  
  5.     <input type="submit"  value= "上传" />  
  6.   </form> 

分享到:
评论

相关推荐

    struts2文件上传和下载

    struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载

    struts2 文件上传

    struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload

    struts2文件上传实例

    struts2文件上传实例,程序员宝典......

    struts2文件上传jar

    里面包括的是实现struts2文件上传所需要的全部jar包

    Struts2文件上传

    Struts2文件上传

    Struts2 文件上传

    Struts2 文件上传

    Struts2文件上传源码

    Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码

    struts2文件上传与下载

    struts2文件上传与下载,eclipse与myeclipse导入即可使用的代码,简单易懂,希望对你有帮助,喜欢的给个五星评价,谢谢!!!

    struts2 文件上传功能

    Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的...

    struts2文件上传下载源代码

    http://blog.csdn.net/johnjobs/article/details/8076832博文中附件的下载链接

Global site tag (gtag.js) - Google Analytics