繁体   English   中英

尝试在 Spring Boot REST API 中下载文件时如何修复“找不到资源”?

[英]how to fix 'resource not found' when trying to download file in spring boot REST API?

我是 spring 框架和 REST 的新手,现在尝试将 REST 从 jersey 迁移到 spring boot 2.1 控制器与 jax-rs 一起工作正常,但是,我不想在 spring boot 中使用 jax-rs。 因此,我尝试了 spring Mvc,但出现“找不到资源”错误。 请我真的很感激任何帮助。

我试过这个

@GetMapping(value ="/generic/download_file/{path:[^\\.+]*}", consumes ="application/vnd.X-FileContent")
    public ResponseEntity<?> downloadFile(@PathVariable("path") String filePath){

        String actualFilePath = "";

        try {

            actualFilePath = filePath.replaceAll("\\/", "\\\\");

            File file = new File(actualFilePath);

            if (file.exists()) {

                return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"").body(file);

            } else {

                return errorHandling.errorResponseFactory("1.0.0", Thread.currentThread().getStackTrace()[1], "",
                        RecommendedSolution.UseValidDirectoryPath, "File not exist.");

            }

        } catch (Exception ex) {

            ActionLog.writeLog("program_library_v510", "1.0.0", "Exception occur during gettig generic package file",
                    ActionLogType.DebugLog);
            ActionLog.writeLog("program_library_v510", "1.0.0", "Exception occur during getting generic package file",
                    ActionLogType.ErrorLog);

            return errorHandling.errorResponseFactory("1.0.0", Thread.currentThread().getStackTrace()[1], "",
                    RecommendedSolution.UnexpectedErrorMsg, "");
        }
    }

2019-01-07 17:17:23.930 INFO 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet:在 10 毫秒内完成初始化 2019-01-07 17:17:23.947 DEBUG 1 [nio-9090-exec-2] osweb.servlet.DispatcherServlet : GET "/packages/download_file/D:/xfolder/test.txt", 参数 rs={} 2019-01-07 17:17:24.002 DEBUG 13664 - -- [nio-9090-exec-2] oswshandler.SimpleUrlHandlerMapping : 映射到 ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath: /public/", "/"] 2019-01-07 17:17:24.006 DEBUG 13664 --- [nio-9090-exec-2] oswsrResourceHttpRequestHandler :找不到资源 2019-01-07 17:17:24.0136 DEBUG --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet : Completed 404 NOT_FOUND 2019-01-07 17:17:24.015 DEBUG 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet :GET“/错误”的“错误”调度,参数={} 2019-01-07 17:17:24.029 DEBUG 13664 --- [nio-9090-exec-2] swsmmaRequestMappingHandlerMapping :映射到 p ublic org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServ letRequest) 2019-01-07 17:17:24.077 DEBUG 13664 -- - [nio-9090-exec-2] oswsmmaHttpEntityMethodProcessor : 使用'application/json',给定[ / ] 并支持[application/json, application/ +json, application/json, application/ +json] 2019-01-07 17:17:24.078 DEBUG 13664 --- [nio-9090-exec-2] oswsmmaHttpEntityMethodProcessor : 写作 [{timestamp=Mon Jan 07 17:17:24 SGT 2019, status=40 4, error=Not Found, message=No消息可用,路径=/packages/download_file/D:/xfolder/test.txt}] 2019-01-07 17:17:24.146 DEBUG 13664 --- [nio-9090-exec-2] osweb.servlet.DispatcherServlet :退出“错误”调度,状态 404

我怀疑这里有两件事。 让我澄清一下。

  1. 你调用的 API url 是什么? /generic/download_file/{path:[^\\\\.+]*}/packages/download_file/D:/xfolder/test.txt

两者看起来都不一样。 请参阅通用

  1. 在 URL 中传递文件名的更好方法是使用@RequestParam而不是@PathVariable

    @GetMapping(value ="/generic/download_file/", 消费 ="application/vnd.X-FileContent") public ResponseEntity downloadFile(@RequestParam("path") String filePath){

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM