zoukankan      html  css  js  c++  java
  • 对于src路径问题,深层理解的实践。且对于输出流write()两个方法的源码阅读。

    根据昨天的总结,可深层理解图片中src的路径。所以今天实现了一个想法。就是路径写入的是Controller,然后自动去本地找。

    其实就是将电脑的本地图片 显示出来。通过输出流的方式。

    代码如下:

    @RequestMapping(value = "/img/{id}")
        public void img(@PathVariable(value = "id") String id,HttpServletResponse response) {
            File file = new File("D:\img\" + id + ".jpg");
            System.out.println(file);
            try {
                InputStream inputStream = new FileInputStream(file);
                OutputStream outputStream = response.getOutputStream();
                byte[] b = new byte[1024];
                int a = -1;
                while ((a = inputStream.read(b)) != -1) {
                    outputStream.write(b);
                    System.out.println(a);
                }
                response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode("a.jpg", "UTF-8"));
                inputStream.close();
                outputStream.flush();
                outputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    可是无法正常显示。发现打开的文件图片大小为1kb,实际图片为43kb。

    最后发现了是输出流写的过程中,发生的错误。其实,就是方法的问题。

    换成outputStream.write(b,0,a);就好了

    所以接下来,我去读一下write(Byte[]),和write(Byte[],0,length)两个方法的区别。

    • write(byte b[])

    public void write(byte b[]) throws IOException {
            write(b, 0, b.length);
        }

    所以可以看到,此方法内部调用的 write(b, 0, b.length);

     可突然发现,重新用回write(byte b[])方法,依然好用。那我之前是见鬼了么???

    [○・`Д´・ ○]

  • 相关阅读:
    POJ 2348 Euclid's Game【博弈】
    POJ 2484 A Funny Game【博弈】
    HDU 4193 Non-negative Partial Sums【单调队列】
    占坑补题
    Codeforces 658D Bear and Polynomials【数学】
    Codeforces 658C Bear and Forgotten Tree 3【构造】
    Codeforces 658B Bear and Displayed Friends【set】
    POJ 1704 Georgia and Bob【博弈】
    1001. A+B Format

  • 原文地址:https://www.cnblogs.com/miaoww/p/8569479.html
Copyright © 2011-2022 走看看