Spring MVC 实现文件上传功能

来源:互联网 发布:信捷usb编程电缆和三菱 编辑:程序博客网 时间:2024/06/05 15:16
@RequestMapping(value="/fileBusinessLicensePath", method=RequestMethod.POST)
    @ResponseBody
    public ResultInfo uploadBusinessLicensePath(@RequestParam("files[]") MultipartFile file){
        Files files = new Files();
        String names = file.getOriginalFilename();
        files.setFileRealName(names);
        names = new Date().getTime() + names.substring(names.indexOf("."));
        String destFileName = "";
        String destFilePath = ParamFileUtil.getConfigProperty("config","business_license") + names;
        destFileName = ParamFileUtil.getConfigProperty("config","business_license_real") + System.getProperty("file.separator") + names;


        if(createFile(destFileName,file)){
            files.setFilePath(destFilePath);
        } else {
            return null;
        }
        Map<String,Object> result = new HashMap<>();
        result.put("file", files);
        return new ResultInfo(Constant.SUCCESS_CODE,Constant.SUCCESS,result);

    }


 /**
     * 保存文件
     * @param destFileName
     * @return
     */
    public static boolean createFile(String destFileName,MultipartFile files) {
        File file = new File(destFileName);
        if(file.exists()) {
            System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
            return false;
        }
        if (destFileName.endsWith(File.separator)) {
            System.out.println("创建单个文件" + destFileName + "失败,目标文件不能为目录!");
            return false;
        }
        //判断目标文件所在的目录是否存在
        if(!file.getParentFile().exists()) {
            //如果目标文件所在的目录不存在,则创建父目录
            System.out.println("目标文件所在目录不存在,准备创建它!");
            if(!file.getParentFile().mkdirs()) {
                System.out.println("创建目标文件所在目录失败!");
                return false;
            }
        }
        //创建目标文件
        try {
            if (file.createNewFile()) {
                System.out.println("创建单个文件" + destFileName + "成功!");
                files.transferTo(file);
                return true;
            } else {
                System.out.println("创建单个文件" + destFileName + "失败!");
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("创建单个文件" + destFileName + "失败!" + e.getMessage());
            return false;
        }
    }


//获取配置文件中的数据

public class ParamFileUtil {
/** Properties对象 */
    private Properties prop;


    public static Properties loadPropertiesFromBundlefile(String bundlefile) {
        return loadPropertiesFromBundlefile(bundlefile, new Properties());
    }

public static Properties loadPropertiesFromBundlefile(String bundlefile, Properties toprop) {
        Properties properties = toprop;
        try {
            ResourceBundle rb = ResourceBundle.getBundle(bundlefile);
            Enumeration enu = rb.getKeys();
            while (enu.hasMoreElements()) {
                String strPName = (String) enu.nextElement();
                String strPValue = null;
                if (strPName != null) {
                    strPValue = rb.getString(strPName);
                }
                if ((strPName != null) && (strPValue != null))
                    properties.setProperty(strPName, strPValue);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return properties;
    }

 public static String getConfigProperty(String propertyName,String propertyValue){
        Properties prop = loadPropertiesFromBundlefile(propertyName);
        return prop.getProperty(propertyValue);
    }

}












0 0
原创粉丝点击