日期:2014-05-16  浏览次数:20404 次

Extjs 与struts2配合文件上传

?

?

<link rel="stylesheet" type="text/css" href="/ext/docs/resources/ext-all.css" />
<script type="text/javascript" src="/ext/docs/resources/ext-base.js"></script>
<script type="text/javascript" src="/ext/docs/resources/ext-all.js"></script>

?

?

<script type="text/javascript">
Ext.onReady(function(){
	
	Ext.QuickTips.init();
	var form = new Ext.form.FormPanel({
	     baseCls : 'x-plain',
	     labelWidth : 70,
	     fileUpload : true,
	     defaultType : 'textfield',
	     items : [{
		        xtype : 'textfield',
		        fieldLabel : '上传文件名',
		        name : 'userfile',
		        id : 'userfile',
		        inputType : 'file', //输入框类型 
		        blankText : 'File can\'t not empty.',
		        anchor : '100%' // anchor width by percentage
	       }]
	    });
	
	var win = new Ext.Window({
	     title : '照片上传',
	     width : 400,
	     height : 100,
	     minWidth : 300,
	     minHeight : 100,
	     layout : 'fit',
	     plain : true,
	     bodyStyle : 'padding:5px;',
	     buttonAlign : 'center',
	     items : form,
	     buttons : [{
		      text : '上传',
		      handler : function() {
				       if (  form.form.isValid()  ) {
					        if(Ext.getCmp('userfile').getValue() == ''){
					         	Ext.Msg.alert('错误','请选择你要上传的文件');
					         	return;
					        }
					        
					        form.getForm().submit({
						         url : '/aicpa/aicpaTest_uploadFile.action',
						         method : 'POST',
						         success : function(form, action) {
						          	Ext.Msg.alert('Message', action.result.success);
						          	win.close();
						         },
						         failure : function() {
						          	Ext.Msg.alert('Error', 'File upload failure.');
						         }
					        })
				       }
	      }
	     }, {
		      text : '关闭',
		      handler : function() {
		       		win.close();
		      }
	     }]
	    });
	
	
	win.show();
 
	
});

</script>


?

?

? struts2 中文件处理代码

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// 文件
	private File userfile;
	// 文件名字
	private String userfileFileName;
	// 文件类型
	private String userfileContentType;

	/**
	 * 图片文件文件上传
	 * 
	 * @return
	 * @throws Exception
	 */
	public String uploadFile() {
		boolean isSuccess = saveFile(userfile, userfileFileName);
		if (isSuccess == false) {
			return "fail";
		}
		return "success";
	}

	private boolean saveFile(File lecture, String fileName) {
		// String secondPath = savepath.replace("\\", File.separator
		// ).replace("/", File.separator );
		String rootPath = ServletActionContext.getServletContext().getRealPath(
				"/");
		String saveDir = rootPath + "file" + File.separator + File.separator
				+ "images";
		File saveDirFile = new File(saveDir);

		if (!saveDirFile.exists()) {
			saveDirFile.mkdirs();
		}
		BufferedOutputStream bos = null;
		BufferedInputStream bis = null;
		try {
			bis = new BufferedInputStream(new FileInputStream(lecture));
			bos = new BufferedOutputStream(new FileOutputStream(new File(
					saveDir, fileName)));
			byte[] bs = new byte[1024];
			int len;
			while ((len = bis.read(bs, 0, 1024)) > 0) {
				bos.write(bs, 0, len);
			}
			bis.close();
			bos.close();
			lecture.delete();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (bis != null) {
					bis.close();
				}
				if (bos != null) {
					bos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		return true;

	}

	public File getUserfile() {
		return userfile;
	}

	public void setUserfile(File userfile) {
		this.userfile = userfile;
	}

	public String getUserfileFileName() {
		return userfileFileName;
	}

	public void setUserfileFileName(String userf