JSP连接远程FTP服务器,生成缩略图
需求是这样的:
图片文件放在远程FTP服务器上,图片是用作宣传的,很大。用户只能访问web服务器,用户需要在web上先预览图片的缩略图,然后点击链接下载该图片。
目前有两种解决方案
第一种方案,通过web服务器的jsp去连接ftp客户端,读取到远程FTP服务器上的图片,生成缩略图,返回到页面,然后用户选择想下载的图片,再一次通过jsp连接ftp下载到客户端。
示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | <%-- Document : img Created on : 2007-10-30, 0:02:28 Author : belltoy --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ page import="java.awt.image.BufferedImage" %> <%@ page import="java.awt.*" %> <%@ page import="com.sun.image.codec.jpeg.*" %> <%@ page import="sun.net.ftp.FtpClient" %> <%@ page import="sun.net.TelnetInputStream" %> <%@ page import="java.io.ByteArrayOutputStream" %> <% String path = request.getParameter("path"); try ...{ response.flushBuffer(); out.clear(); out = pageContext.pushBody(); out.clear(); response.setContentType("image/jpg"); response.addHeader("pragma","NO-cache"); response.addHeader("Cache-Control","no-cache"); response.addDateHeader("Expries",0); /**//*连接ftp服务器*/ FtpClient fc = new FtpClient(); fc.openServer("127.0.0.1"); //连接ftp服务器,参数为ftp服务器地址 fc.login("test", "test"); //登录ftp服务器,参数为ftp用户名密码 fc.binary(); //转成二进制模式 TelnetInputStream bis = fc.get(path); //获取文件,返回输入流 BufferedImage image = javax.imageio.ImageIO.read(bis); bis.close(); //关闭输入流 fc.closeServer(); //关闭服务器连接 /**//*生成缩略图*/ float tag_w=80; float tag_h=60; int old_w=image.getWidth(null); //得到源图宽度 int old_h=image.getHeight(null); //得到源图高度 int new_w=0; //缩略图的宽度 int new_h=0; //缩略图的高度 float tempdouble; if(old_w>old_h)...{ tempdouble=old_w/tag_w; }else...{ tempdouble=old_h/tag_h; } new_w=Math.round(old_w/tempdouble); //计算缩略图高度 new_h=Math.round(old_h/tempdouble); //计算缩略图宽度 BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(image,0,0,new_w,new_h,null); //绘制缩小后的图 ServletOutputStream outStream = response.getOutputStream(); JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(outStream); encoder.encode(tag); outStream.close(); response.reset(); } catch (Exception ex) ...{ //异常处理 } %> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <%...-- Document : main Created on : 2007-10-30, 0:02:11 Author : belltoy --%> <%...@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h3>FTP img tset</h3> <img src="img.jsp?path=/2.jpg"/> </body> </html> |
上例只实现从远程获得图片生成缩略图返回,没有实现远程下载图片,不过原理一样。
这个方案有一个缺点,就是生成缩略图前,得从ftp服务器读取大图,速度比较慢。
第二种解决方案是在远程ftp服务器架设一个web server,两个服务器共享文件目录,把生成缩略图的工作放在远程,而传回来的只是缩略图,所以速度会快不少。
目前打算采用第二种方案 : -)
第二种方案,既然目的在于生成一个缩略图利于网络传输,那么何必要架设web server,直接写个java方法不就好么
May 8, 2008 11:35 AM直接写个java方法还是要到FTP端去取整张图到web server的,第二种方案传回来到web server的只是缩略图
May 8, 2008 12:24 PM或者你有什么更好的方法?能不能具体说说?
就用一个web server,显示ftp上图片的缩略图,和下载地址(绝对地址,安全上有没有关系呢),ftp上跑一个程序生成缩略图,不过也是要有个触发条件就是,比如定时检查新图片生成缩略图,想想也不算更好的方法
May 8, 2008 1:10 PM嗯,后台进程也是一个不错的办法。只是要定期生成,这样实时性就没办法保证了。
May 8, 2008 1:58 PM其实架一个web server 算是杀鸡用牛刀了。你这个方案不错,如果实时性要求不是很高的话还是采用这种方式比较好。
ftp对web 界面的用户来说是透明的,用户只发请求到 web server,由web server 去找这个资源,然后再返回给前端用户,这点没问题。