转:ext+struts2实现上传

upload.html
    <html> 
    <head>    
       <title>上传文件</title>    
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">      
        <link rel="stylesheet" type="text/css" href="/ext-2.1/resources/css/ext-all.css"/> 
       <script type="text/javascript" src="/ext-2.1/adapter/ext/ext-base.js"></script> 
       <script type="text/javascript" src="/ext-2.1/ext-all.js"></script> 
       <script type="text/javascript" src="/newExt/upload.js"></script>     
   </head> 
   <body> 
   <h1>上传文件</h1> 
</body> 
   </html> 

<html>

<head>

    <title>上传文件</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 

    <link rel="stylesheet" type="text/css" href="/ext-2.1/resources/css/ext-all.css"/>

    <script type="text/javascript" src="/ext-2.1/adapter/ext/ext-base.js"></script>

    <script type="text/javascript" src="/ext-2.1/ext-all.js"></script>

    <script type="text/javascript" src="/newExt/upload.js"></script>

</head>

<body>

<h1>上传文件</h1>

</body>

</html>

upload.js

   1. Ext.onReady(function() { 
   2.  
   3.     var form = new Ext.form.FormPanel({ 
   4.  
   5.         fileUpload: true,    
   6.  
   7.         baseCls: 'x-plain', 
   8.  
   9.         layout:'absolute', 
  10.  
  11.         url:'save-form.php', 
  12.  
  13.         defaultType: 'textfield', 
  14.  
  15.         frame:true,    
  16.  
  17.         url: '/member/upload.action',// 
  18.  
  19.  
  20.  
  21.         items: [{ 
  22.  
  23.            xtype: 'textfield',     
  24.  
  25.            fieldLabel: '文件名',     
  26.  
  27.            name: 'upload',     
  28.  
  29.            inputType: 'file'//文件类型     
  30.  
  31.         }], 
  32.  
  33.         buttons: [{     
  34.  
  35.             text: '上传',     
  36.  
  37.             handler: function() {     
  38.  
  39.                 form.getForm().submit({     
  40.  
  41.                    success: function(form, action){     
  42.  
  43.                       Ext.Msg.alert('信息', '文件上传成功!');     
  44.  
  45.                    },     
  46.  
  47.                    failure: function(){     
  48.  
  49.                       Ext.Msg.alert('错误', '文件上传失败');     
  50.  
  51.                    }     
  52.  
  53.                 });     
  54.  
  55.             }     
  56.  
  57.         }]  
  58.  
  59.     }); 
  60.  
  61.  
  62.  
  63.     var window = new Ext.Window({ 
  64.  
  65.         title: 'Resize Me', 
  66.  
  67.         width: 500, 
  68.  
  69.         height:300, 
  70.  
  71.         minWidth: 300, 
  72.  
  73.         minHeight: 200, 
  74.  
  75.         layout: 'fit', 
  76.  
  77.         plain:true, 
  78.  
  79.         bodyStyle:'padding:5px;', 
  80.  
  81.         buttonAlign:'center', 
  82.  
  83.         items: form 
  84.  
  85.     }); 
  86.  
  87.  
  88.  
  89.     window.show(); 
  90.  
  91. }); 

Ext.onReady(function() {

    var form = new Ext.form.FormPanel({

    fileUpload: true,  

        baseCls: 'x-plain',

        layout:'absolute',

        url:'save-form.php',

        defaultType: 'textfield',

        frame:true,  

        url: '/member/upload.action',//



        items: [{

       xtype: 'textfield',   

       fieldLabel: '文件名',   

       name: 'upload',   

       inputType: 'file'//文件类型   

        }],

        buttons: [{   

        text: '上传',   

        handler: function() {   

        form.getForm().submit({   

       success: function(form, action){   

          Ext.Msg.alert('信息', '文件上传成功!');   

       },   

       failure: function(){   

          Ext.Msg.alert('错误', '文件上传失败');   

       }   

   });   

      }   

}]

    });



    var window = new Ext.Window({

        title: 'Resize Me',

        width: 500,

        height:300,

        minWidth: 300,

        minHeight: 200,

        layout: 'fit',

        plain:true,

        bodyStyle:'padding:5px;',

        buttonAlign:'center',

        items: form

    });



    window.show();

});

PhotoAction.java
view plaincopy to clipboardprint?

   1. public class PhotoAction extends SessionAwareAction { 
   2.  
   3.  
   4.  
   5.     private static final long serialVersionUID = -3991411826094455715L; 
   6.  
   7.      
   8.  
   9.      
  10.  
  11.     private File[] upload; 
  12.  
  13.  
  14.  
  15.     private String[] uploadFileName; 
  16.  
  17.  
  18.  
  19.     private String[] uploadContentType; 
  20.  
  21.  
  22.  
  23.     private String[] dir; 
  24.  
  25.  
  26.  
  27.     private String[] targetFileName;  
  28.  
  29.      
  30.  
  31.     /**为上传文件自动分配文件名称,避免重复
  32. 
  33.      * @param fileName
  34. 
  35.      * @return
  36. 
  37.      */ 
  38.  
  39.     private String generateFileName(String fileName) {  
  40.  
  41.         // 获得当前时间 
  42.  
  43.         DateFormat format = new SimpleDateFormat("yyMMddHHmmss");  
  44.  
  45.         // 转换为字符串 
  46.  
  47.         String formatDate = format.format(Calendar.getInstance().getTime());  
  48.  
  49.         // 随机生成文件编号 
  50.  
  51.         int random = new Random().nextInt(10000);  
  52.  
  53.         // 获得文件后缀名称 
  54.  
  55.         int position = fileName.lastIndexOf(".");  
  56.  
  57.         String extension = fileName.substring(position);  
  58.  
  59.         // 组成一个新的文件名称 
  60.  
  61.         return formatDate + random + extension;  
  62.  
  63.     } 
  64.  
  65.      
  66.  
  67.     public String upload() throws IOException { 
  68.  
  69.         // 获得upload路径的实际目录 
  70.  
  71.         @SuppressWarnings("unused") 
  72.  
  73.         Member member = (Member)getSessionMap().get("member"); 
  74.  
  75.         String realPath = "";//ServletActionContext.getServletContext().getRealPath("/image/" + member.getEmail() + "/" + (String)getSessionMap().get("selectAlbum"));  
  76.  
  77.         //获得实际目录 
  78.  
  79.         String targetDirectory = "C://";  
  80.  
  81.         String[] mydir = new String[upload.length];  
  82.  
  83.         String[] tname = new String[upload.length];  
  84.  
  85.          
  86.  
  87.         for (int i = 0; i < upload.length; i++) {  
  88.  
  89.             // 生成保存文件的文件名称 
  90.  
  91.             tname[i] = generateFileName(uploadFileName[i]);  
  92.  
  93.             // 保存文件的路径 
  94.  
  95.             mydir[i] = targetDirectory + "//" + tname[i];  
  96.  
  97.  
  98.  
  99.             // 建立一个目标文件 
100.  
101.             File target = new File(targetDirectory, tname[i]);  
102.  
103.             // 将临时文件复制到目标文件 
104.  
105.             try { 
106.  
107.                 FileUtils.copyFile(upload[i], target); 
108.  
109.             } catch (IOException e) { 
110.  
111.                 this.setMessage(e.getMessage()); 
112.  
113.                 e.printStackTrace(); 
114.  
115.             }  
116.  
117.              
118.  
119.         }  
120.  
121.          
122.  
123.         this.setMessage("上传" + upload.length + "张照片成功"); 
124.  
125.         return SUCCESS; 
126.  
127.     } 
128.  
129.  
130.  
131.     public File[] getUpload() { 
132.  
133.         return upload; 
134.  
135.     } 
136.  
137.  
138.  
139.     public void setUpload(File[] upload) { 
140.  
141.         this.upload = upload; 
142.  
143.     } 
144.  
145.  
146.  
147.     public String[] getUploadFileName() { 
148.  
149.         return uploadFileName; 
150.  
151.     } 
152.  
153.  
154.  
155.     public void setUploadFileName(String[] uploadFileName) { 
156.  
157.         this.uploadFileName = uploadFileName; 
158.  
159.     } 
160.  
161.  
162.  
163.     public String[] getUploadContentType() { 
164.  
165.         return uploadContentType; 
166.  
167.     } 
168.  
169.  
170.  
171.     public void setUploadContentType(String[] uploadContentType) { 
172.  
173.         this.uploadContentType = uploadContentType; 
174.  
175.     } 
176.  
177.  
178.  
179.     public String[] getDir() { 
180.  
181.         return dir; 
182.  
183.     } 
184.  
185.  
186.  
187.     public void setDir(String[] dir) { 
188.  
189.         this.dir = dir; 
190.  
191.     } 
192.  
193.  
194.  
195.     public String[] getTargetFileName() { 
196.  
197.         return targetFileName; 
198.  
199.     } 
200.  
201.  
202.  
203.     public void setTargetFileName(String[] targetFileName) { 
204.  
205.         this.targetFileName = targetFileName; 
206.  
207.     } 
208.  
209.  
210.  
211. } 

public class PhotoAction extends SessionAwareAction {



private static final long serialVersionUID = -3991411826094455715L;





private File[] upload;



private String[] uploadFileName;



private String[] uploadContentType;



private String[] dir;



private String[] targetFileName;



    /**为上传文件自动分配文件名称,避免重复

     * @param fileName

     * @return

     */

    private String generateFileName(String fileName) {

        // 获得当前时间

        DateFormat format = new SimpleDateFormat("yyMMddHHmmss");

        // 转换为字符串

        String formatDate = format.format(Calendar.getInstance().getTime());

        // 随机生成文件编号

        int random = new Random().nextInt(10000);

        // 获得文件后缀名称

        int position = fileName.lastIndexOf(".");

        String extension = fileName.substring(position);

        // 组成一个新的文件名称

        return formatDate + random + extension;

    }

   

    public String upload() throws IOException {

        // 获得upload路径的实际目录

@SuppressWarnings("unused")

Member member = (Member)getSessionMap().get("member");

        String realPath = "";//ServletActionContext.getServletContext().getRealPath("/image/" + member.getEmail() + "/" + (String)getSessionMap().get("selectAlbum"));

        //获得实际目录

        String targetDirectory = "C://";

        String[] mydir = new String[upload.length];

        String[] tname = new String[upload.length];

       

        for (int i = 0; i < upload.length; i++) {

            // 生成保存文件的文件名称

            tname[i] = generateFileName(uploadFileName[i]);

            // 保存文件的路径

            mydir[i] = targetDirectory + "//" + tname[i];



            // 建立一个目标文件

            File target = new File(targetDirectory, tname[i]);

            // 将临时文件复制到目标文件

            try {

FileUtils.copyFile(upload[i], target);

} catch (IOException e) {

this.setMessage(e.getMessage());

e.printStackTrace();

}



        }

       

this.setMessage("上传" + upload.length + "张照片成功");

return SUCCESS;

}



public File[] getUpload() {

return upload;

}



public void setUpload(File[] upload) {

this.upload = upload;

}



public String[] getUploadFileName() {

return uploadFileName;

}



public void setUploadFileName(String[] uploadFileName) {

this.uploadFileName = uploadFileName;

}



public String[] getUploadContentType() {

return uploadContentType;

}



public void setUploadContentType(String[] uploadContentType) {

this.uploadContentType = uploadContentType;

}



public String[] getDir() {

return dir;

}



public void setDir(String[] dir) {

this.dir = dir;

}



public String[] getTargetFileName() {

return targetFileName;

}



public void setTargetFileName(String[] targetFileName) {

this.targetFileName = targetFileName;

}



}


配置文件

<package name="com.hm.member.action" namespace="/member" extends="struts-default">

<action name="upload" class="photoAction" method="upload">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
辽B代驾管理系统对代驾订单管理、用户咨询管理、代驾订单评价管理、代驾订单投诉管理、字典管理、论坛管理、公告管理、新闻信息管理、司机管理、用户管理、管理员管理等进行集中化处理。经过前面自己查阅的网络知识,加上自己在学校课堂上学习的知识,决定开发系统选择小程序模式这种高效率的模式完成系统功能开发。这种模式让操作员基于浏览器的方式进行网站访问,采用的主流的Java语言这种面向对象的语言进行辽B代驾管理系统程序的开发,在数据库的选择上面,选择功能强大的Mysql数据库进行数据的存放操作。辽B代驾管理系统的开发让用户查看代驾订单信息变得容易,让管理员高效管理代驾订单信息。 辽B代驾管理系统具有管理员角色,用户角色,这几个操作权限。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 辽B代驾管理系统针对管理员设置的功能有:添加并管理各种类型信息,管理用户账户信息,管理代驾订单信息,管理公告信息等内容。 辽B代驾管理系统针对用户设置的功能有:查看并修改个人信息,查看代驾订单信息,查看公告信息等内容。 系统登录功能是程序必不可少的功能,在登录页面必填的数据有两项,一项就是账号,另一项数据就是密码,当管理员正确填写并提交这二者数据之后,管理员就可以进入系统后台功能操作区。项目管理页面提供的功能操作有:查看代驾订单,删除代驾订单操作,新增代驾订单操作,修改代驾订单操作。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。新闻管理页面,此页面提供给管理员的功能有:新增新闻,修改新闻,删除新闻。新闻类型管理页面,此页面提供给管理员的功能有:新增新闻类型,修改新闻类型,删除新闻类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值