java web中读取windows对应文件名的 系统图标 。。。。显示
1.获取系统图标工具类
  1. package utils; 
  2. import java.awt.Graphics; 
  3. import java.awt.Graphics2D; 
  4. import java.awt.GraphicsConfiguration; 
  5. import java.awt.GraphicsDevice; 
  6. import java.awt.GraphicsEnvironment; 
  7. import java.awt.HeadlessException; 
  8. import java.awt.Image; 
  9. import java.awt.Transparency; 
  10. import java.awt.image.BufferedImage; 
  11. import java.awt.image.ColorModel; 
  12. import java.awt.image.PixelGrabber; 
  13. import java.io.File; 
  14. import java.io.FileNotFoundException; 
  15. import java.io.IOException; 
  16. import javax.swing.Icon; 
  17. import javax.swing.ImageIcon; 
  18. import sun.awt.shell.ShellFolder; 
  19. public class CommonTool {   
  20.     public static BufferedImage getImageByFileTyle(String filename)   
  21.             throws FileNotFoundException {   
  22.         File file = null;   
  23.         String extension = filename.substring(filename.lastIndexOf("."))   
  24.                 .toLowerCase();   
  25.         try {   
  26.             file = File.createTempFile("icon", extension);   
  27.         } catch (IOException e) {   
  28.             // TODO Auto-generated catch block   
  29.             e.printStackTrace();   
  30.         }   
  31.         return toBufferedImage(toImage(toIcon(file)));   
  32.    
  33.     }   
  34.    
  35.     public static Icon toIcon(File file) throws FileNotFoundException {   
  36.         ShellFolder shellFolder = ShellFolder.getShellFolder(file);   
  37.         Icon icon = new ImageIcon(shellFolder.getIcon(true));   
  38.         return icon;   
  39.     }   
  40.    
  41.     public static Image toImage(Icon icon) {   
  42.         if (icon instanceof ImageIcon) {   
  43.             return ((ImageIcon) icon).getImage();   
  44.         } else {   
  45.             int w = icon.getIconWidth();   
  46.             int h = icon.getIconHeight();   
  47.             GraphicsEnvironment ge = GraphicsEnvironment   
  48.                     .getLocalGraphicsEnvironment();   
  49.             GraphicsDevice gd = ge.getDefaultScreenDevice();   
  50.             GraphicsConfiguration gc = gd.getDefaultConfiguration();   
  51.             BufferedImage image = gc.createCompatibleImage(w, h);   
  52.             Graphics2D g = image.createGraphics();   
  53.             icon.paintIcon(null, g, 0, 0);   
  54.             g.dispose();   
  55.             return image;   
  56.         }   
  57.     }   
  58.    
  59.     private static boolean hasAlpha(Image image) {   
  60.         // If buffered image, the color model is readily available   
  61.         if (image instanceof BufferedImage) {   
  62.             BufferedImage bimage = (BufferedImage) image;   
  63.             return bimage.getColorModel().hasAlpha();   
  64.         }   
  65.    
  66.         // Use a pixel grabber to retrieve the image's color model;   
  67.         // grabbing a single pixel is usually sufficient   
  68.         PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);   
  69.         try {   
  70.             pg.grabPixels();   
  71.         } catch (InterruptedException e) {   
  72.         }   
  73.    
  74.         // Get the image's color model   
  75.         ColorModel cm = pg.getColorModel();   
  76.         return cm.hasAlpha();   
  77.     }   
  78.    
  79.     // This method returns a buffered image with the contents of an image   
  80.     public static BufferedImage toBufferedImage(Image image) {   
  81.         if (image instanceof BufferedImage) {   
  82.             return (BufferedImage) image;   
  83.         }   
  84.    
  85.         // This code ensures that all the pixels in the image are loaded   
  86.         image = new ImageIcon(image).getImage();   
  87.    
  88.         // Determine if the image has transparent pixels; for this method's   
  89.         // implementation, see Determining If an Image Has Transparent Pixels   
  90.         boolean hasAlpha = hasAlpha(image);   
  91.    
  92.         // Create a buffered image with a format that's compatible with the   
  93.         // screen   
  94.         BufferedImage bimage = null;   
  95.         GraphicsEnvironment ge = GraphicsEnvironment   
  96.                 .getLocalGraphicsEnvironment();   
  97.         try {   
  98.             // Determine the type of transparency of the new buffered image   
  99.             int transparency = Transparency.OPAQUE;   
  100.             if (hasAlpha) {   
  101.                 transparency = Transparency.BITMASK;   
  102.             }   
  103.    
  104.             // Create the buffered image   
  105.             GraphicsDevice gs = ge.getDefaultScreenDevice();   
  106.             GraphicsConfiguration gc = gs.getDefaultConfiguration();   
  107.             bimage = gc.createCompatibleImage(image.getWidth(null), image   
  108.                     .getHeight(null), transparency);   
  109.         } catch (HeadlessException e) {  
  110.             // The system does not have a screen   
  111.         }   
  112.    
  113.         if (bimage == null) {   
  114.             // Create a buffered image using the default color model   
  115.             int type = BufferedImage.TYPE_INT_RGB;   
  116.             if (hasAlpha) {   
  117.                 type = BufferedImage.TYPE_INT_ARGB;   
  118.             }   
  119.             bimage = new BufferedImage(image.getWidth(null), image   
  120.                     .getHeight(null), type);   
  121.         }   
  122.    
  123.         // Copy image to buffered image   
  124.         Graphics g = bimage.createGraphics();   
  125.    
  126.         // Paint the image onto the buffered image   
  127.         g.drawImage(image, 0, 0, null);   
  128.         g.dispose();   
  129.    
  130.         return bimage;   
  131.     }   
  132. }   
2.调用
  1. <img src="uploaddispalyIcon?dirName=<%=pathdir+"upload/"%>${name}" style="width:15px;height:15px;"/> 
  2. public void dispalyIcon() {   
  3.         HttpServletResponse response = ServletActionContext.getResponse();   
  4.         response.setContentType("image/png");  
  5.         BufferedImage myImage=null; 
  6.         OutputStream sos =null; 
  7.         try {   
  8.              sos = response.getOutputStream();   
  9.             myImage = CommonTool.getImageByFileTyle(dirName);   
  10.             ImageIO.write(myImage, "png", sos);   
  11.               
  12.         } catch (IOException e) {   
  13.             // TODO Auto-generated catch block   
  14.             e.printStackTrace();   
  15.         }finally{ 
  16.          if(null!=myImage&&null!=sos){ 
  17.            
  18.                 try { 
  19.                  myImage.flush(); 
  20.      sos.flush(); 
  21.       sos.close();  
  22.     } catch (IOException e) { 
  23.      // TODO Auto-generated catch block 
  24.      e.printStackTrace(); 
  25.     }   
  26.                 
  27.          } 
  28.         } 
  29.            
  30.     }  





转载于:https://www.cnblogs.com/signheart/p/6595603.html

java web 中 读取windows图标并显示相关推荐

  1. java web中读取properties文件时的路径问题

    在web开发时,难免会有一些固定的参数,我们一般把这些固定的参数存在properties文件中,然后用的时候要读出来.但经常出现一些错误,找不到相应的路径,所以,今天特地讲一些如何正确获得路径. 首先 ...

  2. Java中从指定文本文件中读取内容,并显示到屏幕上。

    [问题描述]从指定文本文件test.txt中读取内容,并显示到屏幕上. [输入形式]当前目录下的文本文件 test.txt ,内容可能如下: 在完成这个问题的过程中学到了很多,比如相对路径与绝对路径的 ...

  3. Java Web中的中文编码问题分析

    一.为什么需要编码 在计算机中存储信息的最小单位是1个字节,即8bit,所以能标识的最大字符范围是0~255,而人类自然语言中例如汉语.日语要表示的符号太多,无法单纯用一个字节来完全表示,为了解决这个 ...

  4. java web 添加超链接_Javaweb 超链接后显示问题

    java web 项目发送带有超链接文本邮件问题 今天做java web项目的时候遇到了一个很想不通的或者说很奇葩的问题, 小编首先需要设置邮件内容的格式为:html 其次在内容中加上不要太乖,不想做 ...

  5. 深入分析 Java Web 中的中文编码问题

    深入分析 Java Web 中的中文编码问题 背景: 编码问题一直困扰着程序开发人员,尤其是在 Java 中更加明显,因为 Java 是跨平台的语言,在不同平台的编码之间的切换较多.接下来将介绍 Ja ...

  6. Java Web中的EL(表达式语言)详解

     Java Web中的EL(表达式语言)详解 表达式语言(Expression Language)简称EL,它是JSP2.0中引入的一个新内容.通过EL可以简化在JSP开发中对对象的引用,从而规范页面 ...

  7. java web 中有效解决中文乱码问题-pageEncoding与charset区别, response和request的setCharacterEncoding 区别

    java web 中有效解决中文乱码问题-pageEncoding与charset区别, response和request的setCharacterEncoding 区别 参考文章: (1)java ...

  8. 文件_ _android从资源文件中读取文件流并显示的方法

    ======== 1   android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private ...

  9. java 控制jsp_JSP学习之Java Web中的安全控制实例详解

    普通用户界面 修改登录的Servlet,修改后的代码如下: LoginProcess.java代码: package servlet; import javabean.User; import jav ...

最新文章

  1. 使用最新版(2020)IntelliJ IDEA 创建Servlet项目
  2. EF Core Model更新迁移
  3. 三次握手,四次挥手的过程??为什么三握??
  4. html app从上向下弹框,移动端从底部向上过渡弹出弹框
  5. org.hibernate.HibernateException: No Session found for current thread
  6. 用FFmpeg从视频截取任意一帧图片的解决办法~
  7. laravel 队列学习
  8. 磊哥私藏书单分享,160买400的书!
  9. 完整的可按年份和月份查询数据并显示
  10. mysql function DATE_FORMA T(date, format)
  11. 同时面了腾讯三个部门,拿下 offer!
  12. Thread 相关函数和属性
  13. c语言函数调用费波那楔数列,【算法】费波那契数列算法
  14. CCIE试验备考之冗余备份HSRP
  15. 华为鸿蒙2048小游戏,从零开始使用华为DevEco Studio编写2048小游戏
  16. java.lang.IllegalArgumentException: Can not set xx field xx to jav问题解决
  17. react-native android打包失败: GC overhead limit exceeded
  18. 立志高远;毕业后计划
  19. maven修改为阿里巴巴的仓库地址
  20. jsp简易的图书管理系统

热门文章

  1. .NET Core 3 Preview 2 发布,C# 8 更强大的模式匹配
  2. mybatis-generator配置流程(详细) 2021-05-15
  3. stm32时钟树_STM32中的时钟
  4. 卸载idea2020删除以前的配置_推荐一款只有5M大小的绿色良心的卸载工具!
  5. oracle视图查询机制,物化视图及日志内部机制的一点研究
  6. pyqt 实现控件移除_pyqt5:删除树控件(QTreeWidget)的子节点/根节点
  7. edge浏览器如何把网页放到桌面_电脑如何添加便签,便签怎么放到桌面上
  8. oracle11g调整表空间和临时表空间大小
  9. javascript判断日期奇偶_JavaScript_简介学习4
  10. 计算机试讲教案模板范文,试讲教案模板1.doc