




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 mons.fileupload.FileItem;import mons.fileupload.FileUploadException;import mons.fileupload.disk.DiskFileItemFactory;import mons.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 itemsDiskFileItemFactory factory = new DiskFileItemFactory();factory.setSizeThreshold(4096); / 超出这个大小放磁盘,否则放在内存中ServletFileUpload upload = new ServletFileUpload(factory); / Create a new file upload handlerupload.setSizeMax(MAXFILESIZE);try List fileItems = upload.parseRequest(request); / Parse the request,have a List of file items that you need to processIterator 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.;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 品质部主管工作总结
- 颅脑外伤急诊抢救护理
- 山东省淄博市2025-2026学年高三上学期开学考试语文试题及参考答案
- 数学教师培训交流
- 代理记账工作总结
- 2026届重庆市荣昌区化学九上期中教学质量检测模拟试题含解析
- 数学建模博弈论析
- 2026届湖北省荆州市松滋市化学九年级第一学期期中综合测试模拟试题含解析
- 建筑行业个人工作总结
- 2026届山东省青岛市崂山区化学九年级第一学期期中复习检测模拟试题含解析
- 制鞋工岗前考核试卷及答案
- (2025年标准)课时合同转让协议书
- 郑州市突发应急预案
- 2025广东肇庆市怀集县卫生事业单位招聘102人考试备考试题及答案解析
- 原发性胆汁性胆管炎的诊断和治疗课件
- 中医医疗技术相关性感染防控考试卷(附答案)
- 2025强制执行申请书(范文模板)
- 风力发电机自动消防系统
- 公益性岗位业务培训课件
- 时光老人与流浪汉课件
- 政务中心面试常见问题与答案详解
评论
0/150
提交评论