Java 从MvcUriComponentsBuilder.fromMethodName()而不是localhost获取IP地址为的URL
我正在使用MvcUriComponentsBuilder.fromMethodName()获取URL列表并将其返回到前端。以下是示例输出,其中我以localhost的形式获取域: [ , ] 我希望MvcUriComponentsBuilder返回系统的IP地址,而不是本地主机。以下是我的代码实现:Java 从MvcUriComponentsBuilder.fromMethodName()而不是localhost获取IP地址为的URL,java,spring,spring-mvc,localhost,ipv6,Java,Spring,Spring Mvc,Localhost,Ipv6,我正在使用MvcUriComponentsBuilder.fromMethodName()获取URL列表并将其返回到前端。以下是示例输出,其中我以localhost的形式获取域: [ , ] 我希望MvcUriComponentsBuilder返回系统的IP地址,而不是本地主机。以下是我的代码实现: @CrossOrigin @RestController public class ContentResource { @RequestMappi
@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);
}
}
@CrossOrigin
@RestController
公共类内容资源{
@请求映射(“/getAllFiles”)
公共响应属性getAllFiles(@RequestParam String panelName){
List fileNamesList=panelFileListMap.get(panelName);
if(fileNamesList!=null){
List allFiles=fileNamesList.stream()
.map(文件名->MvcUriComponentsBuilder
.fromMethodName(ContentResource.class,“getFile”,文件名,panelName).build().toString())
.collect(Collectors.toList());
返回ResponseEntity.ok().body(所有文件);
}否则{
抛出新的RuntimeException(“category=“+panelName”中没有上载图像);
}
}
@GetMapping(“/files/{filename:.+}/{panelName}”)
@应答器
公共响应属性getFile(@PathVariable(“filename”)字符串文件名,
@PathVariable(“panelName”)字符串(panelName){
资源文件=storageService.loadFile(文件名、面板名);
返回ResponseEntity.ok()
.header(HttpHeaders.CONTENT\u处置,“附件;文件名=\”“+文件.getFilename()+”\“”)
.机构(档案);
}
}
自己找到了解决方案:更改了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);
}
}
inetAddressIP=null;
@请求映射(“/getAllFiles”)
公共响应属性getAllFiles(@RequestParam String panelName){
试一试{
ip=InetAddress.getLocalHost();
}捕获(未知后异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
List fileNamesList=panelFileListMap.get(panelName);
if(fileNamesList!=null){
List allFiles=fileNamesList.stream()
.map(文件名->MvcUriComponentsBuilder
.fromMethodName(ContentResource.class,“getFile”,文件名,面板名)
.host(ip.getHostAddress()).build().toString())
.collect(Collectors.toList());
返回ResponseEntity.ok().body(所有文件);
}否则{
抛出新的RuntimeException(“category=“+panelName”中没有上载图像);
}
}