• 单例模式和文件上传


    单例模式

      

    //读取配置文件的工具类-单例模式
    public class ConfigManager {
        private static Properties properties;
        private static ConfigManager configManager;
    
        // 私有构造器-读取数据库配置文件
        private ConfigManager() {
            properties = new Properties();
            String configFile = "database.properties";
            InputStream is = ConfigManager.class.getClassLoader()
                    .getResourceAsStream(configFile);
            try {
                properties.load(is);
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        // 全局访问点
        public static ConfigManager getInstance() {
            if (configManager == null) {
                configManager = new ConfigManager();
            }
            return configManager;
        }
    
        public String getValue(String key) {
            return properties.getProperty(key);
        }
    
    }

     懒汉模式

      

     饿汉模式

      

      

    Spring MVC-Controller的单例管理

      


    文件上传

     单文件上传

      

      

      

      

        @RequestMapping(value = "/useradd.do")
        public String addUser(
                User user,
                @RequestParam(value = "a_idPicPath", required = false) MultipartFile file,
                HttpSession session, HttpServletRequest request) {
            String idPicPath = null;
            // 判断文件是否为空
            if (!file.isEmpty()) {
                // 定义目标上传路径
                String path = request.getSession().getServletContext()
                        .getRealPath("statics" + File.separator + "uploadfiles");
                String oldFileName = file.getOriginalFilename();
                String suffix = FilenameUtils.getExtension(oldFileName);
                int filesize = 500000;
                if (file.getSize() > filesize) {
                    request.setAttribute("uploadFileError", "* 上传大小不得超过500K");
                    return "useradd";
                } else if (suffix.equalsIgnoreCase("jpg")
                        || suffix.equalsIgnoreCase("jpeg")
                        || suffix.equalsIgnoreCase("png")
                        || suffix.equalsIgnoreCase("pneg")) {
                    // 当前系统时间+随机数+"_Personal.jpg"
                    String fileName = System.currentTimeMillis()
                            + RandomUtils.nextInt(1000000) + "_Personal.jpg";
                    File targetFile = new File(path, fileName);
                    if (!targetFile.exists()) {
                        targetFile.mkdirs();
                    }
                    try {
                        file.transferTo(targetFile);
                    } catch (Exception e) {
                        e.printStackTrace();
                        request.setAttribute("uploadFileError", "* 上传失败");
                        return "useradd";
                    }
                    idPicPath = path + File.separator + fileName;
                } else {
                    request.setAttribute("uploadFileError", "* 上传图片格式不正确");
                    return "useradd";
                }
            }
    
            user.setCreatedBy(((User) session.getAttribute(Constants.USER_SESSION))
                    .getId());
            user.setCreationDate(new Date());
            user.setIdPicPath(idPicPath);
            if (userService.add(user)) {
                return "redirect:/user/userlist.do";
            }
            return "useradd";
        }

     多文件上传

      

      

      

  • 相关阅读:
    Delphi StrUtils-字符串函数RightStr、MidStr、LeftStr
    Delphi 错误:Could not convert variant to type(Null) into type (String)
    Delphi Variants-VarIsEmpty、VarIsNull 判断Variant变量是否为空、是否包含Null值
    Python使用openpyxl读写excel文件
    CentOS7设置为局域网固定ip
    Linux下ps aux命令中STAT的参数含义(转)
    Python生成8位随机字符串的方法分析
    php源码加密方法详解
    普通程序员 与 大牛 的区别 ???
    开始记录前端学习过程中的点点滴滴
  • 原文地址:https://www.cnblogs.com/xhddbky/p/9456132.html
Copyright © 2020-2023  润新知