国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

java - ServletOutputStream 和圖片的關(guān)系
ringa_lee
ringa_lee 2017-04-18 10:33:36
0
3
938

在前臺頁面有如下語句
<img onclick="this.src='/ran/random?random='+Math.random()" alt="驗證碼,點擊圖片更換" src="/ran/random?random=0.2868249340216069" width="86" height="40">

其中src 指向的地址不是一個img文件,而是一個轉(zhuǎn)到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相聯(lián)系的是一個ServletOutputStream
這個地方我不太理解
ServletOutputStream如何和一個圖片聯(lián)系起來?

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 on mime type
This is simply the pseudo code

socket =  ss.accept();
fileInput = new FileInput(socket.getURI());
for(xxxx){  // 將輸入的內(nèi)容發(fā)送到輸出流中
    ....
}

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