zoukankan      html  css  js  c++  java
  • ServletFileUpload(Servlet文件上传)

     1 //**文件上传**    form表单提交必须指定Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型"multipart/form-data"
     2             //1.创建磁盘文件项目工厂
     3         DiskFileItemFactory df=new DiskFileItemFactory();
     4                 //2.设置上传的内存缓存区大小    4096=4KB
     5             df.setSizeThreshold(4096);
     6             //3.实列化Servlet文件上传对象    把'磁盘文件项目工厂'放入构造中(把缓存区大小放进去)
     7         ServletFileUpload upload=new ServletFileUpload(df);
     8             try {
     9                 //4.得到所有的上传表单对象集合    返回List<FileItem> 相当于每一个表单元素都是一个FileItem
    10                 List<FileItem> itemList=upload.parseRequest(request);
    11                     //6.获取文件夹的绝对路径
    12                 String path=request.getSession().getServletContext().getRealPath("\upload");
    13                 
    14                     //5.遍历表单对象集合
    15                 String ntid="";
    16                 String ntitle="";
    17                 String nauthor="";
    18                 String nsummary="";
    19                 String ncontent="";
    20                 String file="";
    21                 String name="";
    22                     for(FileItem item:itemList){
    23 //                        System.out.println("InMemory:"+item.isInMemory());判断FileItem对象是否是一个简单的表单字段
    24                         
    25                         //取出用户提交内容
    26                         if(item.isFormField()){//判断    item是否是简单的表单字段
    27                             if(item.getFieldName().equals("ntid")){
    28                                 ntid=item.getString("utf-8");
    29                             }else if(item.getFieldName().equals("ntitle")){
    30                                 ntitle=item.getString("utf-8");
    31                             }else if(item.getFieldName().equals("nauthor")){
    32                                 nauthor=item.getString("utf-8");
    33                             }else if(item.getFieldName().equals("nsummary")){
    34                                 nsummary=item.getString("utf-8");
    35                             }else if(item.getFieldName().equals("ncontent")){
    36                                 file=item.getString("utf-8");
    37                             }
    38                         }else{
    39                             if(item.getFieldName().equals("file")){
    40                                 //得到用户上传文件的路径
    41                                 String fileName=item.getName();
    42                                 //截取最后文件的名字
    43                                 int index=fileName.lastIndexOf("\");
    44                                 name=fileName.substring(index+1);
    45                                 
    46                                 try {
    47                                     //把文件写入服务器文件夹
    48                                     item.write(new File(path,name));
    49                                 } catch (Exception e) {
    50                                     e.printStackTrace();
    51                                 }
    52                             }
    53                         }
    54                     }
    55             } catch (FileUploadException e) {
    56                 /*    form表单中中没有设置
    57                     encType="multipart/form-data" method="post"
    58                     会造成此异常(FileUploadBase$InvalidContentTypeException)
    59                 */
    60                 e.printStackTrace();
    61             }

    需要:

      commons-fileupload.jar  commons-io.jar  两个插件

  • 相关阅读:
    编程语言的进化
    面向对象
    面向对象oop
    .NET——内存分配
    使用IIS承载WCF服务
    .NET代码生成器ASP.NET Factory 开放所有源代码下载
    SyntaxHighlighter
    写代码的三重境界
    Wijmo jQuery 插件集
    给vs2012换肤
  • 原文地址:https://www.cnblogs.com/wkrbky/p/5743117.html
Copyright © 2011-2022 走看看