• Java一个文件上传工具类


    /**
     * 文件上传
     * 
     * @author cary
     * @since 2012-12-19 下午2:22:12
     */
    public class FileUploader {
        static final String UPLOAD_PATH_KEY = "upload.dir";
        private static Logger logger = Logger.getLogger(FileUploader.class);
    
        /**
         * 定义允许上传的文件扩展名
         */
        private Map<String, String> extMap = new HashMap<String, String>();
        /**
         * 文件上传目录
         */
        private String savePath;
        /**
         * 文件完整目录
         */
        private String saveUrl;
    
        /**
         * 空构造器
         */
        public FileUploader() {
            // 允许上传的文件类型
            extMap.put("extName", "gif,jpg,jpeg,png,bmp,xls,xlsx");
        }
    
        /**
         * @param request 请求对象
         * @param upload 被上传的文件
         * @param uploadFileName 被上传的文件名
         * @param dirName 文件类型对应的路径
         * @param maxSize 文件大小阈值
         * @return
         * @throws IOException
         */
        public String[] uploadFile(HttpServletRequest request, File upload, String uploadFileName, String dirName,
                Long maxSize) throws IOException {
            /**
             * 保存上传文件返回信息
             */
            String[] infos = new String[2];
            /**
             * 获取文件上传目录
             */
            String extName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1, uploadFileName.length());
            infos[0] = this.validateFields(request, extName, dirName, maxSize);
            String prefixName = uploadFileName.substring(0, uploadFileName.lastIndexOf("."));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhh24mmss");
            String ymd = sdf.format(new Date());
            String realName = prefixName + ymd + "." + extName;
            /**
             * 获取文件全路径
             */
            saveUrl = savePath + realName;
            if (infos[0].equals("true")) {
                saveFile(upload, savePath, realName);
                infos[1] = saveUrl;
            }
            return infos;
        }
    
        /**
         * 上传验证,并初始化文件目录
         * 
         * @param request 请求对象
         * @param extName 扩展名
         * @param dirName 子目录名
         * @param maxSize 文件大小阈值
         * @throws IOException
         */
        private String validateFields(HttpServletRequest request, String extName, String dirName, Long maxSize)
                throws IOException {
            String errorInfo = "true";
            // 获取内容类型
            String contentType = request.getContentType();
            String extType = extMap.get("extName");
            int contentLength = request.getContentLength();
            String path = ConfigUtil.getString(FileUploader.UPLOAD_PATH_KEY);
            savePath = path + "/" + dirName + "/";
            File uploadDir = new File(savePath);
            if (contentType == null || !contentType.startsWith("multipart")) {
                logger.error("请求不包含multipart/form-data流");
                return "请求不包含multipart/form-data流";
            }
            if (extType.indexOf(extName) == -1) {
                return "不允许上传的文件类型[" + "." + extName + "]";
            }
            if (maxSize < contentLength) {
                logger.error("上传文件大小超出文件最大大小");
                return "上传文件大小超出文件最大大小[" + maxSize + "]";
            }
            if (!ServletFileUpload.isMultipartContent(request)) {
                return errorInfo = "请选择文件";
            }
            if (!uploadDir.exists()) {// 检查目录
                logger.warn("上传目录[" + savePath + "]不存在");
                uploadDir.mkdir();
            }
            if (!uploadDir.canWrite()) {
                return "上传目录[" + savePath + "]没有写权限";
            }
            return errorInfo;
        }
    
        /**
         * 保存文件
         * 
         * @param upload 待上传文件
         * @param savePath 文件存储目录
         * @param realName 原始文件名
         * @throws IOException
         */
        public void saveFile(File upload, String savePath, String realName) throws IOException {
            File target = new File(savePath, realName);
            FileUtils.copyFile(upload, target);
        }
    
        public static void main(String[] args) {
            File dirFile = new File("/usr/local/upload/excel");
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }
        }
    2.action调用代码

    FileUploader fileUploader = new FileUploader();

      try {
      Long maxSize = getMaxSize();
      String[] infos = fileUploader.uploadFile(getRequest(), upload, uploadFileName, EXCEL_DIR, maxSize);
      logger.info("upload info:" + infos[0]);
      logger.info("upload path:" + infos[1]);

    }catch (IOException e) {

      logger.error("上传文件发生异常" + e);

    }

     

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    idea spring boot 1.x junit单元测试
    linux oracle/jdk启用大页面
    jdk8之CompletableFuture与CompletionService
    gc日志深入解析-覆盖CMS、并行GC、G1、ZGC、openj9
    h2 web console使用
    LockSupport工具类详解
    反射、Unsafe、直接调用性能大比拼
    spring boot druid动态多数据源监控集成
    Linux网络
    org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection总结
  • 原文地址:https://www.cnblogs.com/weiguo21/p/4824012.html
Copyright © 2020-2023  润新知