栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从jsp的本地路径显示图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从jsp的本地路径显示图像

看来您对BalusC的帖子有误解:FileServlet。在这种情况下,磁盘上将具有一个用于存储文件的基本路径(在服务器中Web应用程序文件夹路径的外部),然后URL中使用的路径将用于
该基本路径 进行搜索。从BalusC的示例中注意如何调用资源:

    <a href="file/foo.exe">download foo.exe</a>

哪里:

  • 文件 是Servlet的URL模式。在您的web.xml配置中注意到:
        <servlet-mapping>    <servlet-name>FileServlet</servlet-name>    <url-pattern>/file/*</url-pattern>    </servlet-mapping>
  • URL /foo.exe 的其余部分是文件在服务器硬盘驱动器中的位置。这可以很容易地通过使用
    HttpServletRequest.html#getPathInfo

这是在代码的这一部分中指出的(注释是我的):

    public void init() throws ServletException {        this.filePath = "/files";    }    protected void doGet(HttpServletRequest request, HttpServletResponse response)        throws ServletException, IOException {        // Get the file path from the URL.        String requestedFile = request.getPathInfo();        //...        //using filePath attribute in servletclass as the base path        //to lookup for the files, using the requestedFile        //path to seek for the existance of the file (by name)        //in your server        //decoding the name in case of GET request encoding such as        //%2F => /        File file = new File(filePath, URLDeprer.depre(requestedFile, "UTF-8"));        //...    }

然后,当有这样的请求时(在您的视图中为HTML代码):

    <img src="files/img/myimage.png" />

您必须确保文件存在于服务器中:

    - /  <-- root      - /files <-- this is the base file path set in init method in your servlet        - /img          - myimage.png


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573465.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号