在作业ETL-Load中执行步骤ETL-file-load时遇到错误

时间:2020-04-02 12:41:00

标签: java spring spring-boot spring-mvc spring-batch

我在Heroku服务器上收到此错误,但它完全可以在本地运行。

这是控制器,它正在从表单获取CSV文件并试图存储在资源文件夹中。

open class Test {

    open @objc dynamic func test() {
        print("test")
    }
}

这是项目读取器,采用临时存储在资源文件夹中的CSV文件的路径。

@objc

但是它在日志中显示此错误:

@PostMapping(value = "/import-csv")
    public ModelAndView uploadCSV(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile multipartFile) throws IOException, JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
        logger.info("Entering into import-csv controller");
        int id = Integer.parseInt(request.getParameter("listId"));
        try {
            String listId = request.getParameter("listId");
            logger.info("id is : {}",id);
            String path = new ClassPathResource("tempUpload/").getURL().getPath();
            logger.info("path : {}",path);
            File fileToImport = new File(path + multipartFile.getOriginalFilename());
            logger.info("full path : {}", fileToImport.toString());

            System.out.println("iside cotmt:" + "file:" + fileToImport.getAbsolutePath());


            Map<String, JobParameter> maps = new HashMap<>();
            maps.put("time", new JobParameter(System.currentTimeMillis()));
            maps.put("fullPathFileName", new JobParameter(fileToImport.getAbsolutePath()));
            maps.put("listId", new JobParameter(listId));
            JobParameters parameters = new JobParameters(maps);
            JobExecution jobExecution = jobLauncher.run(job, parameters);


            logger.info("JobExecution: {}" ,jobExecution.getStatus());

            logger.info("Batch is Running...");
            userService.setListofUserSave(listId);
            logger.info("Exiting from import-csv controller");


        } catch (Exception e) {
            logger.error("Error while uploading csv file");
            e.printStackTrace();
        }
        return new ModelAndView("redirect:/view?id=" + id);
    }

1 个答案:

答案 0 :(得分:0)

查看错误:

Apr 02 04:11:31 demo-letter app/web.1 Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode): file [/app/file:/app/target/demo-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/tempUpload/csvtesting.csv

我相信您的资源是从类路径解析的,而不是在文件系统上解析的。您的问题在这里:

String path = new ClassPathResource("tempUpload/").getURL().getPath();

应为:

String path = new FileSystemResource("/absolute/path/to/tempUpload/").getURL().getPath();