【问题标题】:Get URLs with IP address from MvcUriComponentsBuilder.fromMethodName() instead of localhost从 MvcUriComponentsBuilder.fromMethodName() 而不是 localhost 获取带有 IP 地址的 URL
【发布时间】:2019-02-01 04:48:18
【问题描述】:

我正在使用 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);

    }

        }

【问题讨论】:

    标签: java spring spring-mvc localhost ipv6


    【解决方案1】:

    自己找到解决方案:更改 getAllFiles 方法如下:

    InetAddress ip = null;
    @RequestMapping("/getAllFiles")
        public ResponseEntity<List<String>> getAllFiles(@RequestParam String panelName) {
    
            try {
                ip = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            List<String> fileNamesList = panelFileListMap.get(panelName);
            if (fileNamesList != null) {
                List<String> allFiles = fileNamesList.stream()
                        .map(fileName -> MvcUriComponentsBuilder
                                .fromMethodName(ContentResource.class, "getFile", fileName, panelName)
                                .host(ip.getHostAddress()).build().toString())
                        .collect(Collectors.toList());
                return ResponseEntity.ok().body(allFiles);
            } else {
                throw new RuntimeException("No images are uploaded in category = " + panelName);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 2019-02-01
      • 2018-11-15
      • 1970-01-01
      • 2019-01-15
      相关资源
      最近更新 更多