日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

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

發(fā)布時間:2024/9/27 windows 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java获取系统文件类型图标并显示在JSP上 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在網(wǎng)站制作中通常需要上傳附件,而對于附件我們往往希望在其名稱前面有類似于Windows系統(tǒng)中的類型圖標(biāo),那么怎么根據(jù)附件的類型來顯示不同的圖標(biāo)呢?目前有兩種解決方案:? 第一種:將所有類型文件的圖標(biāo)圖片放置到項目中,然后通過分析文件的擴(kuò)展名來調(diào)用相應(yīng)的圖片,這種方式比較簡單常見,但是我們往往無法弄到所有文件類型的圖標(biāo),而且也不能排除意外情況的出現(xiàn),這里就不在介紹了;? 第二種:通過java調(diào)用系統(tǒng)的文件類型圖標(biāo)然后顯示出來,好處是可以顯示跟操作系統(tǒng)中一模一樣的圖標(biāo),但是要復(fù)雜一些,下面詳細(xì)介紹。? 1、JSP? Java代碼 ? <img src="fileAction!dispalyIcon?dirName=<%=request.getAttribute("fileName").toString()%>" style="width:16px;height:16px;"/> ? 2、fileAction? Java代碼 ? @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? Java代碼 ? 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? Java代碼 ? <action name="fileAction" class="com.render.action.cyl.FileAction"> ? <result name="input">login.jsp</result> ? </action>

總結(jié)

以上是生活随笔為你收集整理的Java获取系统文件类型图标并显示在JSP上的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。