- 2
- 0
- 约7.47千字
- 约 6页
- 2018-05-11 发布于浙江
- 举报
FlexJavaServlet处理文件上传汇
flex中的请求交由服务器中的servlet处理,在提交的URL中需要包括servlet名,然后在服务器中的web.xml中配置对该URL的mapping。
java上传文件程序步骤:
首先对request和response设置编码格式,以免产生中文乱码,特别是request,一旦有乱码,后台根本就识别不了了,更别说处理。
创建一个工厂对象(DiskFileItemFactory类),用于对磁盘的写入。再根据这个工厂对象创建一个上传对象。该上传对象会根据request来
解析文件,生成一个ListFileItem,在就是对该List进行循环处理,通过List中对象的write方法将数据写入磁盘。操作结束。
// Check that we have a file upload request 判断是否是文件上传请求
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
The simplest usage scenario of uploading is the following:
1. Uploaded items should be retained in memory as long as they are reasonably small.
2. Larger items should be written to a temporary file on disk.
3. Very large upload requests should not be permitted.
4. The built-in defaults for the maximum size of an item to be retained in memory, the maximum permitted size of an upload request, and the location of temporary files are acceptable.
Flex负责前台界面,Java则负责前台传递过来的请求(即文件上传请求)。这里只是一个简单的例子,供大家入门用哈。有关Flex和Java servlet方面的知识这里就不列出来了,没有整理,大家自己找资料吧。
前提:要从网上下载这两个jar包,commons-fileupload-1.2.jar和commons-io-1.4.jar。然后在MyEclipse中配置Build Path,将这两个包包含进去。
直接贴代码吧:
Flex代码,upload.mxml:
?xml version=1.0 encoding=utf-8?
s:Application xmlns:fx=/mxml/2009
xmlns:s=library:///flex/spark
xmlns:mx=library:///flex/mx minWidth=955 minHeight=600 creationComplete=init();
s:layout
s:BasicLayout/
/s:layout
fx:Declarations
!-- 将非可视元素(例如服务、值对象)放在此处 --
/fx:Declarations
mx:Button label=上传文件 click=myupload() x=85 y=106/
mx:Label text=文件上传 x=84 y=27/
mx:ProgressBar minimum=0 maximum=100 mode=manual id=progress1 label=当前进度:0% x=84 y=65/
mx:Label id=lbProgress x=87 y=142/
fx:Script


文档评论(0)