zoukankan      html  css  js  c++  java
  • FileUploadTool

    package com.second.util;

    import java.io.File;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.UUID;

    import javax.servlet.http.HttpServletRequest;

    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;

    public class FileUpload {
    /**
    * 请下载lib包 commons2.2&1.3.rar
    * @param req
    * 请求对象
    * @param dir
    * 用于放置文件的文件夹名称
    */
    public static Map<String,Object> upload(HttpServletRequest req, String dir) {
    Map<String,Object> params = new HashMap<String, Object>();
    // 生成磁盘(文件项)工厂(用于获得临时目录)
    DiskFileItemFactory factory = new DiskFileItemFactory();
    String path = req.getServletContext().getRealPath(dir);// 得到放置文件的真实路径
    // 指定需要放置文件的真实目录,用于放置上传文件时所生成的.tem(临时)文件
    factory.setRepository(new File(path));
    // 设置读取文件时的缓存大小
    factory.setSizeThreshold(1024 * 1024);
    // 高水平的API文件上传处理
    ServletFileUpload upload = new ServletFileUpload(factory);
    String lastPropertyName = "";
    String lastPropertyValue = "";
    Set<String> strs = new HashSet<String>();
    try {
    // FileItem 就是临时文件对象,每一个表单元素对应一个临时文件对象,分离文本表单和上传文件
    List<FileItem> list = upload.parseRequest(req);
    for (FileItem item : list) {
    String propertyName = item.getFieldName();// 获取表单提交的属性名称
    String contentType = item.getContentType();
    // item.isFormField() 判断临时文件是否就是一个普通的文本信息
    if (!item.isFormField() && "image/png".equals(contentType)) {
    // 获取到上传文件的文件名
    String itemName = item.getName();
    int index = itemName.lastIndexOf(".");// 找到文件名中.的位置
    String suffix = itemName.substring(index);
    // 自己处理文件的名称,格式按照: XXX.XX
    String fileName = UUID.randomUUID().toString();
    fileName += suffix;

    params.put(propertyName, fileName);//将Form表单元素放入Map键值对中
    // 从临时文件对象中读取字节流
    item.getInputStream();
    String filePath = path + "\" + fileName;
    File file = new File(filePath);
    item.write(file);
    } else {
    String propertyValue = item.getString();
    //如果上次属性名与本次属性一致,就是数组
    if(lastPropertyName.equals(propertyName)){
    strs.add(lastPropertyValue);//将上一次的值同样重复赋值给SET,利用Set的不重复做到区分
    strs.add(propertyValue);
    params.put(propertyName, strs);//将Form表单元素放入Map键值对中
    }else{
    propertyValue = new String(
    propertyValue.getBytes("ISO8859-1"), "utf-8");
    params.put(propertyName, propertyValue);//将Form表单元素放入Map键值对中
    }
    lastPropertyValue = propertyValue;
    }

    lastPropertyName = propertyName;

    }
    } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    }
    return params;
    }
    }

  • 相关阅读:
    C#中实现简单的预警提示功能(语音提示与弹窗提示)
    C#中使用SoundPlayer播放音频文件(wav文件)
    Angular中路由的嵌套-父子路由
    Winform中设置ZedGraph的坐标轴的标题和刻度不显示十次幂
    Angular中使用JS实现路由跳转、动态路由传值、get方式传值
    surprise库官方文档分析(二):使用预测算法
    surprise库官方文档分析(一)
    webpack官方文档分析(三):Entry Points详解
    webpack官方文档分析(二):概念
    webpack官方文档分析(一):安装
  • 原文地址:https://www.cnblogs.com/VCandy/p/5475533.html
Copyright © 2011-2022 走看看