解决图片上传时保存信息:java.lang.NullPointerException: null 空指针异常解决

ERROR 73168 — [ XNIO-1 task-28] c.u.l.c.e.GlobalExceptionHandler : 异常信息uri=/sys/WxMainHome/saveEditIn,method=POST,e={}

解决办法(报错信息下)

报错信息: 找到自己写的代码

java.lang.NullPointerException: null
at io.undertow.servlet.spec.PartImpl.write(PartImpl.java:104)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile.transferTo(StandardMultipartHttpServletRequest.java:255)

at com.ultrapower.life.admin.util.FileUtils.save(FileUtils.java:21)

at com.ultrapower.life.admin.service.impl.WxMainHomeServiceImpl.saveInfo(WxMainHomeServiceImpl.java:133)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy114.saveInfo(Unknown Source)

at com.ultrapower.life.admin.controller.WxMainHomeController.saveEditIn(WxMainHomeController.java:185)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)

一、 看提示的代码信息 ,找到提升代码的地方

(加粗的就是,我自己设置的,在idea里会有自动的颜色标出,仔细看很容易)

二、 看代码

1.FileUtils 工具类

public class FileUtils {
    // /上传
    public static String save(String dir, MultipartFile file) {
        if (file.isEmpty()) {
            return null;
        }
        String fileName = getPicRandomName(file.getOriginalFilename());
        File upload = getFileDir(dir);

        File dest = new File(upload,fileName);

        try {
            file.transferTo(dest);
            return Constant.imageUrl + dir+"/" + fileName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getPicRandomName(String picName) {
        String name = "";
        String randomName = RandomValues.getRandoms(16);
        int index = picName.lastIndexOf(".");
        if(index<0)
            return randomName+".png";
        String picType = picName.substring(index);
        name = randomName + picType;
        return name;
    }

    public static File getFileDir(String id) {

        String childPath="/";
        String filePath = Constant.imageFilePath;
        if (StringUtils.isNotBlank(id)){
            childPath = id+"/";
        }
        //如果上传目录为/static/images/upload/,则可以如下获取
        File upload=new File(filePath,childPath);
        if(!upload.exists()){
            upload.mkdirs();
        }
        System.out.println(upload.getAbsolutePath());
        return upload;
    }

}

没问题

2.saveInfo 方法

/**
     * 保存首页图片
     * @param files
     * @param wxMainHome
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean saveInfo(MultipartFile files, WxMainHome wxMainHome) {
        String mainLogo = "";
        // 上传图片名
        String originalFileName = "";
        if (files != null){
            try {
                // 生成新的文件名
                byte[] fileBytes =files.getBytes();
                // 取得当前文件名
                originalFileName = files.getOriginalFilename();
                // 生成文件名
                if (originalFileName.contains("mainLogo")){
                    String save = FileUtils.save(mainLogo,files);
                    wxMainHome.setMainLogo(save);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 保存图片后自动更新时间
        wxMainHome.setUpdateTime(LocalDateTime.now());
        boolean flag = wxMainHomeMapper.saveInfo(wxMainHome);
        return flag;
    }

没问题

3. 在看看controller层

/**
     * 对编辑内页保存
     * @param file
     * @param wxMainHome
     * @param wxMainDetail
     * @return
     */
    @RequiresPermissions("sys:WxMainHome:editIn")
    @PostMapping("/saveEditIn")
    @ResponseBody
    public R saveEditIn(@RequestParam(value = "file",required = false) MultipartFile file, WxMainHome wxMainHome,
                        WxMainDetail wxMainDetail){
        // 在详情页中新建宣传内页时,若homeId为空,在详情表中插入相应的字段
        if (wxMainDetail.getHomeId() == null){
            wxMainDetail.setHomeId(wxMainHome.getId());
            wxMainDetail.setSortOrder(1);
            wxMainDetailService.insertId(wxMainDetail);
        }
        // 保存(内页编辑)首页
        boolean flag = wxMainHomeService.saveInfo(file,wxMainHome);
        // 保存(内页编辑)详情页
        wxMainDetailService.saveInfo(file,wxMainDetail);
        if (flag) {
            return R.ok();
        } else {
            return R.error("保存失败");
        }
    }

也没问题,,那么问题到底在哪呢,这个很细节,

看看工具类里面的代码发现,imageUrl和imageFilePath,这个时候就注意:一个是图片地址,一个是文件路径,

看看yml文件里的内容,解决图片上传时保存信息:java.lang.NullPointerException: null 空指针异常解决_第1张图片

很显然不是本地路径,因此需要加上本地的盘符才可以;

修改(此处修改如果想让图片能够预览出来,需在本地搭建一个windows的 Nginx)

解决图片上传时保存信息:java.lang.NullPointerException: null 空指针异常解决_第2张图片

修改后

解决图片上传时保存信息:java.lang.NullPointerException: null 空指针异常解决_第3张图片
解决图片上传时保存信息:java.lang.NullPointerException: null 空指针异常解决_第4张图片

OK了

你可能感兴趣的:(Java,报错)