基于applet的FTP断点上传组件(二)

本文详细介绍了基于Applet的FTP断点上传组件中的核心类FTPControlSocket,包括其构造方法、初始化流、发送命令、读取回复等关键功能。此外,还涉及了FTPReply类、FTPDataSocket类和FTPFileType类的相关操作,如文件类型的匹配检查。
摘要由CSDN通过智能技术生成

FTP客户端相关的附加类

FTPControlSocket.class

  1. package ftpupload;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStreamWriter;
  7. import java.io.Writer;
  8. import java.net.InetAddress;
  9. import java.net.Socket;
  10. import java.net.SocketException;
  11. import java.util.Vector;
  12. public class FTPControlSocket {
  13.     private InetAddress remoteAddr;
  14.     private Socket controlSocket;
  15.     private String encoding;
  16.     protected BufferedReader br;
  17.     protected Writer writer;
  18.     protected FTPControlSocket(InetAddress remoteAddr, int controlPort,
  19.             int timeout, String encoding) throws IOException {
  20.         this(remoteAddr, new Socket(remoteAddr, controlPort), timeout, encoding);
  21.     }
  22.     protected FTPControlSocket(InetAddress remoteAddr, Socket controlSock,
  23.             int timeout, String encoding) throws IOException {
  24.         this.controlSocket = controlSock;
  25.         this.remoteAddr = remoteAddr;
  26.         this.encoding = encoding;
  27.         initStream();
  28.         // setSoTimeOut(timeout);
  29.         validateConnection();
  30.     }
  31.     
  32.     
  33.     private void validateConnection() throws IOException {
  34.         FTPReply reply = readReply();
  35.         String rightCode[] = { "220""230" };
  36.         validateReply(reply, rightCode);
  37.     }
  38.     private void initStream() {
  39.         try {
  40.             InputStream is = controlSocket.getInputStream();
  41.             br = new BufferedReader(new InputStreamReader(is, encoding));
  42.             writer = new OutputStreamWriter(controlSocket.getOutputStream(),
  43.                     encoding);
  44.         } catch (IOException e) {
  45.             // TODO Auto-generated catch block
  46.             e.printStackTrace();
  47.         }
  48.     }
  49.     public void setSoTimeOut(int millis) throws SocketException {
  50.         controlSocket.setSoTimeout(millis);
  51.     }
  52.     public void close() throws IOException {
  53.         controlSocket.close();
  54.     }
  55.     public void logout() {
  56.         try {
  57.             writer.close();
  58.             br.close();
  59.             controlSocket.close();
  60.         } catch (IOException e) {
  61.             // TODO Auto-generated catch block
  62.             e.printStackTrace();
  63.         }
  64.     }
  65.     public FTPReply sendCommand(String command) throws IOException {
  66.         writeCommand(command);
  67.         return readReply();
  68.     }
  69.     void writeCommand(String command) throws IOException {
  70.         writer.write(command + "/r/n");
  71.         writer.flush();
  72.     }
  73.     public FTPReply readReply() throws IOException {
  74.         String line;
  75.         for (line = br.readLine(); line != null && line.length() == 0; line = br
  76.                 .readLine())
  77.             ;
  78.         if (line == null)
  79.             throw new IOException("Unexpected null reply received");
  80.         if (line.length() < 3)
  81.             throw new IOException("Short reply received");
  82.         String replyCode = line.substring(03);
  83.         StringBuffer reply = new StringBuffer("");
  84.         if (line.length() > 3)
  85.             reply.append(line.substring(4));
  86.         Vector dataLines = null;
  87.         if (line.charAt(3) == '-') {
  88.             dataLines = new Vector();
  89.             for (boolean complete = false; !complete;) {
  90.                 line = br.readLine();
  91.                 if (line == null)
  92.                     throw new IOException("Unexpected null reply received");
  93.                 if (line.length() != 0) {
  94.                     if (line.length() > 3
  95.                             && line.substring(03).equals(replyCode)
  96.                             && line.charAt(3) == ' ') {
  97.                         reply.append(line.substring(3));
  98.                         complete = true;
  99.                     } else {
  100.                         reply.append(" ").append(line);
  101.                         dataLines.addElement(line);
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.         if (dataLines != null) {
  107.             String data[] = new String[dataLines.size()];
  108.             dataLines.copyInto(data);
  109.             return new FTPReply(replyCode, reply.toString(), data);
  110.         } else {
  111.             return new FTPReply(replyCode, reply.toString());
  112.         }
  113.     }
  114.     public FTPReply validateReply(FTPReply reply, String[] rightCode) {
  115.         String replyCode = reply.getReplyCode();
  116.         for (int i = 0; i < rightCode.length; i++) {
  117.             if (replyCode.equals(rightCode[i]))
  118.                 return reply;
  119.         }
  120.         return null;
  121.     }
  122.     protected FTPDataSocket createDataSocketPASV() throws Exception {
  123.         FTPReply reply = sendCommand("PASV");
  124.         String[] rightcode = { "227" };
  125.         validateReply(reply, rightcode);
  126.         String replyText = reply.getReplyText();
  127.         int startIP = replyText.indexOf('(');
  128.         int endIP = replyText.indexOf(')');
  129.         if (startIP < 0) {
  130.             for (startIP = 0; startIP < replyText.length()
  131.                     && !Character.isDigit(replyText.charAt(startIP)); startIP++)
  132.                 ;
  133.             startIP--;
  134.             // System.out.println("startIP:"+startIP);
  135.         }
  136.         if (endIP < 0) {
  137.             for (endIP = replyText.length() - 1; endIP > 0
  138.                     && !Character.isDigit(replyText.charAt(endIP)); endIP--)
  139.                 ;
  140.             if (++endIP >= replyText.length())
  141.                 replyText = replyText + ")";
  142.             // System.out.println("endIP:"+endIP);
  143.         }
  144.         String ipData = replyText.substring(startIP + 1, endIP).trim();
  145.         // System.out.println(ipData);
  146.         int parts[] = new int[6];
  147.         int len = ipData.length();
  148.         int partCount = 0;
  149.         StringBuffer buf = new StringBuffer();
  150.         for (int i = 0; i < len && partCount <= 6; i++) {
  151.             char ch = ipData.charAt(i);
  152.             if (Character.isDigit(ch))
  153.                 buf.append(ch);
  154.             else if (ch != ',' && ch != ' ')
  155.                 throw new Exception("Malformed PASV reply: " + replyText);
  156.             if (ch == ',' || i + 1 == len)
  157.                 try {
  158.                     parts[partCount++] = Integer.parseInt(buf.toString());
  159.                     buf.setLength(0);
  160.                 } catch (NumberFormatException _ex) {
  161.                     throw new Exception("Malformed PASV reply: " + replyText);
  162.                 }
  163.         }
  164.         String ipAddress = parts[0] + "." + parts[1] + "." + parts[2] + "."
  165.                 + parts[3];
  166.         int tport = (parts[4] << 8) + parts[5];
  167.         String hostIP = ipAddress;
  168.         return new FTPDataSocket(new Socket(hostIP, tport));
  169.     }
  170.     public void stop() throws IOException {
  171.         this.controlSocket.close();
  172.     }
  173. }

FTPDataSocket.class

  1. package ftpupload;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.Socket;
  6. import java.net.SocketException;
  7. public class FTPDataSocket {
  8.     
  9. protected Socket socket; 
  10.     
  11.     protected FTPDataSocket(Socket socket){
  12.         
  13.         this.socket=socket;
  14.     }
  15.     
  16.     public int getLocalPort(){
  17.         return socket.getLocalPort();
  18.     }
  19.     
  20.     public void setSoTimeOut(int timeout) throws SocketException{
  21.         socket.setSoTimeout(timeout);
  22.     }
  23.     public OutputStream getOutputStream() throws IOException{
  24.         
  25.         return socket.getOutputStream();
  26.     }
  27.     
  28.     public InputStream getInputStream() throws IOException {
  29.         return socket.getInputStream();
  30.     } 
  31.     
  32.     public void close() throws IOException{
  33.         
  34.         socket.close();
  35.     }
  36. }

FTPException.class

  1. package ftpupload;
  2. public class FTPException extends Exception {
  3.     private int replyCode;
  4.     public FTPException(String msg) {
  5.         super(msg);
  6.         replyCode = -1;
  7.     }
  8.     public FTPException(String msg, String replyCode) {
  9.         super(msg);
  10.         this.replyCode = -1;
  11.         try {
  12.             this.replyCode = Integer.parseInt(replyCode);
  13.         } catch (NumberFormatException _ex) {
  14.             this.replyCode = -1;
  15.         }
  16.     }
  17.     public FTPException(FTPReply reply) {
  18.         super(reply.getReplyText());
  19.         replyCode = -1;
  20.         try {
  21.             replyCode = Integer.parseInt(reply.getReplyCode());
  22.         } catch (NumberFormatException _ex) {
  23.             replyCode = -1;
  24.         }
  25.     }
  26.     public int getReplyCode() {
  27.         return replyCode;
  28.     }
  29. }

 

FTPFileType.class

  1. package ftpupload;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class FTPFileType {
  5.     
  6.     public static Map<String, String> ASCII;    
  7.     static{
  8.         
  9.         ASCII=new HashMap<String, String>();
  10.         ASCII.put("ASC","ASC");
  11.         ASCII.put("C","C");
  12.         ASCII.put("CPP","CPP");
  13.         ASCII.put("CS","CS");
  14.         ASCII.put("CSV","CSV");
  15.         ASCII.put("H","H");
  16.         ASCII.put("HTM","HTM");
  17.         ASCII.put("HTML","HTML");
  18.         ASCII.put("INF","INF");
  19.         ASCII.put("INI","INI");
  20.         ASCII.put("JAVA","JAVA");
  21.         ASCII.put("KSH","KSH");
  22.         ASCII.put("LOG","LOG");
  23.         ASCII.put("PS","PS");
  24.         ASCII.put("SH","SH");
  25.         ASCII.put("SHTML","SHTML");
  26.         ASCII.put("TXT","TXT");
  27.         ASCII.put("UU","UU");
  28.         ASCII.put("UUE","UUE");
  29.         ASCII.put("XML","XML");
  30.         ASCII.put("XSL","XSL");
  31.         ASCII.put("MF""MF");
  32.     }
  33.     
  34.     public static boolean matchASCII(String filename){
  35.         
  36.         int pos=filename.lastIndexOf(".");
  37.         String ext=filename.substring(pos+1).toUpperCase();
  38.         if(ASCII.get(ext)!=null)
  39.             return true;
  40.         return false;
  41.     }
  42. }

FTPProcessListener.class

  1. package ftpupload;
  2. public interface FTPProcessListener {
  3.     
  4.     void getUploadSize(long size);
  5. }

FTPProcessMointor.class

  1. package ftpupload;
  2. public class FTPProcessMointor implements FTPProcessListener{
  3.     
  4.     volatile long uploadSize;
  5.     public void getUploadSize(long size) {
  6.         // TODO Auto-generated method stub
  7.         this.uploadSize=size;
  8.     }
  9. }

FTPReply.class

 

  1. package ftpupload;
  2. public class FTPReply {
  3.     private String replyCode;
  4.     private String replyText;
  5.     private String data[];
  6.     FTPReply(String replyCode, String replyText) {
  7.         this.replyCode = replyCode;
  8.         this.replyText = replyText;
  9.     }
  10.     FTPReply(String replyCode, String replyText, String data[]) {
  11.         this.replyCode = replyCode;
  12.         this.replyText = replyText;
  13.         this.data = data;
  14.     }
  15.     FTPReply(String rawReply) {
  16.         rawReply = rawReply.trim();
  17.         replyCode = rawReply.substring(03);
  18.         if (rawReply.length() > 3)
  19.             replyText = rawReply.substring(4);
  20.         else
  21.             replyText = "";
  22.     }
  23.     public String getReplyCode() {
  24.         return replyCode;
  25.     }
  26.     public String getReplyText() {
  27.         return replyText;
  28.     }
  29.     public String[] getReplyData() {
  30.         return data;
  31.     }
  32. }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值