• 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;
        }
  • 相关阅读:
    经典布局 ---- 双飞翼
    细嚼浏览器兼容----条件注释判断浏览器版本
    webqq的注册登记和聊天页面--运用jsonp跨域
    Bootstrap框架的要点--栅格系统
    html5橡皮檫特效
    PHP正确获取客户端IP地址
    常用排序算法及Java实现
    Math类中的floor、ceil和round方法
    Java中的动态反射机制和动态代理
    测试
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/12751720.html
Copyright © 2020-2023  润新知