java - ServletOutputStream 和图片的关系
ringa_lee
ringa_lee 2017-04-18 10:33:36
0
3
760

在前台页面有如下语句
验证码,点击图片更换

其中src 指向的地址不是一个img文件,而是一个转到SpringMVC的类中方法
src="/ran/random?random=0.2868249340216069"

方法如下:

@Controller public class RandomCodeController { @RequestMapping(value={"/ran/random"}) public void genericRandomCode(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader("Cache-Control", "private,no-cache,no-store"); response.setContentType("image/png"); HttpSession session = request.getSession(); int width = 85; int height = 28; BufferedImage image = new BufferedImage(width, height, 2); Graphics2D g = image.createGraphics(); g.setComposite(AlphaComposite.getInstance(3, 1.0f)); Random random = new Random(); g.setColor(new Color(231, 231, 231)); g.fillRect(0, 0, width, height); g.setFont(new Font("Microsoft YaHei", 2, 24)); String sRand = ""; for (int responseOutputStream = 0; responseOutputStream < 4; ++responseOutputStream) { String rand = String.valueOf(random.nextInt(10)); sRand = sRand + rand; g.setColor(new Color(121, 143, 96)); g.drawString(rand, 13 * responseOutputStream + 16, 23); } session.setAttribute("COMMON_RAND_CODE", (Object)sRand); g.dispose(); ServletOutputStream var12 = response.getOutputStream(); ImageIO.write((RenderedImage)image, "png", (OutputStream)var12); var12.close(); } }

所以其实这个src相联系的是一个ServletOutputStream
这个地方我不太理解
ServletOutputStream如何和一个图片联系起来?

ringa_lee
ringa_lee

ringa_lee

reply all (3)
洪涛

Not only pictures, but other files are also transferred in the form of streams. The browser parses the stream given by the background into pictures.

    小葫芦

    To put it bluntly, how the picture is displayed on your computer is a data stream through the network.
    The same goes for pointing to a specific picture or a resource on another server.It means that your web server reads the corresponding file in the form of a stream and then sends it out through the network. What the browser is connected to is a stream, and it can only judge what the file in the stream is based onmime type
    This is simply the pseudo code

    socket = ss.accept(); fileInput = new FileInput(socket.getURI()); for(xxxx){ // 将输入的内容发送到输出流中 .... }

    The code above you actually mainly generates a picture, but it does not write the file to the hard disk, but directly sends it to the browser.
    ImageIO.write(image, "png", output)这句实际上就是将imageThe data of the object is written to the corresponding location. If this stream is a fileoutput, it will be written to the hard disk, so you can also change it to this.

    ImageIO.write(image, "png", new FileOutputStream(serverPath + "/images/xxx.png")); response.redirect("/images/xxx.png"); //定时计划 删除资源 task.delFile(serverPath + "/images/xxx.png")
      刘奇

      This is a problem with the way file streams are transmitted in Servlets. ServletOutputStream is connected to the browser client through HttpServletResponse.getOutputStream(), and ImageIO.write() converts the file into an output stream. As for the intermediate connection process and the specific implementation method, Then you need to look at the source code.

      I guess you don’t want to study so deeply. Just know the general process of file stream transmission in the B/S structure, and understand the specific code implementation methods and parameter setting rules. In addition, it is recommended that you extract the verification code generation code and write a separate method, and then call the RandomCodeController() method, so that the structure is simple and clear

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!