Flex文件上传

使用Flex + Java实现文件上传

 

Flex主要使用FileReference,代码如下

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
	creationComplete="init()">
	
	<mx:Script>
		[CDATA[
			
			private const defaultRequestUrl : String = "http://localhost:8080/flexFileUploadServer/uploadServlet.do";
			
			private var file : FileReference;
			
			private function init():void {
				Security.allowDomain("*");
				
				file = new FileReference();
				file.addEventListener(Event.SELECT, onFileSelect);
				file.addEventListener(ProgressEvent.PROGRESS, progressHandle);
				file.addEventListener(Event.COMPLETE, completeHandle);
				//file.addEventListener(Event.OPEN, openHandle);
				//file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				//file.addEventListener(Event.CANCEL, cancelHandler);
			}
			
			private function onClickBrowserBtn() : void {
				file.browse(getTypeFilter());
			}
			
			private function getTypeFilter() : Array {
				var imagesFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
				//var docFilter:FileFilter = new FileFilter("Documents", "*.pdf;*.doc;*.txt");
				
				return [imagesFilter];
			}
			
			private function onFileSelect(event : Event) : void {
				uploadBtn.enabled = true;
				infoText.htmlText = 
					"Name: " + file.name + "<br/>" + 
					"Size: " + file.size + "<br/>" + 
					"Type: " + file.type + "<br/>" + 
					"Date: " + file.creationDate;
			}
			
			private function onClickUploadBtn() : void {
				var request : URLRequest = new URLRequest(defaultRequestUrl);
				request.data = "userId=123";
				file.upload(request);
			}
			
			private function progressHandle(event : ProgressEvent) : void {
				progressLabel.text = "complete " + event.bytesLoaded + " bytes";
				var fileUploadPercent : uint = event.bytesLoaded / event.bytesTotal * 100;
				uploadProgressBar.setProgress(fileUploadPercent, 100);
				uploadProgressBar.label = "Complete " + fileUploadPercent + "%";
			}
			
			private function completeHandle(event : Event) : void {
				infoText.htmlText = "Upload " + file.name + " Complete!";
				uploadBtn.enabled = false;
			}
		]]
	</mx:Script>
	
	<mx:Button id="browserBtn" x="10" y="69" label="Browser" 
		click="onClickBrowserBtn()"/>
		
	<mx:Button id="uploadBtn" x="236" y="69" label="Upload" enabled="false" 
		click="onClickUploadBtn()"/>
		
	<mx:ProgressBar id="uploadProgressBar" x="10" y="33" width="291" 
		themeColor="#009dff" maximum="100" direction="right" mode="manual"/>
		
	<mx:TextArea id="infoText" x="10" y="99" width="291" height="131"/>
	<mx:Label id="progressLabel" x="10" y="10" width="291"/>
	
</mx:Application>

 

 

Java使用Spring的MultipartHttpServletRequest, 代码如下

	public void saveFile(HttpServletRequest request, HttpServletResponse response) 
		throws IllegalStateException, IOException {
		
		CommonsMultipartResolver commonsMultipartResolver = 
			new CommonsMultipartResolver(request.getSession().getServletContext());
		commonsMultipartResolver.setDefaultEncoding("utf-8");
		
		if (commonsMultipartResolver.isMultipart(request)) {  
			MultipartHttpServletRequest multipartRequest = 
				commonsMultipartResolver.resolveMultipart(request);  

			Iterator iter = multipartRequest.getFileNames();
			for (;iter.hasNext();) {
				MultipartFile file = multipartRequest.getFile((String)iter.next());
				if (file != null) {
					File localFile = new File(file.getOriginalFilename());
					file.transferTo(localFile);
				}
			} 
			System.out.println("userId: " + request.getParameter("userId"));
		}
	}

 

当然也可以采用纯commons-fileupload-1.1.1.jar的实现代码,这个网上例子很多,这里就不写了。

 

参考

http://www.cnblogs.com/dannyr/archive/2006/11/13/559006.html

http://swingchen.bokee.com/5943371.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值