永发信息网

java 怎么不重复上传文件到ftp

答案:2  悬赏:30  手机版
解决时间 2021-03-21 12:11
  • 提问者网友:暮烟疏雨之际
  • 2021-03-21 07:24
java 怎么不重复上传文件到ftp
最佳答案
  • 五星知识达人网友:妄饮晩冬酒
  • 2021-03-21 07:43
上传之前,判断一下服务器端是否已经存在 。。。。。。。。。
全部回答
  • 1楼网友:酒安江南
  • 2021-03-21 09:19
上传下载的代码 import java.io.bufferedreader; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstreamreader; import sun.net.telnetoutputstream; import sun.net.telnetinputstream; import sun.net.ftp.ftpclient; public class download { string localfilename; string remotefilename; ftpclient ftpclient; // server:服务器名字 // user:用户名 // password:密码 // path:服务器上的路径 public void connectserver(string ip, int port,string user , string password,string path) { try { ftpclient = new ftpclient(); ftpclient.openserver(ip,port); ftpclient.login(user, password); system.out.println("login success!"); if (path.length() != 0) ftpclient.cd(path); ftpclient.binary(); } catch (ioexception ex) { system.out.println("not login"); system.out.println(ex); } } public void closeconnect() { try { ftpclient.closeserver(); system.out.println("disconnect success"); } catch (ioexception ex) { system.out.println("not disconnect"); system.out.println(ex); } } public void upload() { this.localfilename = "d://test2//test.txt"; this.remotefilename = "test.txt"; try { telnetoutputstream os = ftpclient.put(this.remotefilename); java.io.file file_in = new java.io.file(this.localfilename); fileinputstream is = new fileinputstream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { os.write(bytes, 0, c); } system.out.println("upload success"); is.close(); os.close(); } catch (ioexception ex) { system.out.println("not upload"); system.out.println(ex); } } public void download() { try { telnetinputstream is = ftpclient.get(this.remotefilename); java.io.file file_in = new java.io.file(this.localfilename); fileoutputstream os = new fileoutputstream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { // system.out.println((char)is.read()); // system.out.println(file_in); os.write(bytes, 0, c); } system.out.println("download success"); os.close(); is.close(); } catch (ioexception ex) { system.out.println("not download"); system.out.println(ex); } } public void download(string remotepath,string remotefile,string localfile) { try { if (remotepath.length() != 0) ftpclient.cd(remotepath); telnetinputstream is = ftpclient.get(remotefile); java.io.file file_in = new java.io.file(localfile); fileoutputstream os = new fileoutputstream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { // system.out.println((char)is.read()); // system.out.println(file_in); os.write(bytes, 0, c); } system.out.println("download success"); os.close(); is.close(); } catch (ioexception ex) { system.out.println("not download"); system.out.println(ex); } } public void download(string remotefile,string localfile) { try { telnetinputstream is = ftpclient.get(remotefile); java.io.file file_in = new java.io.file(localfile); fileoutputstream os = new fileoutputstream(file_in); byte[] bytes = new byte[1024]; int c; while ((c = is.read(bytes)) != -1) { // system.out.println((char)is.read()); // system.out.println(file_in); os.write(bytes, 0, c); } system.out.println("download success"); os.close(); is.close(); } catch (ioexception ex) { system.out.println("not download"); system.out.println(ex); } } public static void main(string agrs[]) { string filepath[] = { "/callcenter/index.jsp", "/callcenter/ip.txt", "/callcenter/mainframe/image/processing_bar_2.gif", "/callcenter/mainframe/image/logo_01.jpg" }; string localfilepath[] = { "c:\\ftp_test\\index.jsp", "c:\\ftp_test\\ip.txt", "c:\\ftp_test\\processing_bar_2.gif", "c:\\ftp_test\\logo_01.jpg" }; download fu = new download(); fu.connectserver("172.16.1.66",22, "web_test", "123456","/callcenter"); for(int i=0;i
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯