大型网站对图片的下载,存放,及压缩管理
2012-04-07 21:55
330 查看
构建保存图片的路径:
View Code
允许用户上传文件,那么我们一定要注意安全,如果用户上传了一个jsp文件,而这个文件的上传路径恰好能被用户访问到,那么用户可能会在这个jsp文件里面做一个对网站其他文件的文件操作,可以将文件保存到web-inf下面,如果用户需要下载,我们就写一个servlet读取这个文件,以流的方式返回给用户。
View Code
/** * 图像压缩工具 * @author lihuoming@sohu.com * */ public class ImageSizer { public static final MediaTracker tracker = new MediaTracker(new Component() { private static final long serialVersionUID = 1234162663955668507L;} ); /** * @param originalFile 原图像 * @param resizedFile 压缩后的图像 * @param width 图像宽 * @param format 图片格式 jpg, png, gif(非动画) * @throws IOException */ public static void resize(File originalFile, File resizedFile, int width, String format) throws IOException { if(format!=null && "gif".equals(format.toLowerCase())){ resize(originalFile, resizedFile, width, 1); return; } FileInputStream fis = new FileInputStream(originalFile); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); int readLength = -1; int bufferSize = 1024; byte bytes[] = new byte[bufferSize]; while ((readLength = fis.read(bytes, 0, bufferSize)) != -1) { byteStream.write(bytes, 0, readLength); } byte[] in = byteStream.toByteArray(); fis.close(); byteStream.close(); Image inputImage = Toolkit.getDefaultToolkit().createImage( in ); waitForImage( inputImage ); int imageWidth = inputImage.getWidth( null ); if ( imageWidth < 1 ) throw new IllegalArgumentException( "image width " + imageWidth + " is out of range" ); int imageHeight = inputImage.getHeight( null ); if ( imageHeight < 1 ) throw new IllegalArgumentException( "image height " + imageHeight + " is out of range" ); // Create output image. int height = -1; double scaleW = (double) imageWidth / (double) width; double scaleY = (double) imageHeight / (double) height; if (scaleW >= 0 && scaleY >=0) { if (scaleW > scaleY) { height = -1; } else { width = -1; } } Image outputImage = inputImage.getScaledInstance( width, height, java.awt.Image.SCALE_DEFAULT); checkImage( outputImage ); encode(new FileOutputStream(resizedFile), outputImage, format); } /** Checks the given image for valid width and height. */ private static void checkImage( Image image ) { waitForImage( image ); int imageWidth = image.getWidth( null ); if ( imageWidth < 1 ) throw new IllegalArgumentException( "image width " + imageWidth + " is out of range" ); int imageHeight = image.getHeight( null ); if ( imageHeight < 1 ) throw new IllegalArgumentException( "image height " + imageHeight + " is out of range" ); } /** Waits for given image to load. Use before querying image height/width/colors. */ private static void waitForImage( Image image ) { try { tracker.addImage( image, 0 ); tracker.waitForID( 0 ); tracker.removeImage(image, 0); } catch( InterruptedException e ) { e.printStackTrace(); } } /** Encodes the given image at the given quality to the output stream. */ private static void encode( OutputStream outputStream, Image outputImage, String format ) throws java.io.IOException { int outputWidth = outputImage.getWidth( null ); if ( outputWidth < 1 ) throw new IllegalArgumentException( "output image width " + outputWidth + " is out of range" ); int outputHeight = outputImage.getHeight( null ); if ( outputHeight < 1 ) throw new IllegalArgumentException( "output image height " + outputHeight + " is out of range" ); // Get a buffered image from the image. BufferedImage bi = new BufferedImage( outputWidth, outputHeight, BufferedImage.TYPE_INT_RGB ); Graphics2D biContext = bi.createGraphics(); biContext.drawImage( outputImage, 0, 0, null ); ImageIO.write(bi, format, outputStream); outputStream.flush(); } /** * 缩放gif图片 * @param originalFile 原图片 * @param resizedFile 缩放后的图片 * @param newWidth 宽度 * @param quality 缩放比例 (等比例) * @throws IOException */ private static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException { if (quality < 0 || quality > 1) { throw new IllegalArgumentException("Quality has to be between 0 and 1"); } ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath()); Image i = ii.getImage(); Image resizedImage = null; int iWidth = i.getWidth(null); int iHeight = i.getHeight(null); if (iWidth > iHeight) { resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH); } else { resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH); } // This code ensures that all the pixels in the image are loaded. Image temp = new ImageIcon(resizedImage).getImage(); // Create the buffered image. BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB); // Copy image to buffered image. Graphics g = bufferedImage.createGraphics(); // Clear background and paint the image. g.setColor(Color.white); g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null)); g.drawImage(temp, 0, 0, null); g.dispose(); // Soften. float softenFactor = 0.05f; float[] softenArray = {0, softenFactor, 0, softenFactor, 1-(softenFactor*4), softenFactor, 0, softenFactor, 0}; Kernel kernel = new Kernel(3, 3, softenArray); ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); bufferedImage = cOp.filter(bufferedImage, null); // Write the jpeg to a file. FileOutputStream out = new FileOutputStream(resizedFile); // Encodes image as a JPEG data stream JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage); param.setQuality(quality, true); encoder.setJPEGEncodeParam(param); encoder.encode(bufferedImage); } }
允许用户上传文件,那么我们一定要注意安全,如果用户上传了一个jsp文件,而这个文件的上传路径恰好能被用户访问到,那么用户可能会在这个jsp文件里面做一个对网站其他文件的文件操作,可以将文件保存到web-inf下面,如果用户需要下载,我们就写一个servlet读取这个文件,以流的方式返回给用户。
相关文章推荐
- java下的大型网站对图片的下载,存放,及压缩管理
- 巴巴运动网商品交易系统对图片的存放、下载以及压缩管理
- 如果让你来制作一个访问量很高的大型网站,你会如何来管理所有CSS文件、JS与图片?
- 大型web网站前端css\js\图片版本管理解决方案
- 安卓中关于图片从网络获取,压缩,上传,下载,缩略图,缓存的一些处理总结(三)
- 大型网站图片服务器架构的演进
- Java 小程序 实现网络下载图片到制定文件夹,压缩和删除文件
- FMX手机app,如何下载网站图片而不卡界面
- 分享25个网站常用基于jquery焦点图图片展示代码下载
- 从XKCD网站下载自动所有漫画图片---python实现
- 网站访问速度优化之一 - 图片压缩
- java压缩下载(带excel和图片)
- 大型网站图片服务器架构的演进
- 位图管理、图片下载缓存、管理图片内存 (五) 管理位图内存
- 大型网站图片服务器架构的演进
- 用python下载xxxx网站封面作品的所有图片
- 将图片从网站上下载到本地来
- 网站发布后存放图片修改的文件夹访问被拒
- 大型网站图片服务器架构的演进
- 通过URL下载图片并压缩