JSP连接远程FTP服务器,生成缩略图

需求是这样的:
图片文件放在远程FTP服务器上,图片是用作宣传的,很大。用户只能访问web服务器,用户需要在web上先预览图片的缩略图,然后点击链接下载该图片。

目前有两种解决方案
第一种方案,通过web服务器的jsp去连接ftp客户端,读取到远程FTP服务器上的图片,生成缩略图,返回到页面,然后用户选择想下载的图片,再一次通过jsp连接ftp下载到客户端。
示例代码如下:
<%-- 
    Document   : img
    Created on : 
2007-10-300:02:28
    Author     : autumn
--%>

<%@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) {
 
//异常处理
}

%>

<%-- 
    Document   : main
    Created 
on : 2007-10-300:02:11
    Author     : autumn
--
%>

<%@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,两个服务器共享文件目录,把生成缩略图的工作放在远程,而传回来的只是缩略图,所以速度会快不少。

posted on 2009-12-07 14:24 飞熊 阅读(1373) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年12月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

文章档案

收藏夹

搜索

最新评论

阅读排行榜

评论排行榜