dataconnection.java
来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 767 行 · 第 1/2 页
JAVA
767 行
{ // no resuming while(true) { //System.out.println("."); int read = -2; try { read = in.read(buf); } catch(IOException es) { Log.out("got a IOException"); ok = false; bOut.close(); finished = true; con.fireProgressUpdate(file, FAILED, -1); Log.out("last read: " + read + ", len: " + (len + read)); es.printStackTrace(); return; } len += read; if(read == -1) { break; } if(newLine != null) { byte[] buf2 = modifyGet(buf, read); bOut.write(buf2, 0, buf2.length); } else { bOut.write(buf, 0, read); } con.fireProgressUpdate(file, type, len); if(time()) { //Log.debugSize(len, true, false, file); } if(read == StreamTokenizer.TT_EOF) { break; } } bOut.flush(); //Log.debugSize(len, true, true, file); } } catch(IOException ex) { ok = false; debug("Old connection removed"); con.fireProgressUpdate(file, FAILED, -1); //debug(ex + ": " + ex.getMessage()); ex.printStackTrace(); } } } } //---------------upload---------------------- if(type.equals(PUT) || type.equals(PUTDIR)) { if(in == null) { try { fIn = new RandomAccessFile(file, "r"); if(resume) { fIn.skipBytes(skiplen); } //fIn = new BufferedInputStream(new FileInputStream(file)); } catch(Exception ex) { debug("Can't open inputfile: " + " (" + ex + ")"); ok = false; } } if(ok) { try { out = new BufferedOutputStream(sock.getOutputStream()); } catch(Exception ex) { ok = false; debug("Can't get OutputStream"); } if(ok) { try { int len = skiplen; char b; while(true) { int read; if(in != null) { read = in.read(buf); } else { read = fIn.read(buf); } len += read; //System.out.println(file + " " + type+ " " + len + " " + read); if(read == -1) { break; } if(newLine != null) { byte[] buf2 = modifyPut(buf, read); out.write(buf2, 0, buf2.length); } else { out.write(buf, 0, read); } con.fireProgressUpdate(file, type, len); if(time()) { // Log.debugSize(len, false, false, file); } if(read == StreamTokenizer.TT_EOF) { break; } } out.flush(); //Log.debugSize(len, false, true, file); } catch(IOException ex) { ok = false; debug("Error: Data connection closed."); con.fireProgressUpdate(file, FAILED, -1); ex.printStackTrace(); } } } } } } catch(IOException ex) { Log.debug("Can't connect socket to ServerSocket"); ex.printStackTrace(); } finally { try { if(out != null) { out.flush(); out.close(); } } catch(Exception ex) { ex.printStackTrace(); } try { if(bOut != null) { bOut.flush(); bOut.close(); } } catch(Exception ex) { ex.printStackTrace(); } try { if(fOut != null) { fOut.close(); } } catch(Exception ex) { ex.printStackTrace(); } try { if(in != null && !justStream) { in.close(); } if(fIn != null) { fIn.close(); } } catch(Exception ex) { ex.printStackTrace(); } } try { sock.close(); } catch(Exception ex) { debug(ex.toString()); } if(!Settings.getFtpPasvMode()) { try { ssock.close(); } catch(Exception ex) { debug(ex.toString()); } } finished = true; if(ok) { con.fireProgressUpdate(file, FINISHED, -1); } else { con.fireProgressUpdate(file, FAILED, -1); } } public InputStream getInputStream() { return in; } public FtpConnection getCon() { return con; } private void debug(String msg) { Log.debug(msg); } public void reset() { reciever.destroy(); reciever = new Thread(this); reciever.start(); } private void pause(int time) { try { reciever.sleep(time); } catch(Exception ex) { System.out.println(ex); } } private boolean time() { long now = System.currentTimeMillis(); long offset = now - start; if(offset > Settings.statusMessageAfterMillis) { start = now; return true; } return false; } public boolean isThere() { if(finished) { return true; } return isThere; } public void setType(String tmp) { type = tmp; } public boolean isOK() { return ok; } public void interrupt() { if(Settings.getFtpPasvMode() && (type.equals(GET) || type.equals(GETDIR))) { try { reciever.join(); } catch(InterruptedException ex) { ex.printStackTrace(); } } } private byte[] modifyPut(byte[] buf, int len) { //Log.debug("\n\n\n\nNewline: "+newLine); if(newLine == null) return buf; String s = (new String(buf)).substring(0,len); s = s.replaceAll(LINEEND, newLine); //Log.debug("Newline_own: "+LINEEND+", s:"+s); return s.getBytes(); } private byte[] modifyGet(byte[] buf, int len) { //Log.debug("\n\n\n\nNewline: "+newLine); if(newLine == null) return buf; String s = (new String(buf)).substring(0,len); s = s.replaceAll(newLine, LINEEND); //Log.debug("Newline_own: "+LINEEND+", s:"+s); return s.getBytes(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?