Servlet页面显示源代码而不是图像

huangapple go评论56阅读模式
英文:

Servlet page display source code instead of image

问题

这是我的index.jsp:

<%@page contentType="text/html; ISO-8859-1" %>
<html>
<body>
<h2>Hello World!</h2>
<a href="Servlet.java">获取图片</a>
</body>
</html>

链接到Servlet.java:

@WebServlet(name = "Servlet")
public class Servlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("image/jpeg");
        ServletOutputStream out = response.getOutputStream();
        FileInputStream fin = new FileInputStream("D:\34.jpg");
        BufferedInputStream bin = new BufferedInputStream(fin);
        BufferedOutputStream bout = new BufferedOutputStream(out);
        int ch;
        while ((ch = bin.read()) != -1) {
            bout.write(ch);
        }
        bin.close();
        fin.close();
        bout.close();
        out.close();
    }
}

我的结果:https://i.stack.imgur.com/Lkuo8.png

我尝试设置不同的响应内容类型,但似乎没有生效。

英文:

This is my index.jsp

&lt;%@page contentType=&quot;text/html; ISO-8859-1&quot; %&gt;
    &lt;html&gt;
    &lt;body&gt;
    &lt;h2&gt;Hello World!&lt;/h2&gt;
    &lt;a href=&quot;Servlet.java&quot;&gt;Get IMG&lt;/a&gt;
    &lt;/body&gt;
    &lt;/html&gt;

which link to Servlet.java

@WebServlet(name = &quot;Servlet&quot;)
public class Servlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType(&quot;image/jpeg&quot;);
        ServletOutputStream out = response.getOutputStream();
        FileInputStream fin = new FileInputStream(&quot;D:\34.jpg&quot;);
        BufferedInputStream bin = new BufferedInputStream(fin);
        BufferedOutputStream bout = new BufferedOutputStream(out);
        int ch;
        while ((ch = bin.read()) != -1) {
            bout.write(ch);
        }
        bin.close();
        fin.close();
        bout.close();
        out.close();
    }
}

My result : https://i.stack.imgur.com/Lkuo8.png

I did try to set different response contentType but nothing seems to work.

答案1

得分: 0

你做了两件错误的事情:

  • 你的Servlet应该放在源代码目录中,而不是Web根目录中。 <br/>
    如果文件在Web根目录中,它们将原样发送,除非服务器知道如何解析它们(例如jsp文件)。

  • 不要链接到Servlet.java,而应该链接到Servlet,如果你想要通过@WebServlet(name = "Servlet")来指定它的位置。

英文:

You have done two things wrong

  • Your servlets neet to be inside the source directory, not in the web root. <br/>
    If the files are inside the web root, they will be sent as is except the server knows how to parse them(like jsp files).

  • Do not link to Servlet.java but to Servlet if you want to have specified it to be located there using @WebServlet(name = &quot;Servlet&quot;).

huangapple
  • 本文由 发表于 2020年8月8日 22:57:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63316792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定