zoukankan      html  css  js  c++  java
  • base64文件上传 java.io.FileNotFoundException 拒绝访问

    主要是:路径+文件名.后缀

    一定要齐全

    以下代码,主要看:

    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;
        }
  • 相关阅读:
    一、业务场景-随机生成患者姓名
    十一、python的高级语法与用法
    全排列小结
    LeetCode——150. Evaluate Reverse Polish Notation
    斐波那契数列算法小结
    LeetCode——14. Longest Common Prefix
    LeetCode——13. Roman to Integer
    LeetCode——12. Integer to Roman
    LeetCode——11. Container With Most Water
    LeetCode——10. Regular Expression Matching
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/12751720.html
Copyright © 2011-2022 走看看