springboot文件上传request.getServletContext().getRealPath(“/“) 报错记录

在新的环境中,使用`request.getServletContext().getRealPath(/)`无法正确获取SpringBoot项目的路径,导致的问题是路径被重定向到了临时文件夹。解决方法是在Idea的编辑配置中,为SpringBoot启动项设置工作目录。具体操作包括:进入`Edit Configurations`,找到`Working Directory`选项,勾选并选择项目资源目录,这样可以确保获取到正确的项目路径。

问题详解:
在新环境导入项目,之前通过request.getServletContext().getRealPath(“/”) 能够获取到的路径现在获取失败。每次获取路径都是一个类似下列的新路径:
C:\Users\78284\AppData\Local\Temp\tomcat.9157374447778886810.8080
如何解决?
idea–>edit configurations
在这里插入图片描述
添加springboot启动项
在这里插入图片描述
将工作路径配置添加进来
点击more oprtions
在这里插入图片描述
勾选上working directory
在这里插入图片描述
选择当前项目的resource目录
在这里插入图片描述

public List<GrpOrgInfoSettingDomain> getCombinedRatioReport(FileDownForm filterMask); @PostMapping(value = "/commons/download/downReport") public ModelAndView resultDownload(HttpServletRequest request, HttpServletResponse response, FileDownForm filterMask) throws IOException { ModelAndView modelAndView = null; String path = request.getSession().getServletContext().getRealPath("") + Constant.PATH_FIX; log.info("path:{}" +path); StringBuilder fileName = new StringBuilder(); // 根据前端传入报表代码,获取数据库报表配置信息 String repSubName = "01";// 报表子类型默认为01 if (Constant.isEmpty(filterMask.getRepSubName())) { filterMask.setRepSubName(repSubName); } if(!Constant.isEmpty(filterMask.getChannel())){ List<TcompanyOrganDomain> organList =tcompanyOrganService.getOrganCodeByOrganIdLsprod(filterMask.getOrganId(),filterMask.getChannel()); filterMask.setOrganCode(organList.get(0).getOrganCode()); }else{ if (!Constant.isEmpty(filterMask.getOrganId())) { TcompanyOrganDomain company=new TcompanyOrganDomain(); company.setOrganId(Long.parseLong(filterMask.getOrganId())); List<TcompanyOrganDomain> organList = tcompanyOrganService.selectList(company); filterMask.setOrganCode(organList.get(0).getOrganCode()); } } TgrpReportConfigDomain reportConfig = tgrpReportConfigService.selectReportConfig(filterMask); if (reportConfig != null) { String downloadType = reportConfig.getDownloadType(); String sqlId = reportConfig.getSqlId(); String reportDesp = reportConfig.getReportDesp(); String viewName = reportConfig.getViewName(); // 前端传入YM不为空,将YM组装到报表名称中 if (StringUtils.isNotEmpty(filterMask.getYm())) { fileName.append(filterMask.getYm()); } fileName.append(reportDesp).append("_"); fileName.append(DateUtil.getCurrentDate(Constant.YYYYMMDDHHMMSS)); fileName.append(".xls"); if (XML.equals(downloadType)) { List list = tgrpReportConfigService.selectReportList(sqlId, filterMask); // 设置报表文件名称 response.addHeader(Constant.CONTENT_DISPOSITION, Constant.ATTACHMENT_FILE + URLEncoder.encode(fileName.toString(), Constant.UTF_8)); Map<String, Object> map = new HashMap<>(); map.put("list", list); map.put("viewName", viewName); return new ModelAndView("sheetExcelView", map); } } return modelAndView; } <!--报表下载--> <select id="getCombinedRatioReport" parameterType="com.foresealife.newglschannel.grp.domain.FileDownForm" resultMap="CombinedRatioResultMap"> <include refid="CombinedRatioQuery" /> </select> <!-- 查询sql结果与java model映射--> <resultMap id="CombinedRatioResultMap" type="com.foresealife.newglschannel.grp.domain.GrpOrgInfoSettingDomain"> <result property="organCode" column="ORGAN_CODE"/> <result property="organName" column="ORGAN_NAME"/> <result property="ym" column="YM"/> <result property="setValue" column="SET_VALUE"/> </resultMap> <!-- 公用查询sql--> <sql id="CombinedRatioQuery"> <![CDATA[ select ois.organ_code, to_char(ois.start_date, 'yyyy') ym, ois.set_value,tco.abbr_name organ_name from T_GRP_ORG_INFO_SETTING ois,t_company_organ tco where ois.set_type = '1' and ois.organ_code=tco.organ_code ]]> <if test= "organId != null and organId != ''"> and tco.organ_id in (select t.organ_id from t_company_organ t where t.status = 1 start with t.organ_id = #{organId} connect by PRIOR t.organ_id = t.parent_id) </if> <if test= "ym != null and ym != ''"> and ois.start_date = to_date(#{ym} || '0101', 'yyyymmdd') </if> </sql>报错c.f.n.core.support.AppContext - 根据名称:GrpOrgInfoSettingDao获取bean为空 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'GrpOrgInfoSettingDao' available c.f.n.c.support.GlobalServiceSupport - 查询所有对象集合异常{}: java.lang.NullPointerException: null
最新发布
11-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值