com.zhoujie.exceptions.ShopOperationException: addShop error:null
来源:4-5 店铺注册之Service层的实现
叶无道疯人院
2019-07-06
祥哥好,在做存储shop图片时遇到了空指针异常,真心的请教,具体的服务端报错信息是:
2019-07-06 01:43:06.329 [main] DEBUG com.zhoujie.dao.ShopDao.insertShop - ==> Preparing: insert into tb_shop(owner_id, area_id, shop_category_id, shop_name, shop_desc, shop_addr, phone, shop_img, create_time, last_edit_time, enable_status, advice) values (?,?,?,?,?,?,?,?,?,?,?,?)
2019-07-06 01:43:06.397 [main] DEBUG com.zhoujie.dao.ShopDao.insertShop - > Parameters: 2(Long), 1(Integer), 1(Long), 大排档8(String), 测试8(String), 江苏盐城8(String), 18806172172(String), null, 2019-07-06 01:43:06.296(Timestamp), 2019-07-06 01:43:06.296(Timestamp), 0(Integer), 审核中8(String)
2019-07-06 01:43:06.402 [main] DEBUG com.zhoujie.dao.ShopDao.insertShop - < Updates: 1
通过在addShopImg(shop,shopImg)这一句代码前后写上一句System.out.print(XXX),定位到是这句存储图片的方法出现了问题。shopImg字段获取为空,相关代码看了好久,也重复手写了2遍,实在是没辙了。
一、addShopImg的方法:
private void addShopImg(Shop shop, File shopImg) {
String dest = PathUtil.getShopImgPath(shop.getShopId());
String shopImgAddr = ImageUtil.generateThumbnail(shopImg,dest);
shop.setShopImg(shopImgAddr);
}
二、ImgUtil类
package com.zhoujie.util;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class ImageUtil {
private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
private static final SimpleDateFormat sf = new SimpleDateFormat(“yyyyMMddHHmmss”);
private static final Random r = new Random();
private static Logger logger = LoggerFactory.getLogger(ImageUtil.class);
public static void main(String[] args) throws IOException {
//线程逆推到类途径
logger.debug(basePath);
Thumbnails.of(new File("/Users/zhoujie/DeskTop/zhoujie.jpg")).size(200,200)
.watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(basePath+"/watermark.jpg")),0.25f)
.outputQuality(0.8f).toFile("/Users/zhoujie/DeskTop/zhoujienew.jpg");
}
public static File transferMultipartFileToFile(CommonsMultipartFile cFile){
File newFile = new File(cFile.getOriginalFilename());
try {
cFile.transferTo(newFile);
} catch (IOException e) {
logger.error(e.toString());
e.printStackTrace();
}
return newFile;
}
/**
* 处理用户传递过来的文件流,返回新生成图片的相对路径
* @param thumbnail
* @param targetAddr 文件保存在哪里
* @return
*/
public static String generateThumbnail(File thumbnail,String targetAddr){
String realFileName = getRandomFileName();
String extension = getFileExtension(thumbnail);
makeDirPath(targetAddr);
String relativeAddr = targetAddr + realFileName + extension;
logger.debug("current relativeAddr:"+relativeAddr);
File dest = new File(PathUtil.getImgBasePath() + relativeAddr);
logger.debug("current complete addr:"+PathUtil.getImgBasePath()+relativeAddr);
try{
Thumbnails.of(thumbnail).size(200,200).
watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File(basePath+"/watermark.jpg")),0.25f)
.outputQuality(0.8f).toFile(dest);
}catch (IOException e){
logger.error(e.toString());
e.printStackTrace();
}
return relativeAddr;
}
/**
* 创建目标路径上涉及的目录
* @param targetAddr
*/
private static void makeDirPath(String targetAddr) {
String realFileParentPath = PathUtil.getImgBasePath()+targetAddr;
File dirPath = new File(realFileParentPath);
if(!dirPath.exists()){
dirPath.mkdirs();
}
}
/**
* 获取输入文件的后缀名
* @param cFile
* @return
*/
private static String getFileExtension(File cFile) {
String originalFileName = cFile.getName();
return originalFileName.substring(originalFileName.lastIndexOf("."));
}
private static String getRandomFileName() {
int rannum = r.nextInt(89999)+10000;
String nowTimeStr = sf.format(new Date());
return nowTimeStr + rannum;
}
}
三、PathUtil类
public class PathUtil {
private static String seperator = System.getProperty(File.separator);
private static Logger logger = LoggerFactory.getLogger(PathUtil.class);
/**
* 项目图片的根路径
* @return
*/
public static String getImgBasePath(){
String basePath = “”;
String os = System.getProperty(“os.name”);
if(os.toLowerCase().startsWith(“win”)){
basePath = “D:/projectdev/image/”;
}else {
basePath = “/Users/zhoujie/image/”;
}
basePath = basePath.replace("/",seperator);
logger.debug(basePath);
return basePath;
}
/**
* 获取店铺图片的存储路径子路径
* @return
*/
public static String getShopImgPath(Long shopId){
String imagePath = "/upload/item/shop"+shopId+"/";
return imagePath.replace("/",seperator);
}
}
1回答
-
同学好,其实这类问题调试就基本能解决了,主要调试看看addShopImg这个方法里面遇到什么样的异常,感觉要么是水印不正确,要么是文件路径不正确
012019-07-07
相似问题