`
jxdiamond
  • 浏览: 99716 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Java获取系统文件类型图标并显示在JSP上

 
阅读更多
在网站制作中通常需要上传附件,而对于附件我们往往希望在其名称前面有类似于Windows系统中的类型图标,那么怎么根据附件的类型来显示不同的图标呢?目前有两种解决方案:
第一种:将所有类型文件的图标图片放置到项目中,然后通过分析文件的扩展名来调用相应的图片,这种方式比较简单常见,但是我们往往无法弄到所有文件类型的图标,而且也不能排除意外情况的出现,这里就不在介绍了;
第二种:通过java调用系统的文件类型图标然后显示出来,好处是可以显示跟操作系统中一模一样的图标,但是要复杂一些,下面详细介绍。
1、JSP
<img src="fileAction!dispalyIcon?dirName=<%=request.getAttribute("fileName").toString()%>" style="width:16px;height:16px;"/>

2、fileAction
@Component("fileAction")
public class FileAction extends ActionSupport { 
private String dirName;
public String getDirName() {
		return dirName;
	}

	public void setDirName(String dirName) {
		this.dirName = dirName;
	}
public void dispalyIcon() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("image/png");
		try {
			OutputStream sos = response.getOutputStream();
			BufferedImage myImage = CommonTool.getImageByFileTyle(dirName);
			ImageIO.write(myImage, "png", sos);
			sos.flush();
			sos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

3、CommonTool
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;
	}
}

4、struts.xml
<action name="fileAction" class="com.render.action.cyl.FileAction">
	<result name="input">login.jsp</result>
</action>
分享到:
评论

相关推荐

    JAVA上百实例源码以及开源项目

    (1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户可以在终端上直接地使用它,...

    java源码包---java 源码 大量 实例

    (1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户可以在终端上直接地使用它,...

    JAVA上百实例源码以及开源项目源代码

    (1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户可以在终端上直接地使用它,...

    java源码包2

    (1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户可以在终端上直接地使用它,...

    java源码包4

    (1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户可以在终端上直接地使用它,...

    java源码包3

    (1)提高文件的共享性(计算机程序和/或数据),(2)鼓励间接地(通过程序)使用远程计算机,(3)保护用户因主机之间的文件存储系统导致的变化,(4)为了可靠和高效地传输,虽然用户可以在终端上直接地使用它,...

    JSP动态网页制作基础培训教程源代码.rar

    7 sample7.jsp 第8章\ch8 获得文件属性示例 8 file1.txt 第8章\ch8 文本文件 9 sample8.jsp 第8章\ch8 使用FileInputStream类读取文件file1.txt示例 10 sample9.jsp 第8章\ch8 使用FileReader类读取文件file1.txt...

    成百上千个Java 源码DEMO 4(1-4是独立压缩包)

    Java编写的显示器显示模式检测程序 2个目标文件 内容索引:JAVA源码,系统相关,系统信息检测 用JAVA编写了一个小工具,用于检测当前显示器也就是显卡的显示模式,比如分辨率,色彩以及刷新频率等。 Java波浪文字制作...

    成百上千个Java 源码DEMO 3(1-4是独立压缩包)

    Java编写的显示器显示模式检测程序 2个目标文件 内容索引:JAVA源码,系统相关,系统信息检测 用JAVA编写了一个小工具,用于检测当前显示器也就是显卡的显示模式,比如分辨率,色彩以及刷新频率等。 Java波浪文字制作...

    JspRun!社区论坛系统 v6.0 bulid 090424 GBK 安装版.rar

    是飞速创想(北京)科技有限公司推出的一套通用的社区论坛软件系统,用户可以在不需要任何编程的基础上,通过简单的设置和安装,在互联网上搭建起具备完善功能、很强负载能力和可高度定制的论坛服务。JspRun! 的基础...

    JspRun!社区论坛系统 v6.0 bulid 090423 GBK 源码版.rar

    是飞速创想(北京)科技有限公司推出的一套通用的社区论坛软件系统,用户可以在不需要任何编程的基础上,通过简单的设置和安装,在互联网上搭建起具备完善功能、很强负载能力和可高度定制的论坛服务。JspRun! 的基础...

    Jspxcms网站内容管理系统 源码包 v9.0.0.zip

    1、修复:后台右上角小房子图标的前台首页链接在多站点情况下,切换站点后无变化。 2、修复:文档管理标题图、属性图、图集在火狐下点击上传按钮无反应。 3、修复:文件管理搜索功能没有保留当前文件夹路径。 4、...

    网管教程 从入门到精通软件篇.txt

    ISO:根据ISD 9660有关CD-ROM文件系统标准列出CD-ROM上的文件 ISP:X-Internet签字文件 IST:数字跟踪设备文件 ISU:InstallShield卸装脚本 IT:脉冲跟踪系统音乐模块(MOD)文件 ITI:脉冲跟踪系统设备 ITS:...

    java应用软件程序设计

    这里边包括:第1章 Java图形用户界面编程 1 实例1 布局管理 2 实例2 设计软件启动界面 9 实例3 实现多色窗口 11 实例4 切分窗口 13 实例5 丰富多彩的按钮 15 实例6 在窗口中显示背景图 16 实例...

    JAVA web.xml配置详解

    &lt;web-app xmlns="http://java.sun.... 若在Servlet可以使用下列方法来获得:String param_name=getServletContext().getInitParamter("param_name"); --&gt; ... ... ...

    Jspxcms网站内容管理系统 安装包 v9.0.0 tomcat版.zip

    1、修复:后台右上角小房子图标的前台首页链接在多站点情况下,切换站点后无变化。 2、修复:文档管理标题图、属性图、图集在火狐下点击上传按钮无反应。 3、修复:文件管理搜索功能没有保留当前文件夹路径。 4、...

    swing界面设计之JTree

    下载依赖的 JAR 文件并在 Eclipse 的 classpath 中定义这种依赖性 15 创建 to-do 列表:基本的 Swing 和 Spring 应用程序设置 17 创建 MainFrame、Launcher 和 ToDo 类 17 创建 Spring app-context.xml bean 定义...

    DataTables自行封装请求参数和返回数据的零耦合服务端分页示例(PHP&JAVA)

    JAVA:修改user-manage.js,将访问后台的url由"datasource.php"改为"datasource.jsp",然后将WebRoot下的文件放到tomcat下直接运行。也可使用Eclipse直接导入此项目文件。 简要说明: 使用DataTable默认的ajax交互...

    Lerx 网站内容管理系统 v5.5.zip

    用户在模板市场获取模板时,亦是静默下载,完成后网站后台会自动解压到指定位置并自动导入网站系统,用户只需在下载后点击一下设定为默认模板即可。 32.★充份保护模板的制作人的版权。只有在上传的服务器上才可对...

Global site tag (gtag.js) - Google Analytics