tomcat作为图片服务器

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
<Context path="/pic" docBase="E:/imgupload" reloadable="false"></Context>
      </Host>

host节点增加Context配置。

其中path为请求路径前缀;docBase为图片存储的路径。

如:localhost:8080/pic/2020/xxx.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将图片传到 Tomcat 上,你需要将图片文件放置在 Web 应用程序的 WebContent 目录下(或者你定义的其他资源目录),以便在 Web 应用程序运行时能够访问这些文件。然后,你需要使用 Java 代码将图片文件读取并输出到响应中。 以下是一个简单的示例代码,该代码将名为 "example.jpg" 的图片文件作为响应输出到 Web 浏览器: ```java @WebServlet("/image") public class ImageServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String imagePath = request.getServletContext().getRealPath("/WEB-INF/images/example.jpg"); File imageFile = new File(imagePath); FileInputStream fis = new FileInputStream(imageFile); byte[] data = new byte[(int) imageFile.length()]; fis.read(data); fis.close(); response.setContentType("image/jpeg"); OutputStream os = response.getOutputStream(); os.write(data); os.close(); } } ``` 在上面的代码中,我们使用 `@WebServlet` 注释将 Servlet 映射到 "/image" URL。在 `doGet` 方法中,我们使用 `getServletContext().getRealPath` 方法获取图片文件的真实路径。然后,我们使用 `FileInputStream` 读取图片文件的内容,并将其存储在字节数组中。最后,我们使用 `setContentType` 方法设置响应的 MIME 类型为 "image/jpeg",并使用 `getOutputStream` 方法获取响应输出流,将像文件的内容写入响应中。 使用上述代码,你可以将任意数量的图片文件传输到 Tomcat 服务器上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值