Struts2 上传文件(1)

 参考资料:

http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990674.html

效果图:

src/struts.xml(节选)

 

 1     <!-- 
 2         请求路径 /projectName/upload/upload.do
 3         相当于 namespace + action name
 4         这种方法好,与类的路径相一至。
 5      -->
 6     <package name="upload" namespace="/upload" extends="struts-default">
 7         <!-- <action name="*_upload" class="labratory.fileUpload.Action" method="{1}"> -->
 8         <action name="upload" class="labratory.fileUpload.Action" method="execute">
 9             <result name="success">/WEB-INF/page/message.jsp</result>
10         </action>
11     </package>

WebContent/labratory/testFileUpload.js ( 节选 )

 1 var createUploadForm = function(){
 2     
 3     // http://localhost:8080/ExtJS/docs/index.html#!/api/Ext.form.Panel
 4     Ext.create('Ext.form.Panel', {
 5         title: 'Simple Form',
 6         bodyPadding: 5,
 7         draggable: true ,
 8         collapsible: true ,
 9         resizable: true ,
10         width: 350,
11         margin: 10 ,
12 
13         // The form will submit an AJAX request to this URL when submitted
14         // url: 'save-form.php',
15         // url: '/taskEvaluate/upload2.do' ,
16         // url:  '/taskEvaluate/upload.do' ,
17         url:  '/taskEvaluate/upload/upload.do' ,
18 
19         // Fields will be arranged vertically, stretched to full width
20         layout: 'anchor',
21         defaults: {
22             anchor: '100%'
23         },
24 
25         // The fields
26         defaultType: 'textfield',
27         items: [{
28             fieldLabel: 'First Name',
29             name: 'firstName',
30             value: 'firstName' ,
31             allowBlank: false
32         },{
33             fieldLabel: 'Last Name',
34             name: 'lastName',
35             value: 'lastName' ,
36             allowBlank: false
37         } ,
38         {
39             // http://localhost:8080/ExtJS/docs/index.html#!/api/Ext.form.field.File
40             xtype: 'filefield',
41             // name: 'photo',
42             // name: 'imageFileName' ,
43             name: 'image' ,
44             fieldLabel: 'Photo',
45             labelWidth: 50,
46             msgTarget: 'side',
47             allowBlank: false,
48             anchor: '100%',
49             buttonText: 'Select Photo...'
50         }
51         
52         ],
53 
54         // Reset and Submit buttons
55         buttons: [{
56             text: 'Reset',
57             handler: function() {
58                 this.up('form').getForm().reset();
59             }
60         }, {
61             text: 'Submit',
62             formBind: true, //only enabled once the form is valid
63             disabled: true,
64             handler: function() {
65                 var form = this.up('form').getForm();
66                 if (form.isValid()) {
67                     form.submit({
68                         success: function(form, action) {
69                            Ext.Msg.alert('Success', action.result.msg) ;
70                         },
71                         failure: function(form, action) {
72                             Ext.Msg.alert('Failed', action.result.msg) ;
73                         }
74                     });
75                 }
76             }
77         }],
78         renderTo: Ext.getBody()
79     });
80     
81 } ;

 src/labratory/fileUpload/Action.java

  1 package labratory.fileUpload;
  2 
  3 import java.io.File;
  4 
  5 import org.apache.commons.io.FileUtils;
  6 import org.apache.struts2.ServletActionContext;
  7 
  8 import com.opensymphony.xwork2.ActionContext;
  9 import com.opensymphony.xwork2.ActionSupport;
 10 
 11 @SuppressWarnings("serial")
 12 public class Action extends ActionSupport{
 13     
 14     private File image ; //上传的文件
 15     private String imageFileName; //文件名称
 16     private String imageContentType; //文件类型
 17 
 18     private String firstName ;
 19     private String lastName ;
 20 
 21     public String execute() throws Exception {
 22         
 23         // String realpath = ServletActionContext.getServletContext().getRealPath("/images");
 24         /*
 25          *   注意:目录 WebContent/media 一定要存在,文件将会保存在服务器的这个目录下
 26          *   可以直接指定其它的存在的路径,比如: d:\myImageFilesIsHere
 27          */
 28         String realpath = ServletActionContext.getServletContext().getRealPath("/media");
 29         // D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
 30         System.out.println("realpath: "+realpath );
 31         
 32         if ( image != null ) {
 33             // 保存的文件
 34             File savefile = new File( new File( realpath ), imageFileName );
 35             
 36             // 如果路径不存在则创建
 37             if ( !savefile.getParentFile().exists() )
 38                 savefile.getParentFile().mkdirs();
 39             
 40             // 拷贝文件
 41             FileUtils.copyFile( image, savefile );
 42             
 43             // http://blog.csdn.net/smcfy/article/details/5693481
 44             ActionContext.getContext().put("message", "文件上传成功");
 45         }
 46         return "success";
 47     }
 48 
 49     
 50     
 51     /*
 52      *  - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 53      *  getters and setters
 54      *  - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 55      */    
 56     
 57     public File getImage() {
 58         return image;
 59     }
 60 
 61     public void setImage(File image) {
 62         this.image = image;
 63     }
 64 
 65     public String getImageFileName() {
 66         return imageFileName;
 67     }
 68 
 69     public void setImageFileName(String imageFileName) {
 70         this.imageFileName = imageFileName;
 71     }
 72 
 73     public String getImageContentType() {
 74         return imageContentType;
 75     }
 76 
 77     public void setImageContentType(String imageContentType) {
 78         this.imageContentType = imageContentType;
 79     }
 80 
 81 
 82 
 83     public String getFirstName() {
 84         return firstName;
 85     }
 86 
 87 
 88 
 89     public void setFirstName(String firstName) {
 90         this.firstName = firstName;
 91     }
 92 
 93 
 94 
 95     public String getLastName() {
 96         return lastName;
 97     }
 98 
 99 
100 
101     public void setLastName(String lastName) {
102         this.lastName = lastName;
103     }
104 
105     
106     
107 }

 

 

posted on 2013-05-24 14:26  Livon  阅读(331)  评论(0编辑  收藏  举报

导航