现在网上 大部分能找到的关于文件上传路径的代码一般是

private static final Logger LOGGER = LoggerFactory.getLogger(AppUpgradeController.class);
    //保存时的文件名
    DateFormat df = new SimpleDateFormat(\"yyyyMMddHHmmss\");
    Calendar calendar = Calendar.getInstance();
    String dateName = df.format(calendar.getTime())+file.getOriginalFilename();
    WebApplicationContext webApplicationContext = (WebApplicationContext) SpringContextUtils.applicationContext;
    ServletContext servletContext = webApplicationContext.getServletContext();
    String realPath = servletContext.getRealPath(\"/\");

通过ervletContext.getRealPath(\"/\")访问到项目classes文件的目录中

那么问题来了  当你打war包到linux 下的时候 直接访问war包,这个方法会报java.lang.NullPointException  

因为classes目录在WEB-INF下  不解压 所以一定是访问不到该路径的,空指针也就是情理之中了

并不是因为这个方法是过时的 而导致会上传失败空指针!!

String realPath = System.getProperty(\"user.dir\");

我是用这种获取到项目的目录+/  表示在项目目录之下     不加的话跟项目同级 就是名字显得有点奇guai =.=(手动滑稽)

 

收藏 打印