zoukankan      html  css  js  c++  java
  • Flex 实现多文件上传之二:后台部分(java)


    http://hi.baidu.com/half_cup_coffee/blog/item/d421001037f02307203f2e7c.html

    在《Flex实现多文件上传之一:前台部分》里介绍了使用flex实现多文件上传,一直没时间把后面的放上去。

    这里说一下后台是如何处理上传文件的,用的是java。

    上传的功能使用了apache的通用jar包,可以在这里下载。

    java代码如下,简单说明下:

    1.设置上传目录,这个目录是指web服务器所在机器的目录
    2.防止乱码的设置
    3.上传功能写在action里,示例直接使用了默认的方法名doPost,如果你要用struts之类的,可以定义自己的方法名称
    Java 代码
    1. package com.nstar.orderexpress.web;  
    2.   
    3. import javax.servlet.http.HttpServlet;  
    4. import org.apache.log4j.Logger;  
    5. import javax.servlet.http.HttpServletRequest;  
    6. import javax.servlet.http.HttpServletResponse;  
    7. import javax.servlet.ServletException;  
    8. import org.apache.commons.fileupload.disk.DiskFileItemFactory;  
    9. import org.apache.commons.fileupload.servlet.ServletFileUpload;  
    10. import java.util.List;  
    11. import org.apache.commons.fileupload.FileUploadException;  
    12. import java.util.Iterator;  
    13. import org.apache.commons.fileupload.FileItem;  
    14. import java.io.File;  
    15. import java.io.UnsupportedEncodingException;  
    16.   
    17. import org.apache.commons.lang.ObjectUtils;  
    18.   
    19. public class FileUploadAction extends HttpServlet{  
    20.     private static Logger logger = Logger.getLogger(FileUploadAction.class.getName());  
    21.   
    22.     private int maxPostSize = 100 * 1024 * 1024;  
    23.     private String uploadPath = "D:\\upload\\";  
    24.   
    25.   
    26.     public FileUploadAction() {  
    27.     }  
    28.   
    29.     //文件上传  
    30.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
    31.         throws ServletException {  
    32.         logger("begin upload");  
    33.         try {  
    34.     request.setCharacterEncoding("UTF-8");//防止文 件名称带有汉字后传到服务器乱码  
    35.    } catch (UnsupportedEncodingException e) {  
    36.     // TODO Auto-generated catch block  
    37.     e.printStackTrace();  
    38.    }  
    39.         response.setContentType("text/html; charset=UTF-8");  
    40.         DiskFileItemFactory factory = new DiskFileItemFactory();  
    41.         factory.setSizeThreshold(1024 * 4);  
    42.         ServletFileUpload upload = new ServletFileUpload(factory);  
    43.         upload.setFileSizeMax(maxPostSize);  
    44.   
    45.         logger("request================"+ObjectUtils.toString(request));  
    46.         List fileItems = null;  
    47.         try {  
    48.             fileItems = upload.parseRequest(request);  
    49.             logger("===================="+ObjectUtils.toString(fileItems));  
    50.             Iterator iter = fileItems.iterator();  
    51.             while (iter.hasNext()) {  
    52.                 FileItem item = (FileItem) iter.next();  
    53.                 log(item.toString());  
    54.                 if (!item.isFormField()) {  
    55.                     String name = item.getName();  
    56.                     logger("file name = "+name);  
    57.                     try {  
    58.                         item.write(new File(uploadPath + name));  
    59.                     }  
    60.                     catch (Exception ex) {  
    61.                         logger(ex.getMessage());  
    62.                     }  
    63.                 }  
    64.             }  
    65.         }  
    66.         catch (FileUploadException ex1) {  
    67.             logger("FileUploadException->"+ex1.getMessage());  
    68.         }  
    69.   
    70.   
    71.     }  
    72.   
    73.     private void logger(String info){  
    74.         System.out.println(info);  
    75.     }  
    76.   
    77. }  
    package com.nstar.orderexpress.web;  import javax.servlet.http.HttpServlet; import org.apache.log4j.Logger; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import java.util.List; import org.apache.commons.fileupload.FileUploadException; import java.util.Iterator; import org.apache.commons.fileupload.FileItem; import java.io.File; import java.io.UnsupportedEncodingException;  import org.apache.commons.lang.ObjectUtils;  public class FileUploadAction extends HttpServlet{     private static Logger logger = Logger.getLogger(FileUploadAction.class.getName());      private int maxPostSize = 100 * 1024 * 1024;     private String uploadPath = "D:\\upload\\";       public FileUploadAction() {     }      //文件上传     public void doPost(HttpServletRequest request, HttpServletResponse response)         throws ServletException {         logger("begin upload");         try {     request.setCharacterEncoding("UTF-8");//防止文件名称带有汉字后传到服务器乱码    } catch (UnsupportedEncodingException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }         response.setContentType("text/html; charset=UTF-8");         DiskFileItemFactory factory = new DiskFileItemFactory();         factory.setSizeThreshold(1024 * 4);         ServletFileUpload upload = new ServletFileUpload(factory);         upload.setFileSizeMax(maxPostSize);          logger("request================"+ObjectUtils.toString(request));         List fileItems = null;         try {             fileItems = upload.parseRequest(request);             logger("===================="+ObjectUtils.toString(fileItems));             Iterator iter = fileItems.iterator();             while (iter.hasNext()) {                 FileItem item = (FileItem) iter.next();                 log(item.toString());                 if (!item.isFormField()) {                     String name = item.getName();                     logger("file name = "+name);                     try {                         item.write(new File(uploadPath + name));                     }                     catch (Exception ex) {                         logger(ex.getMessage());                     }                 }             }         }         catch (FileUploadException ex1) {             logger("FileUploadException->"+ex1.getMessage());         }       }      private void logger(String info){         System.out.println(info);     }  } 


    文件上传基本上就这么多了,可以在此基础上去扩展你想要的功能。
  • 相关阅读:
    svn使用方法
    董事、执行董事、总裁、总经理
    华联、联华、世纪华联、百联
    声明式编程和命令式编程
    NodeJS中的require和import
    JSONP跨站访问
    Camera服务之--架构浅析
    HashMap实现原理分析
    设计模式解读之一: 策略模式
    Java6.0中Comparable接口与Comparator接口详解
  • 原文地址:https://www.cnblogs.com/nianshi/p/1737098.html
Copyright © 2011-2022 走看看