springboot文件上传

//文件上传
@RequestMapping(value = "/userPhoto/{user}", method = RequestMethod.POST)
    @ResponseBody
    public void userPhoto(@Param("file") MultipartFile file,
                          @Param("user") String user) throws IOException {
        String path = FILEURL + "\\headportrait\\" + user + ".jpg";
        upload(path, file);
    }
    //文件路径
    public  class Constant {
    public final static String FILEURL="C:\\chongwushequFile";
}
    public String upload(String path, MultipartFile file) {

        File dest = new File(path);
        if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        if (!dest.exists()) {
            dest.mkdirs();
        }
        try {
            file.transferTo(dest); //保存文件
            return "ok";
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "no";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "no";
        }
    }
上一篇:C/C++ 对常见字符串库函数的实现


下一篇:网络攻防实验一