主要是:路径+文件名.后缀
一定要齐全
以下代码,主要看:
tagetFile.getParentFile().mkdirs(); // 先创建目录
tagetFile.createNewFile(); //创建文件
file: accessPath: /file/** #访问文件前缀 uploadFolder: d://uploadFiles/ #上传文件存放路径 /** * 移动端上传图片 * base64字符串转化成图片 */ @RequestMapping(value = "uploadImage", method = RequestMethod.POST) public ApiResult<String> uploadImage(@RequestBody String requestVO) throws Exception { ApiResult<String> result = new ApiResult<String>(); String resultPath = ""; //替换掉base64头部 String imgStr = requestVO.replaceAll("data:image/png;base64,",""); // Base64解码 byte[] b = Base64.decode(imgStr); for (int i = 0; i < b.length; ++i) { if (b[i] < 0) {// 调整异常数据 b[i] += 256; } } String fileName = FileUtil.getFileName(".jpg"); File tagetFile = new File(uploadFolder+fileName); if (!tagetFile.exists()) { try { tagetFile.getParentFile().mkdirs(); // 先创建目录 tagetFile.createNewFile(); //创建文件 } catch (Exception ex) { throw new RuntimeException("The direction creat fail"); } } // 生成jpeg图片 FileOutputStream fout = new FileOutputStream(tagetFile); fout.write(b); fout.flush(); fout.close(); resultPath = accessPath.substring(0, accessPath.lastIndexOf('/') + 1) + fileName; result.setData(resultPath); result.setCodeToSuccessed(); return result; }