本文主要是介绍解决图片上传时保存信息: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")@ResponseBodypublic 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文件里的内容,
很显然不是本地路径,因此需要加上本地的盘符才可以;
修改(此处修改如果想让图片能够预览出来,需在本地搭建一个windows的 Nginx)
修改后
OK了
这篇关于解决图片上传时保存信息:java.lang.NullPointerException: null 空指针异常解决的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!