Javaweb Base Class getresourceasstream resource Path no slash

Source: Internet
Author: User

Li Wu:
Learn to think more, honouring teachers save Thanksgiving. Leaf See Root 321, rivers with one.
Meekness Conscience Lord, willing to do without regrets to the most bitter. Reading exercise strong body and mind, Prudential advised and the line and cherish.



Javaee:7
javase:1.8
jstl:1.2.2
Server:tomcat 8.5
Explorer:chrome
Os:windows7 x64
Ide:myeclipse



Project directory Structure


Xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee/http Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "id=" webapp_id "version=" 3.1 "><servlet><servlet-name> myservlet</servlet-name><servlet-class>jizuiku.web.servlet.myservlet</servlet-class></ servlet><servlet-mapping><servlet-name>myservlet</servlet-name><url-pattern>/ Myservlet</url-pattern></servlet-mapping></web-app>

The folder where the resource resides


Servlet Class Code

Package Jizuiku.web.servlet;import Java.io.ioexception;import Java.io.inputstream;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import org.apache.commons.io.ioutils;/** * * * * @author to the hardest * @version V17.10.25 */public class Myservlet extends HttpServlet {/ * * */private static final long serialversionuid = 1L; @Overrideprotected void doget (HttpServletRequest request, Httpserv Letresponse response) throws Servletexception, IOException {class<myservlet> myservletclass = MyServlet.class;// No/is the path relative to the. class file InputStream pictureinput = Myservletclass.getresourceasstream ("laozi.jpg");//Will flow into a byte array, It is convenient to use Ioutils.tobytearray byte[] b = Ioutils.tobytearray (pictureinput); Response.getoutputstream (). write (b);} @Overrideprotected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {}}

Open a browser to view

The resource path gets correct, and the file resource is read correctly.

Learning resources: Itcast and Itheima Video library. If you have public resources, can share to me, with your resources to learn also can.
Blog post is to watch the video, into thinking written. It's good that the teacher speaks well. Blog bad, is to give the most bitter not serious.

Javaweb Base Class getresourceasstream resource Path no slash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.