访问 webapp 之外的文件(Tomcat V6 和 Spring) [英] Access files that are outside webapp (Tomcat V6 and Spring)

查看:26
本文介绍了访问 webapp 之外的文件(Tomcat V6 和 Spring)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供了上传图片的选项,图片正在上传到 opt/uploads/contactImages,在 Windows 的情况下是 C:/opt/uploads/contactImages

我想在我的 JSP 中显示图像.为此,我尝试通过在 Host 标签下添加以下标签来配置我的 tomcat.

但是当我尝试访问时

http://localhost:8080/images/file.png

我收到 404 错误.我哪里出错了?

更新

Servlet web.xml 文件

<servlet-mapping><servlet-name>mvc-dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping><上下文参数><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value></context-param><监听器><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></监听器>

解决方案

Servlet 或资源处理程序不能直接从文件系统提供文件.您需要编写自己的 @Controller 处理程序方法.例如

@Controller公共类图像控制器{public static final String BASE_PATH = "/opt/uploads/contactImages";@RequestMapping(value = "/{fileName}" , method = RequestMethod.GET)公共响应实体getFile(@PathVariable("fileName") String fileName) {FileSystemResource 资源 = new FileSystemResource(new File(BASE_PATH, fileName));ResponseEntityresponseEntity = new ResponseEntity<>(resource, HttpStatus.OK);返回响应实体;}}

随着ResponseEntity 类还可以设置不同的响应头和状态码.

您现在可以访问以上内容

http://localhost:8080/images/file.png

我认为上述内容不适用于嵌套目录中的文件.

<小时>

注意

中的docBase属性

不正确.docBase 属性指定<块引用>

此文档的文档库(也称为上下文根)目录Web 应用程序,或 Web 应用程序存档文件的路径名(如果此 Web 应用程序直接从 WAR 中执行文件).您可以为此目录或 WAR 指定绝对路径名文件,或相对于 appBase 目录的路径名拥有主机.

因此它必须指向您的 Web 应用程序,而不是您存储文件的随机目录.

I am giving option to upload images and the Images are being upload to opt/uploads/contactImages which in the case of windows is C:/opt/uploads/contactImages

I want to display Images in my JSP. For which I tried to configure my tomcat by adding the following tag under Host tag.

<Context docBase="/opt/uploads/contactImages/" path="/images" />

But when I try to access

http://localhost:8080/images/file.png 

I get 404 Error. Where I am going wrong?

Update

Servlet web.xml file

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring Web MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

解决方案

A Servlet or a resource handler can't just serve files from the file system directly. You need to write your own @Controller handler methods. For example

@Controller
public class ImagesController {

    public static final String BASE_PATH = "/opt/uploads/contactImages";

    @RequestMapping(value = "/{fileName}" , method = RequestMethod.GET) 
    public ResponseEntity<FileSystemResource> getFile(@PathVariable("fileName") String fileName) {
        FileSystemResource resource = new FileSystemResource(new File(BASE_PATH, fileName));
        ResponseEntity<FileSystemResource> responseEntity = new ResponseEntity<>(resource, HttpStatus.OK);
        return responseEntity;
    }
}

With the ResponseEntity class you can also set different response headers and status codes.

You can now access the above at

http://localhost:8080/images/file.png 

I don't think the above will work for files in nested directories.


Note that the docBase attribute in

<Context docBase="/opt/uploads/contactImages/" path="/images" />

is incorrect. The docBase attribute specifies

The Document Base (also known as the Context Root) directory for this web application, or the pathname to the web application archive file (if this web application is being executed directly from the WAR file). You may specify an absolute pathname for this directory or WAR file, or a pathname that is relative to the appBase directory of the owning Host.

So it has to point to your web application, not a random directory where you store files.

这篇关于访问 webapp 之外的文件(Tomcat V6 和 Spring)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆