如何从localhost访问本地文件

时间:2017-12-26 22:51:34

标签: java spring spring-boot localhost

我正在使用spring boot开发一个Web应用程序。在应用程序中,我将文件绝对路径保存在数据库中后上传文件并复制到本地文件夹。所以我有文件,我知道它在哪里,但我无法在localhost中访问它。我知道在spring中有一个静态文件夹用于静态文件,但我不想复制文件,因为我会在其他应用程序中使用这些数据。

例如:

本地文件位置:/Users/user/data/image.png

我希望达到这样的目的:http://localhost:8080/data/image.png

编辑:我找到了解决方案。我为此使用了MvcUriComponentsBuilder

String url = MvcUriComponentsBuilder.fromMethodName(FileController.class,"serveFile",resource.getFilename()).build().toString();

它是控制器方法的正确值。

方法:

@GetMapping("/files/{filename:.+}")
    @ResponseBody
    public ResponseEntity<Resource> serveFile(@PathVariable String filename) {

        Resource file = storageService.loadAsResource(filename);
        return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=\"" + file.getFilename() + "\"").body(file);
    }

1 个答案:

答案 0 :(得分:0)

我认为问题在于您正在使用的网址。将其更改为:

Application.SheetSelectionChange

这将有效。