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();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章