Ext2.1 + 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>

 

 upload.js


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



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">

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章