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

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

目前有两种解决方案
第一种方案,通过web服务器的jsp去连接ftp客户端,读取到远程FTP服务器上的图片,生成缩略图,返回到页面,然后用户选择想下载的图片,再一次通过jsp连接ftp下载到客户端。
示例代码如下:
<%--  
    Document   : img
    Created on : 
2007 - 10 - 30 0 : 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,两个服务器共享文件目录,把生成缩略图的工作放在远程,而传回来的只是缩略图,所以速度会快不少。
目前打算采用第二种方案 : )
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值