当前位置: 代码迷 >> JavaScript >> jsp 往 FTP 下传文件有关问题
  详细解决方案

jsp 往 FTP 下传文件有关问题

热度:497   发布时间:2012-08-30 09:55:54.0
jsp 往 FTP 上传文件问题

?FtpUtil:

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;




import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

public class FtpUtil {
    FtpClient ftpClient ;
    /** 
        * connectServer 
        * 连接ftp服务器 
        * @throws java.io.IOException 
        * @param path 文件夹,空代表根目录 
        * @param password 密码 
        * @param user   登陆用户 
        * @param server 服务器地址 
        */ 
      public void connectServer(String server, String user, String password,  String path) 
      throws IOException  
      { 
         // server:FTP服务器的IP地址;user:登录FTP服务器的用户名 
         // password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径 
         ftpClient = new FtpClient(); 
         ftpClient.openServer(server); 
         ftpClient.login(user, password); 
         //path是ftp服务下主目录的子目录 
         if (path.length() != 0)  ftpClient.cd(path); 
         //用2进制上传、下载 
         ftpClient.binary();      
     } 
      public void connectServer() throws IOException  
      { 
         // server:FTP服务器的IP地址;user:登录FTP服务器的用户名 
         // password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径 
         ftpClient = new FtpClient(); 
         ftpClient.openServer("221.193.212.119"); 
         ftpClient.login("admin", "admin"); 
         //path是ftp服务下主目录的子目录 
        ftpClient.cd("test"); 
         
         //用2进制上传、下载 
         ftpClient.binary();      
     } 
      
     /** 
        * upload 
        * 上传文件 
        * @throws java.lang.Exception 
        * @return -1 文件不存在 
        *          -2 文件内容为空  
        *          >0 成功上传,返回文件的大小 
        * @param newname 上传后的新文件名 
        * @param filename 上传的文件 
        */ 
     public long upload(String filename,String newname) throws Exception  
     { 
         long result = 0; 
         TelnetOutputStream os = null; 
         FileInputStream is = null; 
         try {    
             connectServer();
             java.io.File file_in = new java.io.File(filename); 
             if (!file_in.exists()) return -1; 
             if (file_in.length()==0) return -2; 
             os = ftpClient.put(newname); 
             result = file_in.length(); 
             is = new FileInputStream(file_in); 
             byte[] bytes = new byte[1024]; 
             int c; 
             while ((c = is.read(bytes)) != -1) { 
                  os.write(bytes, 0, c); 
             } 
         }finally { 
             if (is != null) { 
                 is.close(); 
             } 
             if (os != null) { 
                os.close(); 
             } 
             closeServer();
         } 
        return result; 
     } 
     /** 
        * upload 
        * @throws java.lang.Exception 
        * @return  
        * @param filename 
        */ 
     public long upload(String filename) 
     throws Exception  
     { 
        String str = ""; 
        System.out.println("11111");
        if (filename.indexOf("/")>-1) 
        { 
            str = filename.substring(filename.lastIndexOf("/")+1); 
        }else 
        { 
            str = filename; 
        } 
        System.out.println("22222");
        String suffix=filename.substring(filename.lastIndexOf("."),filename.length());
        
        String md5=MD5Digest.Md5(str);
        java.text.SimpleDateFormat simpledateformat;
        simpledateformat = new java.text.SimpleDateFormat ("yyyyMMddhhmmssss");
        Date dt=new Date();
        String strDate=simpledateformat.format(dt);
           String newname=strDate+md5+suffix; 
           
           
           System.out.println("filename:"+filename);

        System.out.println("newname:"+newname);
        
        
      
       return upload(filename,newname);
       
     } 
      
     /** 
        *  download 
        *  从ftp下载文件到本地 
        * @throws java.lang.Exception 
        * @return  
        * @param newfilename 本地生成的文件名 
        * @param filename 服务器上的文件名 
        */ 
     public long download(String filename,String newfilename)  
     throws Exception 
     {   
        long result = 0; 
        TelnetInputStream is = null; 
        FileOutputStream os = null; 
        try  
        { 
           is = ftpClient.get(filename);        
           java.io.File outfile = new java.io.File(newfilename); 
           os = new FileOutputStream(outfile); 
           byte[] bytes = new byte[1024]; 
           int c; 
           while ((c = is.read(bytes)) != -1) { 
               os.write(bytes, 0, c); 
               result = result + c; 
           } 
        } catch (IOException e)  
        { 
           e.printStackTrace(); 
        } 
        finally { 
             if (is != null) { 
                 is.close(); 
             } 
             if (os != null) { 
                os.close(); 
             } 
         } 
         return result; 
     } 
     /** 
      * 取得某个目录下的所有文件列表 
      * 
      */ 
     @SuppressWarnings({ "unchecked", "deprecation" })
    public List getFileList(String path) 
     { 
        List list = new ArrayList(); 
        try  
        { 
           DataInputStream dis = new  DataInputStream(ftpClient.nameList(path)); 
           String filename = ""; 
           while((filename=dis.readLine())!=null)   
           {   
             list.add(filename);         
           }   
        
        } catch (Exception e)  
        { 
           e.printStackTrace(); 
        } 
        return list; 
     } 
      
     /** 
        * closeServer 
        * 断开与ftp服务器的链接 
        * @throws java.io.IOException 
        */ 
     public void closeServer() 
     throws IOException  
     {    
       try  
       { 
          if (ftpClient != null)  
          { 
            ftpClient.closeServer();      
          } 
       } catch (IOException e) { 
          e.printStackTrace(); 
       } 
     } 
        
}





?

Upload_append.jsp :

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.slwlsh.common.FtpUtil"%>
<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf8");
response.setCharacterEncoding("UTF-8");
String filename=request.getParameter("filename");
FtpUtil ftp=new FtpUtil();
Long result=ftp.upload(filename);
if(result>0){
out.print("OK");
}else{
out.print("shibai ");
}
%>

?

?

upload.jsp form

<form method="post"  action="Upload_append.jsp" >
<input type="file" name="filename"><BR />
<input type="submit" name="submit" value="上传">
</form>

?

http://topic.csdn.net/u/20100227/15/abacab97-d49b-46b8-9689-3b2227ba52d3.html

  相关解决方案