Java获取系统文件类型图标并显示在JSP上
生活随笔
收集整理的這篇文章主要介紹了
Java获取系统文件类型图标并显示在JSP上
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在網站制作中通常需要上傳附件,而對于附件我們往往希望在其名稱前面有類似于Windows系統中的類型圖標,那么怎么根據附件的類型來顯示不同的圖標呢?目前有兩種解決方案:?
第一種:將所有類型文件的圖標圖片放置到項目中,然后通過分析文件的擴展名來調用相應的圖片,這種方式比較簡單常見,但是我們往往無法弄到所有文件類型的圖標,而且也不能排除意外情況的出現,這里就不在介紹了;?
第二種:通過java調用系統的文件類型圖標然后顯示出來,好處是可以顯示跟操作系統中一模一樣的圖標,但是要復雜一些,下面詳細介紹。?
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>
總結
以上是生活随笔為你收集整理的Java获取系统文件类型图标并显示在JSP上的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天翼橙分期什么意思
- 下一篇: Java中几种输出数组的for循环,以及