`
solon.jar
  • 浏览: 44411 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

使用apache.commons.fileupload组件来进行文件上传

阅读更多
1. 从www.apache.org的common项目下搞到commons-fileupload.jar 和commons-io.jar,导入项目。

2. jsp页面中,form的属性设置要求为:
<form action="uploadServlet" method="post" enctype="multipart/form-data">
其中,post和multipart/form-data要求必须指定。

3. 创建处理请求的Servlet:
DiskFileItemFactory factory = new DiskFileItemFactory();
		// 获得项目根路径
		String path = request.getRealPath("/");	
		// 设置缓存路径
		factory.setRepository(new File(path));
		// 设置文件缓存到硬盘的大小临界值
		factory.setSizeThreshold(1024 * 1024);

		ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
		try {
			// 指定解析request
			List<FileItem> list = (List<FileItem>)
					servletFileUpload.parseRequest(request);
			for (FileItem item : list)
			{
				// 非文件的表单字段
				if (item.isFormField() == true){
					String filedName = item.getFieldName(); // 获得字段名
					String value = item.getString("UTF-8"); // 获得字段值
					System.out.println("fieldName=" + filedName);
					System.out.println("value=" + value);
					
				}else{ //文件表单字段
					String fieldName = item.getFieldName(); // 获得字段名
					String value = item.getName();			// 获得文件名
					System.out.println("fieldName=" + fieldName);
					System.out.println("file path=" + value);
					// 处理浏览器兼容问题处理
					int index = value.lastIndexOf("\\");
					String fileName = value.substring(index + 1);
					
					item.write(new File(path, fileName));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
 
4. 如果还想要用Ext美化一下客户端的话:
Ext.onReady(function()
{
	var msg = function(title, msg){
        Ext.Msg.show({
            title: title, 
            msg: msg,
            minWidth: 200,
            modal: true,
            icon: Ext.Msg.INFO,
            buttons: Ext.Msg.OK
        });
    };  
	var fp = new Ext.form.FormPanel({
		baseCls: 'x-plain',
        //renderTo: 'fiform',
        fileUpload: true,
        labelWidth: 60,
        url:'uploadServlet',
        defaults:{
        	anchor: '95%',
            allowBlank: false,
            msgTarget: 'side'
        },
        //bodyStyle: 'padding: 10px 10px 0 10px;',
        items: 
        [
	    	{
	           xtype: 'textfield',         
	           fieldLabel: '用户名',         
	           name: 'user',         
	           inputType: 'text'//文件类型   
	    		
	    	},
	    	{
	           xtype: 'textfield',         
	           fieldLabel: '密 码',         
	           name: 'password',         
	           inputType: 'password'//文件类型   
	    		
	    	},
	    	{   
	           height: 23,
	           xtype: 'textfield',         
	           fieldLabel: '文件名',         
	           name: 'file',         
	           inputType: 'file'//文件类型         
	    	}
        ],     
        buttons: [{
            text: '上传',
            handler: function(){
                if(fp.getForm().isValid()){
	                fp.getForm().submit({
	                    waitMsg: 'Uploading your photo...',
	                    success: function(fp, o){
	                    	fp.getForm().reset();
	                        msg('Success', 'cool~~');
	                    },
	                    failure : function(fp, o){
	                        msg('failure', 'pool~~');
	                    }
	                });
                }else{
                	Ext.MessageBox.alert("提示","请填写必要的字段!");
                }
                
            }
        },{
            text: '清空',
            handler: function(){
                fp.getForm().reset();
            }
        }]     
	}); 
	
	var window = new Ext.Window({
        title: 'Resize Me',
        width: 500,
        autoHeight: true,
        minWidth: 300,
        layout: 'fit',
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        items: fp
    });

    window.show();
});	
 
效果如图:

 
 
  • 大小: 7.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics