1 package net.sf.jftp.net; 2 3 import java.io.BufferedInputStream ; 4 import java.io.BufferedOutputStream ; 5 import java.io.File ; 6 import java.io.FileOutputStream ; 7 import java.net.URL ; 8 import java.util.Vector ; 9 10 import net.sf.jftp.system.StringUtils; 11 import net.sf.jftp.system.logging.Log; 12 13 14 public class HttpTransfer extends Transfer implements Runnable 15 { 16 private String url; 17 private String localPath; 18 private String file; 19 public boolean work = true; 20 public boolean pause = false; 21 public Thread runner; 22 private Vector listeners; 23 private int stat = 1; 24 private ConnectionHandler handler = new ConnectionHandler(); 25 26 public HttpTransfer(String url, String localPath, Vector listeners, 27 ConnectionHandler handler) 28 { 29 this.url = url; 30 this.localPath = localPath; 31 this.listeners = listeners; 32 this.handler = handler; 33 34 file = StringUtils.getFile(url); 35 36 prepare(); 37 } 38 39 public void prepare() 40 { 41 runner = new Thread (this); 42 runner.setPriority(Thread.MIN_PRIORITY); 43 runner.start(); 44 } 45 46 public void run() 47 { 48 try 49 { 50 if(handler.getConnections().get(file) == null) 51 { 52 Log.out("download started: " + url); 53 Log.out("connection handler present: " + handler + 54 ", poll size: " + handler.getConnections().size()); 55 Log.out("local file: " + localPath + file); 56 handler.addConnection(file, this); 57 } 58 else 59 { 60 Log.debug("Transfer already in progress: " + file); 61 work = false; 62 stat = 2; 63 64 return; 65 } 66 67 URL u = new URL (url); 68 69 BufferedOutputStream f = new BufferedOutputStream (new FileOutputStream (localPath + 70 file)); 71 BufferedInputStream in = new BufferedInputStream (u.openStream()); 72 byte[] buf = new byte[4096]; 73 int len = 0; 74 75 while((stat > 0) && work) 76 { 77 stat = in.read(buf); 78 79 if(stat == -1) 80 { 81 break; 82 } 83 84 f.write(buf, 0, stat); 85 86 len += stat; 87 fireProgressUpdate(file, DataConnection.GET, len); 88 } 89 90 f.flush(); 91 f.close(); 92 in.close(); 93 94 fireProgressUpdate(file, DataConnection.FINISHED, len); 95 } 96 catch(Exception ex) 97 { 98 work = false; 99 Log.debug("Download failed: " + ex.toString()); 100 101 File f = new File (localPath + file); 102 f.delete(); 103 fireProgressUpdate(file, DataConnection.FAILED, -1); 104 105 ex.printStackTrace(); 106 107 return; 108 } 109 110 if(!work) 111 { 112 File f = new File (localPath + file); 113 f.delete(); 114 Log.out("download aborted: " + file); 115 } 116 } 117 118 public void fireProgressUpdate(String file, String type, int bytes) 119 { 120 if(listeners == null) 121 { 122 return; 123 } 124 125 for(int i = 0; i < listeners.size(); i++) 126 { 127 ((ConnectionListener) listeners.elementAt(i)).updateProgress(file, 128 type, 129 bytes); 130 } 131 } 132 133 public int getStatus() 134 { 135 return stat; 136 } 137 138 public boolean hasStarted() 139 { 140 return true; 141 } 142 143 public FtpConnection getFtpConnection() 144 { 145 return null; 146 } 147 148 public DataConnection getDataConnection() 149 { 150 return null; 151 } 152 } 153 | Popular Tags |