springmvc上传文件

public void fileUpload(HttpServletRequest request,MultipartFile upload){
        System.out.println("文件上传");
        //上传位置
        String realPath = request.getSession().getServletContext().getRealPath("/uploads/");
        //判断该路径是否存在
        File file = new File(realPath);
        if(!file.exists()){
            //如果不存在创建文件夹
            file.mkdirs();
        }
        //文件上传项
        //获取上传文件名
        String fileName = upload.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        fileName = uuid+"_"+fileName;
        //完成文件上传
        try {
            upload.transferTo(new File(realPath,fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章