AD

breakpoint upload java ftp function in the realization of

Mainly used in the net apache package to achieve. Website **commons.apache**/net/. Specific packages to download and API documentation, please Tell me what network.

Breakpoint upload upload process is set transmission begins. And set the binary transfer.

Java code
  1. package apache**.test;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.io.PrintWriter;
  9. import org.apache**mons**.PrintCommandListener;
  10. import org.apache**mons**.ftp.FTP;
  11. import org.apache**mons**.ftp.FTPClient;
  12. import org.apache**mons**.ftp.FTPFile;
  13. import org.apache**mons**.ftp.FTPReply;
  14. public class ContinueFTP (
  15. private FTPClient ftpClient = new FTPClient ();
  16. public ContinueFTP () (
  17. / / Set the process will be used to the command output to the console
  18. this. ftpClient.addProtocolCommandListener (new PrintCommandListener (new PrintWriter (System.out)));
  19. )
  20. / **
  21. * Connect to FTP server
  22. * @ Param hostname hostname
  23. * @ Param port port
  24. * @ Param username user name
  25. * @ Param password password
  26. * @ Return whether a connection
  27. * @ Throws IOException
  28. * /
  29. public boolean connect (String hostname, int port, String username, String password) throws IOException (
  30. ftpClient.connect (hostname, port);
  31. if (FTPReply.isPositiveCompletion (ftpClient.getReplyCode ())){
  32. if (ftpClient.login (username, password)) (
  33. return true;
  34. )
  35. )
  36. disconnect ();
  37. return false;
  38. )
  39. / **
  40. * Download files from FTP server
  41. * @ Param remote the remote file path
  42. * @ Param local the local file path
  43. * @ Return success
  44. * @ Throws IOException
  45. * /
  46. public boolean download (String remote, String local) throws IOException (
  47. ftpClient.enterLocalPassiveMode ();
  48. ftpClient.setFileType (FTP.BINARY_FILE_TYPE);
  49. boolean result;
  50. File f = new File (local);
  51. FTPFile [] files = ftpClient.listFiles (remote);
  52. if (files.length! = 1) (
  53. System.out.println ("Remote file is not the only");
  54. return false;
  55. )
  56. long lRemoteSize = files [0]. getSize ();
  57. if (f.exists ()) (
  58. OutputStream out = new FileOutputStream (f, true);
  59. System.out.println ("local file size:" + f.length ());
  60. if (f.length ()> = lRemoteSize) (
  61. System.out.println ("local file size is greater than remote file size, download stop");
  62. return false;
  63. )
  64. ftpClient.setRestartOffset (f.length ());
  65. result = ftpClient.retrieveFile (remote, out);
  66. out.close ();
  67. ) Else (
  68. OutputStream out = new FileOutputStream (f);
  69. result = ftpClient.retrieveFile (remote, out);
  70. out.close ();
  71. )
  72. return result;
  73. )
  74. / **
  75. * Upload files to the FTP server to support HTTP
  76. * @ Param local the local file name, absolute path
  77. * @ Param remote the remote file path, use / home/directory1/subdirectory/file.ext Linux on the path in accordance with the prescribed manner, to support multi-level directory nesting, there is no support for recursive directory structure created
  78. * @ Return From the results
  79. * @ Throws IOException
  80. * /
  81. public UploadStatus upload (String local, String remote) throwsIOException (
  82. / / Set PassiveMode transmission
  83. ftpClient.enterLocalPassiveMode ();
  84. / / Set to binary stream transmitted
  85. ftpClient.setFileType (FTP.BINARY_FILE_TYPE);
  86. UploadStatus result;
  87. / / Address of the remote directory
  88. String remoteFileName = remote;
  89. if (remote.contains ("/")){
  90. remoteFileName = remote.substring (remote.lastIndexOf ("/")+ 1);
  91. String directory = remote.substring (0, remote.lastIndexOf ("/")+ 1);
  92. if (! directory.equalsIgnoreCase ("/")&&! ftpClient.changeWorkingDirectory (directory)) (
  93. / / If the remote directory does not exist, create a remote server directory recursive
  94. int start = 0;
  95. int end = 0;
  96. if (directory.startsWith ("/")){
  97. start = 1;
  98. ) Else (
  99. start = 0;
  100. )
  101. end = directory.indexOf ("/", start);
  102. while (true) (
  103. String subDirectory = remote.substring (start, end);
  104. if (! ftpClient.changeWorkingDirectory (subDirectory)) (
  105. if (ftpClient.makeDirectory (subDirectory)) (
  106. ftpClient.changeWorkingDirectory (subDirectory);
  107. ) Else (
  108. System.out.println ("create directory failed");
  109. return UploadStatus.Create_Directory_Fail;
  110. )
  111. )
  112. start = end + 1;
  113. end = directory.indexOf ("/", start);
  114. / / Check whether all the directory to create complete
  115. if (end <= start) (
  116. break;
  117. )
  118. )
  119. )
  120. )
  121. / / Check remote file exists
  122. FTPFile [] files = ftpClient.listFiles (remoteFileName);
  123. if (files.length == 1) (
  124. long remoteSize = files [0]. getSize ();
  125. File f = new File (local);
  126. long localSize = f.length ();
  127. if (remoteSize == localSize) (
  128. return UploadStatus.File_Exits;
  129. ) Else if (remoteSize> localSize) (
  130. return UploadStatus.Remote_Bigger_Local;
  131. )
  132. / / Try to move the pointer to read the paper, to achieve HTTP
  133. InputStream is = new FileInputStream (f);
  134. if (is.skip (remoteSize) == remoteSize) (
  135. ftpClient.setRestartOffset (remoteSize);
  136. if (ftpClient.storeFile (remote, is)) (
  137. return UploadStatus.Upload_From_Break_Success;
  138. )
  139. )
  140. / / If HTTP is not successful, delete the file on the server, re-upload
  141. if (! ftpClient.deleteFile (remoteFileName)) (
  142. return UploadStatus.Delete_Remote_Faild;
  143. )
  144. is = new FileInputStream (f);
  145. if (ftpClient.storeFile (remote, is)) (
  146. result = UploadStatus.Upload_New_File_Success;
  147. ) Else (
  148. result = UploadStatus.Upload_New_File_Failed;
  149. )
  150. is.close ();
  151. ) Else (
  152. InputStream is = new FileInputStream (local);
  153. if (ftpClient.storeFile (remoteFileName, is)) (
  154. result = UploadStatus.Upload_New_File_Success;
  155. ) Else (
  156. result = UploadStatus.Upload_New_File_Failed;
  157. )
  158. is.close ();
  159. )
  160. return result;
  161. )
  162. / **
  163. * Disconnect the connection with the remote server
  164. * @ Throws IOException
  165. * /
  166. public void disconnect () throws IOException (
  167. if (ftpClient.isConnected ()) (
  168. ftpClient.disconnect ();
  169. )
  170. )
  171. public static void main (String [] args) (
  172. ContinueFTP myFtp = new ContinueFTP ();
  173. try (
  174. myFtp.connect ("192.168.21.171", 21, "test", "test");
  175. System.out.println (myFtp.upload ("E: \ \ VP6.flv", "/ MIS/video/VP6.flv"));
  176. myFtp.disconnect ();
  177. ) Catch (IOException e) (
  178. System.out.println ("FTP connection error:" + e.getMessage ());
  179. )
  180. )
  181. )
标签: java code, import java, import org, ioexception, fileinputstream, inputstream, printwriter, string username, string password, return success, outputstream, breakpoint, package apache, fileoutputstream, string hostname, apache package, java ftp, upload java, binary transfer
分类: Java
时间: 2010-09-13

相关文章

  1. Java ftp login, upload and download

    Recent projects using the ftp function to upload and download from the Internet to find a lot about the conten ...
  2. With edtftpj Java FTP client tool to achieve

    http://lavasoft.blog.51cto.com/62575/101108 With edtftpj Java FTP client tool to achieve edtftpj is a java FTP ...
  3. JAVA FTP protocol implementation in the server and client \

    JAVA FTP protocol implementation in the server and client \
  4. Java Oracle function calls

    oracle stored procedures and functions different from 1, can use out parameters to return multiple values, the ...
  5. simple file upload java

    simple file upload java smartUplaodupload.rar
  6. How to implement the program in Java FTP upload and download capabilities

    List is used to display some of the files on the FTP; GetButton part of the FTP server to download from a file ...
  7. xhEditor File Upload Java implementation

    Recently when a project needs to use the HTML editor, although the FCKEditor, CKEditor and many ready-made too ...
  8. File Upload Java development process a comprehensive summary of the various ways

    So far: I met the type of the upload so few JSP + Servlet's, Struts2's, Struts's, FTP's, ExtJs's, Flex's First ...
  9. Java Excel function of the effective interest rate demand RATE

    There is a function in excel RATE is to get the real interest rate, have identified a half-line, the formula'd ...
  10. swfupload file upload java upload programs way!

    On the period of time have seen a lot of information about swfupload! Many people write so that I can not very ...
  11. Simple flex file upload - java client

    Use of third-party package: commons-fileupload-1.2.jar apache official website can be package fileupload; impo ...
  12. File Upload Java Application Development

    Today, spare nothing, did not write a blog for a long time, and suddenly remembered a lot of web development p ...
  13. Understanding of Java callback function

    The so-called callback, that is, client service program C calls a function S in A, then S has at some point tu ...
  14. python implemented automatically upload download ftp (directory-recursive)

    Because the python script can be modified directly with the text tool to open, so there is no set parameters. ...
  15. Reproduced java ping function

    A pure Java implementation of ICMP ping command import java.io. *; import java.net .*; import java.nio.channel ...
  16. Java Ftp 操作上传与下载

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOExcepti ...
  17. Farm income dishes of time reminders for free download java source code using the java hot-key function

    JIntellitype.dll put this file into C: \ WINDOWS \ system32 If you installed the java runtime environment do n ...
  18. java recursive function

    1, a recursive function definition: A: The recursive function that is called from the function, the function b ...
  19. A description of DES encryption standard encryption / decryption function of the realization of the source code

    Surface from the Wikipedia note on the DES: Data Encryption Standard (DES) is an encryption algorithm (a metho ...