ftp上传文件第一种

编程入门 行业动态 更新时间:2024-10-28 00:14:56

ftp上传文件<a href=https://www.elefans.com/category/jswz/34/1066824.html style=第一种"/>

ftp上传文件第一种

最近在做ftp上传,使用该种方法后会报错:
Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3
所以把解决方法也列出来了,详情见ftp上传另一篇博客

package com.sinotrans.fms.nvocc.utils;import java.io.ByteArrayInputStream;
import java.io.IOException;import javafx.beans.property.Property;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;public class FtpUtils {/*** 属性集*/private static Property property = null;/*** FTP 登录用户名*/private static String userName = "root";/*** FTP 登录密码*/private static String passWord = "xxx";/*** FTP 服务器地址IP地址*/private static String hostName = "xxx";/*** FTP 端口*/private static int port = 22;/*** 文件服务器路径*/private static String path = "/root/file";/*** 文件服务器访问地址*/private static String header;/*** 配置文件的路径名*/private static String configFile = "application.properties";/*** ftp文件下载的地址,例如:模板*/private static String tmstctpFilePath;/*** 关闭连接-FTP方式** @param ftp*            FTPClient对象* @return boolean*/public static boolean closeFTP(FTPClient ftp) {if (ftp.isConnected()) {try {ftp.disconnect();System.out.println("ftp已经关闭");return true;} catch (Exception e) {e.printStackTrace();}}return false;}/*** 上传文件-FTP方式**/public static String uploadFile(String partPath, byte[] decoderBytes,String imgName) throws Exception {FTPClient ftp = new FTPClient();try {//setArg(configFile);// 连接FTP服务器ftp.connect(hostName, port);// 下面三行代码必须要,而且不能改变编码格式,否则不能正确下载中文文件ftp.setControlEncoding("GBK");FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);conf.setServerLanguageCode("zh");// 登录ftpftp.login(userName, passWord);if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {ftp.disconnect();System.out.println("连接服务器失败");}System.out.println("登陆服务器成功");String realPath = path + "/" + partPath;boolean changeWD = ftp.changeWorkingDirectory(realPath);// 转移到指定FTP服务器目录if (!changeWD) {if (!CreateDirecroty(realPath, ftp)) {throw new Exception("创建远程文件夹失败!");}}FTPFile[] fs = ftp.listFiles();// 得到目录的相应文件列表String fileName = imgName;fileName = FtpUtils.changeName(fileName, fs);fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");realPath = new String(realPath.getBytes("GBK"), "ISO-8859-1");// 转到指定上传目录ftp.changeWorkingDirectory(realPath);// 将上传文件存储到指定目录ftp.setFileType(FTP.BINARY_FILE_TYPE);// 如果缺省该句 传输txt正常 但图片和其他格式的文件传输出现乱码ftp.storeFile(fileName, new ByteArrayInputStream(decoderBytes));// 退出ftpftp.logout();System.out.println("上传成功。。。。。。");return header + realPath + fileName;} catch (Exception e) {e.printStackTrace();throw e;} finally {// 关闭ftp连接closeFTP(ftp);}}/*** 判断是否有重名文件** @param fileName* @param fs* @return*/public static boolean isFileExist(String fileName, FTPFile[] fs) {for (int i = 0; i < fs.length; i++) {FTPFile ff = fs[i];if (ff.getName().equals(fileName)) {return true; // 如果存在返回 正确信号}}return false; // 如果不存在返回错误信号}/*** 根据重名判断的结果 生成新的文件的名称** @param fileName* @param fs* @return*/public static String changeName(String fileName, FTPFile[] fs) {int n = 0;// fileName = fileName.append(fileName);while (isFileExist(fileName.toString(), fs)) {n++;String a = "[" + n + "]";int b = fileName.lastIndexOf(".");// 最后一出现小数点的位置int c = fileName.lastIndexOf("[");// 最后一次"["出现的位置if (c < 0) {c = b;}StringBuffer name = new StringBuffer(fileName.substring(0, c));// 文件的名字StringBuffer suffix = new StringBuffer(fileName.substring(b + 1));// 后缀的名称fileName = name.append(a) + "." + suffix;}return fileName.toString();}/*** 递归创建远程服务器目录** @param remote*            远程服务器文件绝对路径** @return 目录创建是否成功* @throws IOException*/public static boolean CreateDirecroty(String remote, FTPClient ftp) throws IOException {boolean success = true;String directory = remote.substring(0, remote.lastIndexOf("/") + 1);// 如果远程目录不存在,则递归创建远程服务器目录if (!directory.equalsIgnoreCase("/") && !ftp.changeWorkingDirectory(new String(directory))) {int start = 0;int end = 0;if (directory.startsWith("/")) {start = 1;} else {start = 0;}end = directory.indexOf("/", start);while (true) {String subDirectory = new String(remote.substring(start, end));if (!ftp.changeWorkingDirectory(subDirectory)) {if (ftp.makeDirectory(subDirectory)) {ftp.changeWorkingDirectory(subDirectory);} else {System.out.println("创建目录失败");success = false;return success;}}start = end + 1;end = directory.indexOf("/", start);// 检查所有目录是否创建完毕if (end <= start) {break;}}}return success;}}/*** * @param configFile*//*private static void setArg(String configFile) {property = new Property(configFile);try {userName = property.getValue("ciftpUserName");passWord = property.getValue("ciftpPassword");hostName = property.getValue("ciftpHost");path = property.getValue("ciftpPath");port = Integer.parseInt(property.getValue("ciftpPort"));header = property.getValue("ciftpHeader");tmstctpFilePath=property.getValue("tmstctpFilePath");} catch (Exception e1) {System.out.println("配置文件 【" + configFile + "】不存在!");e1.printStackTrace();}}*/

更多推荐

ftp上传文件第一种

本文发布于:2023-07-28 18:16:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1272216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一种   上传文件   ftp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!