java web 中 读取windows图标并显示
package utils;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.GraphicsConfiguration;import java.awt.GraphicsDevice;import java.awt.GraphicsEnvironment;import java.awt.HeadlessException;import java.awt.Image;import java.awt.Transparency;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.awt.image.PixelGrabber;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import javax.swing.Icon;import javax.swing.ImageIcon;import sun.awt.shell.ShellFolder;public class CommonTool {public static BufferedImage getImageByFileTyle(String filename)throws FileNotFoundException {File file = null;String extension = filename.substring(filename.lastIndexOf(".")).toLowerCase();try {file = File.createTempFile("icon", extension);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return toBufferedImage(toImage(toIcon(file)));}public static Icon toIcon(File file) throws FileNotFoundException {ShellFolder shellFolder = ShellFolder.getShellFolder(file);Icon icon = new ImageIcon(shellFolder.getIcon(true));return icon;}public static Image toImage(Icon icon) {if (icon instanceof ImageIcon) {return ((ImageIcon) icon).getImage();} else {int w = icon.getIconWidth();int h = icon.getIconHeight();GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();GraphicsDevice gd = ge.getDefaultScreenDevice();GraphicsConfiguration gc = gd.getDefaultConfiguration();BufferedImage image = gc.createCompatibleImage(w, h);Graphics2D g = image.createGraphics();icon.paintIcon(null, g, 0, 0);g.dispose();return image;}}private static boolean hasAlpha(Image image) {// If buffered image, the color model is readily availableif (image instanceof BufferedImage) {BufferedImage bimage = (BufferedImage) image;return bimage.getColorModel().hasAlpha();}// Use a pixel grabber to retrieve the image's color model;// grabbing a single pixel is usually sufficientPixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);try {pg.grabPixels();} catch (InterruptedException e) {}// Get the image's color modelColorModel cm = pg.getColorModel();return cm.hasAlpha();}// This method returns a buffered image with the contents of an imagepublic static BufferedImage toBufferedImage(Image image) {if (image instanceof BufferedImage) {return (BufferedImage) image;}// This code ensures that all the pixels in the image are loadedimage = new ImageIcon(image).getImage();// Determine if the image has transparent pixels; for this method's// implementation, see Determining If an Image Has Transparent Pixelsboolean hasAlpha = hasAlpha(image);// Create a buffered image with a format that's compatible with the// screenBufferedImage bimage = null;GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();try {// Determine the type of transparency of the new buffered imageint transparency = Transparency.OPAQUE;if (hasAlpha) {transparency = Transparency.BITMASK;}// Create the buffered imageGraphicsDevice gs = ge.getDefaultScreenDevice();GraphicsConfiguration gc = gs.getDefaultConfiguration();bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);} catch (HeadlessException e) {// The system does not have a screen}if (bimage == null) {// Create a buffered image using the default color modelint type = BufferedImage.TYPE_INT_RGB;if (hasAlpha) {type = BufferedImage.TYPE_INT_ARGB;}bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);}// Copy image to buffered imageGraphics g = bimage.createGraphics();// Paint the image onto the buffered imageg.drawImage(image, 0, 0, null);g.dispose();return bimage;}}
<img src="uploaddispalyIcon?dirName=<%=pathdir+"upload/"%>${name}" style="width:15px;height:15px;"/>public void dispalyIcon() {HttpServletResponse response = ServletActionContext.getResponse();response.setContentType("image/png");BufferedImage myImage=null;OutputStream sos =null;try {sos = response.getOutputStream();myImage = CommonTool.getImageByFileTyle(dirName);ImageIO.write(myImage, "png", sos);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(null!=myImage&&null!=sos){try {myImage.flush();sos.flush();sos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
java web 中 读取windows图标并显示的更多相关文章
- java web中读取properties文件时的路径问题
在web开发时,难免会有一些固定的参数,我们一般把这些固定的参数存在properties文件中,然后用的时候要读出来.但经常出现一些错误,找不到相应的路径,所以,今天特地讲一些如何正确获得路径. 首先 ...
- Java web中常见编码乱码问题(二)
根据上篇记录Java web中常见编码乱码问题(一), 接着记录乱码案例: 案例分析: 2.输出流写入内容或者输入流读取内容时乱码(内容中有中文) 原因分析: a. 如果是按字节写入或读取时乱码, ...
- Java Web 中使用ffmpeg实现视频转码、视频截图
Java Web 中使用ffmpeg实现视频转码.视频截图 转载自:[ http://www.cnblogs.com/dennisit/archive/2013/02/16/2913287.html ...
- Java web中常见编码乱码问题(一)
最近在看Java web中中文编码问题,特此记录下. 本文将会介绍常见编码方式和Java web中遇到中文乱码问题的常见解决方法: 一.常见编码方式: 1.ASCII 码 众所周知,这是最简单的编码. ...
- JDBC在Java Web中的应用
JDBC在Java Web中的应用 制作人:全心全意 在Java Web开发中,JDBC的应用十分广泛.通常情况下,Web程序操作数据库都是通过JDBC实现,即使目前数据库方面的开源框架层出不穷,但其 ...
- Redis(十四)Redis 在Java Web 中的应用
在传统的 Java Web 项目中,使用数据库进行存储数据,但是有一些致命的弊端,这些弊端主要来自于性能方面. 由于数据库持久化数据主要是面向磁盘,而磁盘的读/写比较慢,在一般管理系统中,由于不存在高 ...
- java IO流读取图片供前台显示
最近项目中需要用到IO流来读取图片以提供前台页面展示,由于以前一直是用url路径的方式进行图片展示,一听说要项目要用IO流读取图片感觉好复杂一样,但任务下达下来了,做为程序员只有选择去执行喽,于是找了 ...
- 【中文乱码】深入分析 Java Web 中的中文编码问题
深入分析 Java Web 中的中文编码问题 1.几种常见的编码格式 1.1 为什么要编码 在计算机中存储信息的最小单元是 1 个字节,即 8 个 bit, 所以能表示的字符范围是 0 ~ 255 个 ...
- 文件_ _android从资源文件中读取文件流并显示的方法
======== 1 android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private ...
随机推荐
- POJ--2752--Seek the Name, Seek the Fame【KMP】
链接:http://poj.org/problem? id=2752 题意:对于一个字符串S,可能存在前n个字符等于后n个字符,从小到大输出这些n值. 思路:这道题加深了对next数组的理解.next ...
- iOS-高仿微信摇一摇动画效果加震动音效
概述 摇一摇动画效果 (加震动音效) 详细 代码下载:http://www.demodashi.com/demo/10707.html 众所周知, 微信中的摇一摇功能: 搜索人/歌曲/电视,同样在一些 ...
- HDUOJ-----4512吉哥系列故事——完美队形I(LCIS)
吉哥系列故事——完美队形I Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- eclipse代码格式化设置
http://www.cnblogs.com/zhxiaomiao/archive/2010/06/19/1760995.html java---code style ---formatter 首先新 ...
- iOS - Contacts 通讯录
Contacts 通讯录 1.访问通讯录 设置系统访问通讯录权限 1.1 iOS 9.0 及 iOS 9.0 之后获取通讯录的方法 iOS 9.0 及 iOS 9.0 之后获取通讯录的方法 // 包含 ...
- mongodb 安装教学
安装文件:mongodb-win32-x86_64-2008plus-ssl-3.2.6-signed.msi 电脑配置:win7 64位 MongoDB的安装很简单,设置好安装路径后,一直Next直 ...
- Oschina 安卓client源代码学习之中的一个
今天主要研究一下两个功能 (1)双击返回键退出程序 (2)接近完美地退出程序 (1) 在非常多应用程序里都有一个功能,就是点击返回键,之后提示你再点击返回键就退出程序. 之前一直非常好奇这是怎么实现的 ...
- activiti表
act_re_deployment #部署对象表 act_re_prodef #流程定义表 act_ge_bytearray #资源文件表 act_ge_property #主键生成策略表 ac ...
- 使用jQuery的ajax同步请求吃过的亏
jQuery是一个非经常常使用的js库.甚至我们开发不论什么一个项目都首先把jquery导入进行.jQuery太过强大,使用起来非常方便.可是在使用的过程中也遇到过非常多坑.我在这里分享一下.避免大家 ...
- Java学习笔记----main
1.java中main方法是程序的入口方法.main还是必须的两个修饰符是public和static.举例 A:public static void main(String args[]) B:pub ...