java显示图片

1、base64编码显示;但是尽量避免使用该方式,因为可能存在丢失内容情况;

2、使用servlet获取图片,以流的形式显示。serlvet路径最好带个随机数,防止页面缓存问题。

方法1:

        byte[] data = getImageStr(bean.getImgPath());
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        String imgStr = "";
        if(data!=null){
            try {
                imgStr ="data:image/jpg;base64," + encoder.encode(data) + "";
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    /**
     * 图片转化成base64字符串
     * 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
     * @param path
     * @return
     */
    public static byte[] getImageStr(String path) {
        if (!new File(path).exists()) {
            return null;
        }
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(path);
            data = new byte[in.available()];
            in.read(data);
            in.close();
            // 对字节数组Base64编码
//            BASE64Encoder encoder = new BASE64Encoder();
//            return encoder.encode(data); // 返回Base64编码过的字节数组字符串
            return data; // 返回Base64编码过的字节数组字符串
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
<img src='"+imgStr+"' style='width:112px;height:125px;'/>

方法2:

   java.util.Random random=new java.util.Random();// 定义随机类
    int ran = random.nextInt(10);// 返回[0,10)集合中的整数,注意不包括10

"<img src='/XX(项目名)/zpservlet?n="+ran+"&path="+bean.getImgPath()+"' style='width:112px;height:125px;'/>"
     /**
     * 文件路径
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException,
                                                             IOException {
          response.setContentType(CONTENT_TYPE);
          request.setCharacterEncoding("UTF-8");
          String path = request.getParameter("path");
          System.out.println("path:"+path);
          InputStream in = null;
          OutputStream out = null;
          
              if(path != null && !"".equals(path)){
                  File outputFile = new File(path);
                  if (outputFile.exists() && outputFile.length() > 0) {
                      in = new FileInputStream(outputFile);
                      out = response.getOutputStream();
                      IOUtils.copy(in, out);
    //                      byte[] buffer = new byte[1024];
    //                      int read = -1;
    //                          while ((read = in.read(buffer)) != -1) {
    //                          out.write(buffer, 0, read);
    //                      }
                      out.close();
                     // if(in!=null)in.close();
                  } else {
                      System.out.println("找不到文件:" + outputFile.getName());
                  }
              } 
              
          }

猜你喜欢

转载自563432906.iteye.com/blog/2324020
今日推荐