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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值