猿问

从 MvcUriComponentsBuilder.fromMethodName()

我正在使用 MvcUriComponentsBuilder.fromMethodName() 来获取 URL 列表并将它们返回到前端。以下是示例输出,其中我以 localhost 的形式获取域:


[ http://localhost:8081/files/1800_tiger.jpg/slideshow , http://localhost:8081/files/1800_trees.jpg/slideshow ]


我希望 MvcUriComponentsBuilder 返回我系统的 IP 地址,而不是 localhost。以下是我的代码实现:


     @CrossOrigin

     @RestController

     public class ContentResource {



        @RequestMapping("/getAllFiles")

        public ResponseEntity<List<String>> getAllFiles(@RequestParam String panelName) {

            List<String> fileNamesList = panelFileListMap.get(panelName);

            if (fileNamesList != null) {

                List<String> allFiles = fileNamesList.stream()

                        .map(fileName -> MvcUriComponentsBuilder

                                .fromMethodName(ContentResource.class, "getFile", fileName, panelName).build().toString())

                        .collect(Collectors.toList());

                return ResponseEntity.ok().body(allFiles);

            } else {

                throw new RuntimeException("No images are uploaded in category = " + panelName);

            }

        }



 @GetMapping("/files/{filename:.+}/{panelName}")

    @ResponseBody

    public ResponseEntity<Resource> getFile(@PathVariable("filename") String filename,

            @PathVariable("panelName") String panelname) {


        Resource file = storageService.loadFile(filename, panelname);

        return ResponseEntity.ok()

                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getFilename() + "\"")

                .body(file);


    }


        }



素胚勾勒不出你
浏览 351回答 1
1回答
随时随地看视频慕课网APP

相关分类

Java
我要回答