Flex+java servlet处理文件上传

baowei1003 贡献于2010-12-04

作者 Huahua  创建于2010-07-22 14:03:00   修改者Huahua  修改于2010-07-23 05:09:00字数6423

文档摘要:flex中的请求交由服务器中的servlet处理,在提交的URL中需要包括servlet名,然后在服务器中的web.xml中配置对该URL的mapping。java上传文件程序步骤:首先对request和response设置编码格式,以免产生中文乱码,特别是request,一旦有乱码,后台根本就识别不了了,更别说处理。创建一个工厂对象(DiskFileItemFactory类),用于对磁盘的写入。
关键词:

flex中的请求交由服务器中的servlet处理,在提交的URL中需要包括servlet名,然后在服务器中的web.xml中配置对该URL的mapping。 java上传文件程序步骤: 首先对request和response设置编码格式,以免产生中文乱码,特别是request,一旦有乱码,后台根本就识别不了了,更别说处理。 创建一个工厂对象(DiskFileItemFactory类),用于对磁盘的写入。再根据这个工厂对象创建一个上传对象。该上传对象会根据request来 解析文件,生成一个List,在就是对该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: Java Servlet代码,FileUploaded.java: package fileupload; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * Processes requests for both HTTP GET and POST methods. * * * @author huahua */ public class FileUploaded extends HttpServlet { private String uploadPath = "E:\\"; // 定义文件的上传路径 private int MAXFILESIZE = 100 * 1024 * 1024; // 限制文件的上传大小 public FileUploaded() { super(); } public void destroy() { super.destroy(); } // 主处理 protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Access !"); request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); // 保存文件到服务器中 |Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(4096); // 超出这个大小放磁盘,否则放在内存中 ServletFileUpload upload = new ServletFileUpload(factory); // Create a new file upload handler upload.setSizeMax(MAXFILESIZE); try { List fileItems = upload.parseRequest(request); // Parse the request,have a List of file items that you need to process Iterator iter = fileItems.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); // 如果item.isFormField为true,则代表简单域,否则为file域。Field有2种,一种是简单域,一种是file域。 if (!item.isFormField()) { // 获得文件名,包括文件的扩展名 String name = item.getName(); System.out.println(name); try { // "factory.setRepository(new File(path));"可以设置临时目录 item.write(new File(uploadPath + name)); // 写入磁盘。 } catch (Exception e) { e.printStackTrace(); } } } } catch (FileUploadException e) { e.printStackTrace(); System.out.println(e.getMessage() + "结束"); } } /** * Handles the HTTP GET method. * * @param request * servlet请求 * @param response * servlet响应 */ // 如果是GET请求,则调用doGet方法。 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP POST method. * * @param request * servlet请求 * @param response * servlet响应 */ // 如果是POST请求,则调用doPost方法 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ public String getServletInfo() { return "ServletName: FileUploaded. extends: HttpServlet."; } } web.xml中再配置一下: UploadHandler fileupload.FileUploaded UploadHandler /* OK,将Java servlet项目(web project)部署到tomcat服务器上,启动tomcat服务器。最后就是运行flex界面,进行测试了。 参考:http://commons.apache.org/fileupload/streaming.html

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档