首页 > 编程知识 正文

如何往ftp上传文件(ftp连接工具)

时间:2023-05-05 13:28:33 阅读:71893 作者:3370

当然,代码1 .中使用的软件包importorg.Apache.com mons.net.FTP.FTP; importorg.Apache.com mons.net.FTP.FTP客户端; importorg.Apache.com mons.net.FTP.FTP file; importorg.Apache.com mons.net.FTP.FTP reply; importorg.spring framework.beans.factory.annotation.value; importorg.spring framework.stereotype.com ponent; import java.io.*; 2 .参数配置privatestaticfinalstringftp _ IP=' XXX.XXX.XXX.XXX '; //FTP连接地址privatestaticfinalstringftp _ basepath='/public '; //请求路径privatestaticfinalstringimage _ base _ URL=' http://XXX.XXX.XXX :999/public/'; //下载路径私有身份证明inalintftp _ port=21; //FTP连接端口privatestaticfinalstringftp _ username=' rain '; //FTP用户名privatestaticfinalstringftp _ password=' rain '; //FT平面密码privatestaticfinalintftp _ connection _ time out=60 * 1000; //FTP连接超时时间privatestaticstringseparator=file.separator; //系统分隔符3 .文件上传/* * *将文件上传到说明: FTP服务器* * @ param文件路径FTP服务器文件存储路径。 例如,按日期保管:/2015/01/01。 的路径是basepath file path * @ param filename上传到FTP服务器的文件名* @param input输入流* @return上传的路径*/public string upput try { int reply; FTP.connect(FTPIP,ftpPort ); 如果要连接到//FTP服务器//使用默认端口,可以通过FTP.connect(host )直接连接到FTP服务器FTP.login ) FTPusername,ftpPassword。 登录FTP.enterlocalpassivemode (//设置被动模式reply=ftp.getReplyCode (; if (! FTP reply.ispositivecompletion (reply ) ) { ftp.disconnect; 返回结果; //切换到上传目录if (! FTP.changeworkingdirectory (ftpbasepathfilepath ) /如果目录不存在则string[]dirs=filepath.split) ); String tempPath=ftpBasepath; for (字符串dir : dirs ) if ) null==dir||'.equals(dir ) ) continue; tempPath ='/' dir; if (! FTP.changeworkingdirectory (temppath ) ) if (! FTP.makedirectory(temppath ) { return result; } else { FTP.changeworkingdirectory (temppath ); }//将上载文件的类型设置为二进制类型FTP.set filetype (FTP.binary _ file _ type ); //上传文件if (! FTP.storefile(filename,input ) { return result; } input.close (; //流ftp.logout (; //取消登录result=imagebaseurlfilepath '/' filename; }catch(ioexceptione ) { e.printStackTrace ); (finally ) if(FTP.isconnected ) ) try (FTP.disconnect ); }catch(ioexceptionIOE ) { } }返回结果; } 4.说明`我使用FTP作为上传工具时,在Nginx上设置文件代理服务器作为下载使用。 上传文件的回复字符串是我连接的下载地址,可以在浏览器直接下载和播放视频`

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。