java 如何在 Spring Boot 中将页面重定向到静态文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35979403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:50:19  来源:igfitidea点击:

How to redirect page to static file in Spring Boot?

javaspring-mvcspring-boot

提问by codeshark

How can I redirect a page web request / request mapping in Spring Boot (MVC) to point to a static file (example: .txt, .json, .jpg, .mp4, etc). All I have in my Spring Boot project is an application.properties file and the @Controllers.

如何在 Spring Boot (MVC) 中重定向页面 Web 请求/请求映射以指向静态文件(例如:.txt、.json、.jpg、.mp4 等)。我在 Spring Boot 项目中只有一个 application.properties 文件和@Controllers。

I'd like for the user, when making a web request to the url in browser to be asked to download the file (not use it to attempt to render the page, like it does with .html, .jsp)

我希望用户在向浏览器中的 url 发出 Web 请求以要求下载文件时(不要使用它来尝试呈现页面,就像使用 .html、.jsp 一样)

采纳答案by Daniel Lavoie

You can achive this by telling the response that you wish to attach a downloadable file. Then you can simple write the content you want to make downloadable.

您可以通过告诉响应您希望附加可下载文件来实现此目的。然后您可以简单地编写您想要下载的内容。

Here is an example :

这是一个例子:

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/myredirect", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void downloadFile(HttpServletResponse response) {
    // Remove this instruction if you wish to disable the download dialog.
    response.setHeader("Content-Disposition", "attachment; filename=filename.ext");

    // Load your file content as byte.
    byte[] fileContent = IOUtils.toByteArray(new ClasspathResource("myfile").getIntputStream());

    response.getOutputStream().write(fileContent);
}

On the other hand, if you simply want a direct mapping to a static file. You may use the default publicfolder of Spring Boot Starter Web.

另一方面,如果您只想直接映射到静态文件。您可以使用publicSpring Boot Starter Web的默认文件夹。

Any file found inside classpath:/publicwill be mapped to /*by default.

默认情况下,在其中找到的任何文件classpath:/public都将被映射到/*

回答by Willy du Preez

You can redirect using the "redirect:" prefix in Spring. From the Spring documentation:

您可以在 Spring 中使用“redirect:”前缀进行重定向。从Spring 文档

A logical view name such as redirect:/myapp/some/resource will redirect relative to the current Servlet context, while a name such as redirect:http://myhost.com/some/arbitrary/pathwill redirect to an absolute URL.

诸如 redirect:/myapp/some/resource 之类的逻辑视图名称将相对于当前 Servlet 上下文重定向,而诸如 redirect: http://myhost.com/some/arbitrary/path 之类的名称将重定向到绝对 URL。

An example would be:

一个例子是:

@RequestMapping("/redirectToResource")
protected String redirect(@RequestParameter("resource") String resource) {
    return "redirect:/myapp/some/" + resource;
}

You can place static resources to be served directly in your classpath in any of the following locations (see Serving static resources):

您可以将要直接提供的静态资源放置在以下任何位置的类路径中(请参阅提供静态资源):

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
    "classpath:/META-INF/resources/", "classpath:/resources/",
    "classpath:/static/", "classpath:/public/" };