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.     }  






java web 中 读取windows图标并显示的更多相关文章

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

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

  2. Java web中常见编码乱码问题(二)

    根据上篇记录Java web中常见编码乱码问题(一), 接着记录乱码案例: 案例分析:   2.输出流写入内容或者输入流读取内容时乱码(内容中有中文) 原因分析: a. 如果是按字节写入或读取时乱码, ...

  3. Java Web 中使用ffmpeg实现视频转码、视频截图

    Java Web 中使用ffmpeg实现视频转码.视频截图 转载自:[ http://www.cnblogs.com/dennisit/archive/2013/02/16/2913287.html  ...

  4. Java web中常见编码乱码问题(一)

    最近在看Java web中中文编码问题,特此记录下. 本文将会介绍常见编码方式和Java web中遇到中文乱码问题的常见解决方法: 一.常见编码方式: 1.ASCII 码 众所周知,这是最简单的编码. ...

  5. JDBC在Java Web中的应用

    JDBC在Java Web中的应用 制作人:全心全意 在Java Web开发中,JDBC的应用十分广泛.通常情况下,Web程序操作数据库都是通过JDBC实现,即使目前数据库方面的开源框架层出不穷,但其 ...

  6. Redis(十四)Redis 在Java Web 中的应用

    在传统的 Java Web 项目中,使用数据库进行存储数据,但是有一些致命的弊端,这些弊端主要来自于性能方面. 由于数据库持久化数据主要是面向磁盘,而磁盘的读/写比较慢,在一般管理系统中,由于不存在高 ...

  7. java IO流读取图片供前台显示

    最近项目中需要用到IO流来读取图片以提供前台页面展示,由于以前一直是用url路径的方式进行图片展示,一听说要项目要用IO流读取图片感觉好复杂一样,但任务下达下来了,做为程序员只有选择去执行喽,于是找了 ...

  8. 【中文乱码】深入分析 Java Web 中的中文编码问题

    深入分析 Java Web 中的中文编码问题 1.几种常见的编码格式 1.1 为什么要编码 在计算机中存储信息的最小单元是 1 个字节,即 8 个 bit, 所以能表示的字符范围是 0 ~ 255 个 ...

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

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

随机推荐

  1. iOS 音乐播放器之锁屏效果+歌词解析

    概述 功能描述:锁屏歌曲信息.控制台远程控制音乐播放:暂停/播放.上一首/下一首.快进/快退.列表菜单弹框和拖拽控制台的进度条调节进度(结合了QQ音乐和网易云音乐在锁屏状态下的效果).歌词解析并随音乐 ...

  2. 执行Android项目时指定特定的AVD进行測试

    一个Androidproject空间能够创建一个或多个AVD来对指定的Android项目进行測试,假设仅仅创建了一个AVD则执行Android项目时自然启动该AVD,但是假设创建了多个AVD那么我们该 ...

  3. 【laravel5.4】vue分页删除

    1.a标签执行ajax删除,后台删除成功后,执行vue分页对象删除对应数据 VUE.js

  4. 利用Windows 2003系统中实现两个网段的路由

    利用Windows 2003系统中实现两个网段的路由 当一个局域网中存在两个以上网段时,分属于不同网段内的主机彼此互不可见.为了解决这个问 题,就必须在不同的网段之间设置路由器.如果花费上万元资金购买 ...

  5. ubuntu(14.04) remote access(远程连接数据库)

    1.修改mysql的配置文件.   /etc/mysql/my.cnf 把 bind-address 的那行代码注释掉,保存退出,重启mysql

  6. C#中Windows Media Player控件使用实例|方法

    Windows Media Player控件Windows Media Player是一种媒体播放器,可以播放当前最流行的音频.视频文件和大多数混合型的多媒体文件.为了便于程序的开发,Visual S ...

  7. 【php】基础学习3

    本节主要是php中函数的学习: <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta http-equiv=Con ...

  8. Python学习笔记015——文件file的常规操作之三(标准输入输出文件)

    1 标准输入输出文件 在Python中,模块sys中含有标准的输入输出文件 sys.stdin     标准输入方法(一般是键盘) sys.stdout   标准输出方法(到显示器的缓冲输出) sys ...

  9. fcntl 函数与文件锁

    一.fcntl函数 功能:操纵文件描述符,改变已打开的文件的属性 int fcntl(int fd, int cmd, ... /* arg */ ); cmd的取值可以如下: 复制文件描述符 F_D ...

  10. mark Pay http://git.oschina.net/littleCrazy/dianshangpingtai-zhifu/blob/master/OrderPayController

    @Controller @RequestMapping("/api/pay/") public class OrderPayController extends BaseContr ...