转自:http://wangcheng.iteye.com/blog/175146

 

使用Flex + Java实现文件上传

 

Flex主要使用FileReference,代码如下

Xml代码 复制代码 收藏代码
  1. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
  2. creationComplete="init()">
  3. <mx:Script>
  4. [CDATA[
  5. private const defaultRequestUrl : String = "http://localhost:8080/flexFileUploadServer/uploadServlet.do";
  6. private var file : FileReference;
  7. private function init():void {
  8. Security.allowDomain("*");
  9. file = new FileReference();
  10. file.addEventListener(Event.SELECT, onFileSelect);
  11. file.addEventListener(ProgressEvent.PROGRESS, progressHandle);
  12. file.addEventListener(Event.COMPLETE, completeHandle);
  13. //file.addEventListener(Event.OPEN, openHandle);
  14. //file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
  15. //file.addEventListener(Event.CANCEL, cancelHandler);
  16. }
  17. private function onClickBrowserBtn() : void {
  18. file.browse(getTypeFilter());
  19. }
  20. private function getTypeFilter() : Array {
  21. var imagesFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
  22. //var docFilter:FileFilter = new FileFilter("Documents", "*.pdf;*.doc;*.txt");
  23. return [imagesFilter];
  24. }
  25. private function onFileSelect(event : Event) : void {
  26. uploadBtn.enabled = true;
  27. infoText.htmlText =
  28. "Name: " + file.name + "<br/>" +
  29. "Size: " + file.size + "<br/>" +
  30. "Type: " + file.type + "<br/>" +
  31. "Date: " + file.creationDate;
  32. }
  33. private function onClickUploadBtn() : void {
  34. var request : URLRequest = new URLRequest(defaultRequestUrl);
  35. request.data = "userId=123";
  36. file.upload(request);
  37. }
  38. private function progressHandle(event : ProgressEvent) : void {
  39. progressLabel.text = "complete " + event.bytesLoaded + " bytes";
  40. var fileUploadPercent : uint = event.bytesLoaded / event.bytesTotal * 100;
  41. uploadProgressBar.setProgress(fileUploadPercent, 100);
  42. uploadProgressBar.label = "Complete " + fileUploadPercent + "%";
  43. }
  44. private function completeHandle(event : Event) : void {
  45. infoText.htmlText = "Upload " + file.name + " Complete!";
  46. uploadBtn.enabled = false;
  47. }
  48. ]]
  49. </mx:Script>
  50. <mx:Button id="browserBtn" x="10" y="69" label="Browser"
  51. click="onClickBrowserBtn()"/>
  52. <mx:Button id="uploadBtn" x="236" y="69" label="Upload" enabled="false"
  53. click="onClickUploadBtn()"/>
  54. <mx:ProgressBar id="uploadProgressBar" x="10" y="33" width="291"
  55. themeColor="#009dff" maximum="100" direction="right" mode="manual"/>
  56. <mx:TextArea id="infoText" x="10" y="99" width="291" height="131"/>
  57. <mx:Label id="progressLabel" x="10" y="10" width="291"/>
  58. </mx:Application>
<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, 代码如下

Java代码 复制代码 收藏代码
  1. public void saveFile(HttpServletRequest request, HttpServletResponse response)
  2. throws IllegalStateException, IOException {
  3. CommonsMultipartResolver commonsMultipartResolver =
  4. new CommonsMultipartResolver(request.getSession().getServletContext());
  5. commonsMultipartResolver.setDefaultEncoding("utf-8");
  6. if (commonsMultipartResolver.isMultipart(request)) {
  7. MultipartHttpServletRequest multipartRequest =
  8. commonsMultipartResolver.resolveMultipart(request);
  9. Iterator iter = multipartRequest.getFileNames();
  10. for (;iter.hasNext();) {
  11. MultipartFile file = multipartRequest.getFile((String)iter.next());
  12. if (file != null) {
  13. File localFile = new File(file.getOriginalFilename());
  14. file.transferTo(localFile);
  15. }
  16. }
  17. System.out.println("userId: " + request.getParameter("userId"));
  18. }
  19. }
	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