java下的大型网站对图片的下载,存放,及压缩管理

news/2024/5/15 16:12:29/文章来源:https://blog.csdn.net/LanSeTianKong12/article/details/46473395

大型网站对图片的下载,存放,及压缩管理

构建保存图片的路径:

1 String pathdir = "/images/product/"+ productTypeId+ "/"+ productId+ "/prototype";//构建文件保存的目录  

为什么要有那么多个目录,因为java本身不会去获取图片,而是调用了操作系统的一些接口来获取图片,如果一个目录下图片太多的话,操作系统获取图片的速度会变慢 ,所以巴巴运动网在构建图片路径的时候搞了多个目录,分散保存图片。

 

有了这个pathdir就可以得到图片保存目录的真实路径:

1 String realpathdir = request.getSession().getServletContext().getRealPath(pathdir);

获取了图片的真实路径后,就可以开始保存图片了: 

1 File savedir = new File(realpathdir);
2 File file = saveFile(savedir, filename, imagefile.getFileData());

imagefile为struts的FormFile类的对象,filename为文件名,这两个属性都可以从前台获取过来。以下是saveFile方法的代码: 

复制代码
 1 /**
 2      * 保存文件
 3      * @param savedir 存放目录
 4      * @param fileName 文件名称
 5      * @param data 保存的内容
 6      * @return 保存的文件
 7      * @throws Exception
 8 */
 9 public static File saveFile(File savedir, String fileName, byte[] data) throws Exception{
10 if(!savedir.exists()) savedir.mkdirs();//如果目录不存在就创建
11 File file = new File(savedir, fileName);
12 FileOutputStream fileoutstream = new FileOutputStream(file);
13 fileoutstream.write(data);
14 fileoutstream.close();
15 return file;
16 }
复制代码

保存完图片后还要保存一张图片的缩略图,宽度为140px 

 

View Code

这里我们用到了一个从网上下的用于压缩图片的ImageSizer工具类的静态方法,resize方法传进去的四个参数分别代表原始图片对象,需要被压缩的图片对象,压缩宽度的大小,图片后缀名。这个工具类只能压缩jpg, png, gif(非动画)三种格式,如果想压缩更多的格式需要付费。以下是该工具类: 

 

复制代码
  1 /**
  2  * 图像压缩工具
  3  * @author lihuoming@sohu.com
  4  *
  5 */
  6 public class ImageSizer {
  7 public static final MediaTracker tracker = new MediaTracker(new Component() {
  8 private static final long serialVersionUID = 1234162663955668507L;} 
  9 );
 10 /**
 11      * @param originalFile 原图像
 12      * @param resizedFile 压缩后的图像
 13      * @param width 图像宽
 14      * @param format 图片格式 jpg, png, gif(非动画)
 15      * @throws IOException
 16 */
 17 public static void resize(File originalFile, File resizedFile, int width, String format) throws IOException {
 18 if(format!=null && "gif".equals(format.toLowerCase())){
 19 resize(originalFile, resizedFile, width, 1);
 20 return;
 21 }
 22 FileInputStream fis = new FileInputStream(originalFile);
 23 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
 24 int readLength = -1;
 25 int bufferSize = 1024;
 26 byte bytes[] = new byte[bufferSize];
 27 while ((readLength = fis.read(bytes, 0, bufferSize)) != -1) {
 28 byteStream.write(bytes, 0, readLength);
 29 }
 30 byte[] in = byteStream.toByteArray();
 31 fis.close();
 32 byteStream.close();
 33 
 34 Image inputImage = Toolkit.getDefaultToolkit().createImage( in );
 35 waitForImage( inputImage );
 36 int imageWidth = inputImage.getWidth( null );
 37 if ( imageWidth < 1 ) 
 38 throw new IllegalArgumentException( "image width " + imageWidth + " is out of range" );
 39 int imageHeight = inputImage.getHeight( null );
 40 if ( imageHeight < 1 ) 
 41 throw new IllegalArgumentException( "image height " + imageHeight + " is out of range" );
 42 
 43 // Create output image.
 44 int height = -1;
 45 double scaleW = (double) imageWidth / (double) width;
 46 double scaleY = (double) imageHeight / (double) height;
 47 if (scaleW >= 0 && scaleY >=0) {
 48 if (scaleW > scaleY) {
 49 height = -1;
 50 } else {
 51 width = -1;
 52 }
 53 }
 54 Image outputImage = inputImage.getScaledInstance( width, height, java.awt.Image.SCALE_DEFAULT);
 55 checkImage( outputImage ); 
 56 encode(new FileOutputStream(resizedFile), outputImage, format); 
 57 } 
 58 
 59 /** Checks the given image for valid width and height. */
 60 private static void checkImage( Image image ) {
 61 waitForImage( image );
 62 int imageWidth = image.getWidth( null );
 63 if ( imageWidth < 1 ) 
 64 throw new IllegalArgumentException( "image width " + imageWidth + " is out of range" );
 65 int imageHeight = image.getHeight( null );
 66 if ( imageHeight < 1 ) 
 67 throw new IllegalArgumentException( "image height " + imageHeight + " is out of range" );
 68 }
 69 
 70 /** Waits for given image to load. Use before querying image height/width/colors. */
 71 private static void waitForImage( Image image ) {
 72 try {
 73 tracker.addImage( image, 0 );
 74 tracker.waitForID( 0 );
 75 tracker.removeImage(image, 0);
 76 } catch( InterruptedException e ) { e.printStackTrace(); }
 77 } 
 78 
 79 /** Encodes the given image at the given quality to the output stream. */
 80 private static void encode( OutputStream outputStream, Image outputImage, String format ) 
 81 throws java.io.IOException {
 82 int outputWidth = outputImage.getWidth( null );
 83 if ( outputWidth < 1 ) 
 84 throw new IllegalArgumentException( "output image width " + outputWidth + " is out of range" );
 85 int outputHeight = outputImage.getHeight( null );
 86 if ( outputHeight < 1 ) 
 87 throw new IllegalArgumentException( "output image height " + outputHeight + " is out of range" );
 88 
 89 // Get a buffered image from the image.
 90 BufferedImage bi = new BufferedImage( outputWidth, outputHeight,
 91 BufferedImage.TYPE_INT_RGB ); 
 92 Graphics2D biContext = bi.createGraphics();
 93 biContext.drawImage( outputImage, 0, 0, null );
 94 ImageIO.write(bi, format, outputStream);
 95 outputStream.flush(); 
 96 } 
 97 
 98 /**
 99      * 缩放gif图片
100      * @param originalFile 原图片
101      * @param resizedFile 缩放后的图片
102      * @param newWidth 宽度
103      * @param quality 缩放比例 (等比例)
104      * @throws IOException
105 */
106 private static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException {
107 if (quality < 0 || quality > 1) {
108 throw new IllegalArgumentException("Quality has to be between 0 and 1");
109 } 
110 ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
111 Image i = ii.getImage();
112 Image resizedImage = null; 
113 int iWidth = i.getWidth(null);
114 int iHeight = i.getHeight(null); 
115 if (iWidth > iHeight) {
116 resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH);
117 } else {
118 resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);
119 } 
120 // This code ensures that all the pixels in the image are loaded.
121 Image temp = new ImageIcon(resizedImage).getImage(); 
122 // Create the buffered image.
123 BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
124 BufferedImage.TYPE_INT_RGB); 
125 // Copy image to buffered image.
126 Graphics g = bufferedImage.createGraphics(); 
127 // Clear background and paint the image.
128 g.setColor(Color.white);
129 g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
130 g.drawImage(temp, 0, 0, null);
131 g.dispose(); 
132 // Soften.
133 float softenFactor = 0.05f;
134 float[] softenArray = {0, softenFactor, 0, softenFactor, 1-(softenFactor*4), softenFactor, 0, softenFactor, 0};
135 Kernel kernel = new Kernel(3, 3, softenArray);
136 ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
137 bufferedImage = cOp.filter(bufferedImage, null); 
138 // Write the jpeg to a file.
139 FileOutputStream out = new FileOutputStream(resizedFile); 
140 // Encodes image as a JPEG data stream
141 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
142 JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage); 
143 param.setQuality(quality, true); 
144 encoder.setJPEGEncodeParam(param);
145 encoder.encode(bufferedImage);
146 }
147 }
复制代码

允许用户上传文件,那么我们一定要注意安全,如果用户上传了一个jsp文件,而这个文件的上传路径恰好能被用户访问到,那么用户可能会在这个jsp文件里面做一个对网站其他文件的文件操作,可以将文件保存到web-inf下面,如果用户需要下载,我们就写一个servlet读取这个文件,以流的方式返回给用户。 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_801922.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

网站制作的切图技巧

一般的网站制作步骤大体上为&#xff1a;设计效果图–》切图制作静态html模板–》嵌套至CMS&#xff0c;其中&#xff0c;切图虽然是很简单的一个步骤&#xff0c;但其中也有很多技巧&#xff0c;以下是我个人总结出来的几点。 总体上&#xff0c;把握一个原则&#xff0c;能用…

计算机组成原理与jsp总分,基于JSP的计算机组成原理教学网站的设计与开发

随着互联网技术不断的发展和成熟,开展远程教学,开发整体教学系统成为计算机发展的一个全新课题。这种方式打破了时间、空间的限制,有效地实现了教学资源和教学方法的共享、教学过程的交互性、教学内容快速的更新、教学媒体的充分利用。介绍了该教学系统的设计意图及工作原理,论…

Centos6.0下安装mono 4.0和Jexus 5.6.4,并配置运行网站

很多朋友想要在Linux下部署asp.net的网站&#xff0c;但是又苦于不懂配置环境。今天&#xff0c;我在这里给大家讲解一下如何在Centos 7.0下安装最新版的mono 4.0.0 Jexus 5.6.4&#xff0c;并配置运行网站。 首先用ssh连接linux机器&#xff0c;登录root账号。在这里直接用我…

oracle10g 是rac,dbca建库-RAC安装完成 - Oracle10.2.0.4-RAC两节点集群环境搭建_数据库技术_Linux公社-Linux系统门户网站...

Oracle用户下,dbca启动界面&#xff0c;来创建数据库实例&#xff1a;选择节点&#xff1a;配置ORACLE_SID选择ASM存储(ASM磁盘之前已经创建)换成pfile这里提示没有监听&#xff0c;选Yes&#xff0c;会自动配置监听/oracle/product/10.2.0/crs_1/log/dbcc1/crsd/crsd.log日志信…

网站用户单点登录系统解决方案

1 背景 在网站建设的过程中&#xff0c;多个应用系统一般是在不同的时期开发完成的。各应用系统由于功能侧重、设计方法和开发技术有所不同&#xff0c;也就形成了各自独立的用户库和用户认证体系。随着网站的发展&#xff0c;会出现这样的用户群体&#xff1a;以其中的一个用户…

CTFHub-备份文件下载 - 网站源码

上面一些是网页源码文件后缀&#xff0c;下面的是文件名&#xff0c;用他它们排列组合&#xff0c;最终www.zip有文件下载 但三个文件里都没有flag&#xff0c;其中flag文件的文件名有点奇怪&#xff0c;输入到网页上

bugku-网站被黑

网站和网页源代码都没有东西&#xff0c;根据题目提示&#xff0c;应该要扫描一下目录 用AppScan没扫出来 再用御剑扫描试试 第一个是题目的页面&#xff0c;第二个就是后门页面 用字典没爆出来&#xff0c;查了wp是hack&#xff0c;说要用shell专用字典

【ASP.NET 问题】IIS发布网站后出现 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误的解决办法...

新装IIS&#xff0c;然后发布网站&#xff0c;运行出现如下错误提示 处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” 于是去网上找资料&#xff0c;轻松搞定。o(∩_∩)o 哈哈 原因: vs2010默认的是4.0框架&#xff0c;4.0的…

【ASP.NET 问题】IIS发布网站后出现“检测到在集成的托管管道模式下不适用的ASP.NET设置”的解决办法...

系统环境:win7 asp.net4.0网站挂到本地IIS上报错: google一下,发现N页解决方案,但是点进去一看前篇一律的解决方法是.将IIS7 下网站托管管道由继承模式修改为经典模式,这其实是治标不治本,iis7在经典模式下和iis6没有什么两样. 但是你修改以后也许真跑起来了,但是也有可能接着出…

【温故而知新-CSS】使用CSS设计网站导航栏

1.实现背景变换的导航菜单效果预览&#xff1a; 首页公司频道最新动态客房介绍酒店服务休闲娱乐旅行社源代码&#xff1a; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"…

【读书笔记《Bootstrap 实战》】4.企业网站

上一章有对个人作品站点进行一些优化。本章&#xff0c;轮到我们充实这个作品站点了&#xff0c;补充一些项目&#xff0c;从而展示我们的能力。换句话说&#xff0c;我们要构建一个相对复杂的企业网站主页。 下面有几个成功企业的网站&#xff1a; □ Zappos (http://www.zapp…

【读书笔记《Bootstrap 实战》】5.电子商务网站

构建了公司网站之后&#xff0c;接下来就可以考虑设计一个在线商店了。 此次的设计以上一章的设计为基础&#xff0c; 只是添加了一个包含如下元素的新页面&#xff1a; □ 包含商品小图、标题和说明的产品网格&#xff1b; □ 位于左侧的变懒&#xff0c;用于按类别、品牌等筛…

【读书笔记《Bootstrap 实战》】6.单页营销网站

我们已经掌握了很多实用 Bootstrap 的重要技能。现在&#xff0c;是时候拿出更多的创意来帮助客户实现他们全方位在线营销的愿望了。此次将带领大家做一个漂亮的单页高端营销网站。 主要任务如下&#xff1a; □ 一个大型介绍性传送带图片展示区&#xff0c;配有自定义的响应式…

【CSS Demo】网站页面变灰

让网站所有元素变成灰色调&#xff0c;全浏览器支持&#xff0c;使用了滤镜&#xff0c;比较吃性能&#xff0c;建议作临时方案使用。 实现效果&#xff08;点击下面的按钮&#xff09;&#xff1a; 这里放一张图片作为效果展示&#xff1a; 其CSS代码如下&#xff1a; body{-w…

IIS 网站 HTTP 转 HTTPS

最近需要做 http 链接转成 https 链接&#xff0c;所以就去弄了&#xff0c;现在记录下&#xff1a; 1.准备SSL证书 最开始的时候用的是腾讯云的免费证书&#xff0c;有效期1年&#xff0c;但只能绑定一个二级域名。测试成功后&#xff0c;就去阿里云购买了证书。 参考链接&…

课后作业:模拟婚礼网站用户名的输入框 模拟京东关闭广告 模拟下拉菜单 实现开关灯

模拟婚礼网站用户名的输入框 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><style>div {width: 250px;color: #6db8ff;border: #555555 solid 1px;margin: 1px auto;}in…

js 两个值得推荐的网页插件网站

插件使用方法: 点击进入 一. 轮播图插件 链接:https://www.swiper.com.cn 这个网站有各种各样的轮播图效果,种类繁多的程度出乎你的想象,下面列出几个 二. tab栏,焦点图,图片无缝滚动 链接:http://www.superslide2.com/

当网站遭遇DDOS攻击的解决方案及展望

当网站遭遇DDOS攻击的解决方案及展望一、事件发生春节长假刚过完&#xff0c;WEB就出现故障&#xff0c;下午1点吃完回来&#xff0c;立即将桌面解锁并习惯性的检查了Web服务器。通过Web服务器性能监视软件图像显示的向下滑行的红色曲线看到WEB出现问题了。根据上述的问题&…

java+mysql+tomcat MVC框架网站搭建

所有源码都在github上&#xff08;https://github.com/seasonyao/24languages-words-meanings-web&#xff09; 1.配置介绍&#xff1a; Windows8环境下Java&#xff08;1.8.0_121&#xff09;mysqltomcat8.5.24 用的软件intellij idea2017和navicat premium 2.需求 为以上2…

基于WebSphere与Domino的电子商务网站构架分析

本文出自 “李晨光原创技术博客” 博客&#xff0c;谢绝转载&#xff01;