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 block
e.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 available
if (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 sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}
// Get the image's color model
ColorModel cm = pg.getColorModel();
return cm.hasAlpha();
}
// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels; for this method's
// implementation, see Determining If an Image Has Transparent Pixels
boolean hasAlpha = hasAlpha(image);
// Create a buffered image with a format that's compatible with the
// screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha) {
transparency = Transparency.BITMASK;
}
// Create the buffered image
GraphicsDevice 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 model
int 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 image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.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 block
e.printStackTrace();
}finally{
if(null!=myImage&&null!=sos){
try {
myImage.flush();
sos.flush();
sos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
转载于:https://www.cnblogs.com/signheart/p/6595603.html
java web 中 读取windows图标并显示相关推荐
- java web中读取properties文件时的路径问题
在web开发时,难免会有一些固定的参数,我们一般把这些固定的参数存在properties文件中,然后用的时候要读出来.但经常出现一些错误,找不到相应的路径,所以,今天特地讲一些如何正确获得路径. 首先 ...
- Java中从指定文本文件中读取内容,并显示到屏幕上。
[问题描述]从指定文本文件test.txt中读取内容,并显示到屏幕上. [输入形式]当前目录下的文本文件 test.txt ,内容可能如下: 在完成这个问题的过程中学到了很多,比如相对路径与绝对路径的 ...
- Java Web中的中文编码问题分析
一.为什么需要编码 在计算机中存储信息的最小单位是1个字节,即8bit,所以能标识的最大字符范围是0~255,而人类自然语言中例如汉语.日语要表示的符号太多,无法单纯用一个字节来完全表示,为了解决这个 ...
- java web 添加超链接_Javaweb 超链接后显示问题
java web 项目发送带有超链接文本邮件问题 今天做java web项目的时候遇到了一个很想不通的或者说很奇葩的问题, 小编首先需要设置邮件内容的格式为:html 其次在内容中加上不要太乖,不想做 ...
- 深入分析 Java Web 中的中文编码问题
深入分析 Java Web 中的中文编码问题 背景: 编码问题一直困扰着程序开发人员,尤其是在 Java 中更加明显,因为 Java 是跨平台的语言,在不同平台的编码之间的切换较多.接下来将介绍 Ja ...
- Java Web中的EL(表达式语言)详解
Java Web中的EL(表达式语言)详解 表达式语言(Expression Language)简称EL,它是JSP2.0中引入的一个新内容.通过EL可以简化在JSP开发中对对象的引用,从而规范页面 ...
- java web 中有效解决中文乱码问题-pageEncoding与charset区别, response和request的setCharacterEncoding 区别
java web 中有效解决中文乱码问题-pageEncoding与charset区别, response和request的setCharacterEncoding 区别 参考文章: (1)java ...
- 文件_ _android从资源文件中读取文件流并显示的方法
======== 1 android从资源文件中读取文件流并显示的方法. 在android中,假如有的文本文件,比如TXT放在raw下,要直接读取出来,放到屏幕中显示,可以这样: private ...
- java 控制jsp_JSP学习之Java Web中的安全控制实例详解
普通用户界面 修改登录的Servlet,修改后的代码如下: LoginProcess.java代码: package servlet; import javabean.User; import jav ...
最新文章
- 使用最新版(2020)IntelliJ IDEA 创建Servlet项目
- EF Core Model更新迁移
- 三次握手,四次挥手的过程??为什么三握??
- html app从上向下弹框,移动端从底部向上过渡弹出弹框
- org.hibernate.HibernateException: No Session found for current thread
- 用FFmpeg从视频截取任意一帧图片的解决办法~
- laravel 队列学习
- 磊哥私藏书单分享,160买400的书!
- 完整的可按年份和月份查询数据并显示
- mysql function DATE_FORMA T(date, format)
- 同时面了腾讯三个部门,拿下 offer!
- Thread 相关函数和属性
- c语言函数调用费波那楔数列,【算法】费波那契数列算法
- CCIE试验备考之冗余备份HSRP
- 华为鸿蒙2048小游戏,从零开始使用华为DevEco Studio编写2048小游戏
- java.lang.IllegalArgumentException: Can not set xx field xx to jav问题解决
- react-native android打包失败: GC overhead limit exceeded
- 立志高远;毕业后计划
- maven修改为阿里巴巴的仓库地址
- jsp简易的图书管理系统
热门文章
- .NET Core 3 Preview 2 发布,C# 8 更强大的模式匹配
- mybatis-generator配置流程(详细) 2021-05-15
- stm32时钟树_STM32中的时钟
- 卸载idea2020删除以前的配置_推荐一款只有5M大小的绿色良心的卸载工具!
- oracle视图查询机制,物化视图及日志内部机制的一点研究
- pyqt 实现控件移除_pyqt5:删除树控件(QTreeWidget)的子节点/根节点
- edge浏览器如何把网页放到桌面_电脑如何添加便签,便签怎么放到桌面上
- oracle11g调整表空间和临时表空间大小
- javascript判断日期奇偶_JavaScript_简介学习4
- 计算机试讲教案模板范文,试讲教案模板1.doc