在网站制作中通常需要上传附件,而对于附件我们往往希望在其名称前面有类似于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获取double类型区间随机数
获得0.68-6.88的随机数 前提:java获取double类型区间随机数 *** 获取0.68-6.88之间的随机数* @return*/ public static Double queryHo ...
- java获取Date类型时间的前3个月,后3个月,前3天,后3天
java获取Date类型时间的前3个月,后3个月,前3天,后3天 Calendar cal = Calendar.getInstance(); Date date = new Date(); cal. ...
- java的mime类型_常用的mimeType,以及java获取mime类型
将文件系统上的文件获取了这些mimeType gif : image/gif bmp : image/bmp ico : image/x-ico jpeg : image/jpeg jpg : ima ...
- java 获取泛型的属性_java在泛型类 T 上利用反射取属性值
记录一次反射的使用,第一次真正运用到实际工作中,发现反射是真的强大! 写了一个通用类,主要是便于和 db 的交互操作,使用到了泛型 T 对象,但是有一步需要获取该对象的属性 id 值(前提是所有赋值给 ...
- Java获取文件类型的5种方法
前言 工作中经常会用到,判断一个文件的文件类型,这里总结一把,一般判断文件类型的原理有2种方式: 根据文件扩展名判断 优点:速度快,代码简单 缺点:无法判断出真实的文件类型,例如一些伪造的文件或者没有 ...
- java获取Timestamp类型的当前系统时间
方法1: Timestamp d = new Timestamp(System.currentTimeMillis()); 方法2: Date date = new Date(); Timestamp ...
- java点击菜单项文字显示在窗体上_java点击菜单项弹出窗口怎么做啊?
程序如下,可弹不出窗口呀,高手教教我!importjava.awt.*;importjava.awt.event.*;publicclassW111extendsFrameimplementsActi ...
- 微信小程序获取用户位置信息并显示到地图上
1.配置地理位置用途说明 在app.json中的大括号内输入 "permission": {"scope.userLocation": {"desc& ...
- Java获取24小时之前的时间点
Java 获取24小时之前的时间点 直接上代码 import java.text.DateFormat; import java.text.SimpleDateFormat; import java. ...
最新文章
- Yahoo为啥赚不到钱
- 软件工程python就业方向-月薪2万+的Python Web岗,学到什么程度能找到工作?
- python3.7下载安装教程-CentOS 7 下 安装 Python3.7
- java 制作报表案例_javaweb项目报表案例
- DVWA--文件上传漏洞
- python sanic orm_Sanic + 前端MVVM 一种新一代Python高性能全栈开发实践
- WinDbg实战调试命令笔记
- Linux用户管理(五)Linux系统的启动
- Qt:Qt实现飞秋拦截助手—Mac地址扫描器
- Effective Use of Word Order for Text Categorization with Convolutional Neural Networks
- Sublime Text 3常用插件安装(持续更新)
- 反编译工具Reflector下载(集成两个常用.net插件,FileGenerator和FileDisassembler)
- 小程序上传图片前将图片剪切成固定尺寸
- 新在线一键制作表白网系统源码
- 相机模型--针孔相机投影(pinhole camera model)
- linux控制NVme硬盘点灯,一种实现多NVMe硬盘背板点灯的设计方法与流程
- Oracle题目求帮助
- python 正则表达式 断言 不定长表达式_【教程】详解Python正则表达式之: (?!…) negative lookahead assertion 前向否定匹配 /前向否定断言...
- 计算机考研跨审计好跨嘛,跨专业如何考研
- 英语构词法-learning
热门文章
- JavaScript实现k-Means算法(附完整源码)
- boost::program_options模块实现处理响应文件的测试程序
- boost::mp11::mp_flatten相关用法的测试程序
- boost::graph模块演示 GGCL Edge 接口
- boost::function模块boost::lambda::bind用法的测试程序
- boost::format模块测试 wchar_t 格式的使用
- boost::fibers::buffered_channel的测试程序
- boost::describe模块实现嵌套枚举的测试程序
- GDCM:gdcm::FileAnonymizer的测试程序
- ITK:像素是否在区域内