1.首先我们需要将图片下载至本地。相关操作类如下所示:
/**
* 下载文件到本地
*
* @param urlString
* 被下载的文件地址
* @param filename
* 本地文件名
* @throws Exception
* 各种异常
*/
public static void download(String urlString, String filename) throws Exception {
// 构造URL
URL url = new URL(urlString);
// 打开连接
URLConnection con = url.openConnection();
// 输入流
InputStream is = con.getInputStream();
// 1K的数据缓冲
byte[] bs = new byte[1024];
// 读取到的数据长度
int len;
// 输出的文件流
OutputStream os = new FileOutputStream(filename);
// 开始读取
while ((len = is.read(bs)) != -1) {
os.write(bs, 0, len);
}
// 完毕,关闭所有链接
os.close();
is.close();
}
2.下载到本地以后,我们再将其上传至FTP,FTP相关操作类如下所示:
2.1 FTP登陆类,如下:
/**
* 获取FTPClient对象
* @param ftpHost FTP主机服务器
* @param ftpPassword FTP 登录密码
* @param ftpUserName FTP登录用户名
* @param ftpPort FTP端口 默认为21
* @return
*/
public static FTPClient getFTPClient(String ftpHost, String ftpPassword, String ftpUserName, int ftpPort) {
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
logger.info("未连接到FTP,用户名或密码错误。");
ftpClient.disconnect();
} else {
logger.info("FTP连接成功。");
}
} catch (SocketException e) {
e.printStackTrace();
logger.info("FTP的IP地址可能错误,请正确配置。");
} catch (IOException e) {
e.printStackTrace();
logger.info("FTP的端口错误,请正确配置。");
}catch(Exception evt){
evt.printStackTrace();
}
return ftpClient;
}
2.2 上传FTP类,如下所示:
public boolean upMyFile(String fileName, InputStream ins, String Dir) {
FTPClient ftpClient = FTPUtil.getFTPClient(ftpHost, ftpPassword, ftpUserName, ftpPort);
try {
// ftpClient.setControlEncoding("GBK");
// // 设置编码为中文否则文件测试错误
//
// try {
// ftpClient.connect(ip, prot);
// if (!ftpClient.login(userId, pwd)) {
// System.out.println("系统登录不成功!");
// error = "ftp登录失败";
// }
// DateFormat df = new SimpleDateFormat("yyyyMMdd");
// String strDate = df.format(new Date());
// 改变当前目录到日期目录下,如果存在该目录就返回250,不存在则创建目录
//定位文件的路径
if (ftpClient.cwd("/" + Dir) == 250) {
ftpClient.cwd("/" + Dir);
// 当前工作目录指定到Dir目录下,目录存在返回 250.
} else {
ftpClient.makeDirectory("/" + Dir);
// 这个方法不会改变当前工作目录
// 当前工作目录指定到Dir目录下
ftpClient.cwd("/" + Dir);
}
System.out.println("当前工作目录:" + ftpClient.printWorkingDirectory());
// FTPFile[] ftpFiles = null;
String dir = "root/" + Dir;
// int i = 1;
// int nums = 0;
// while (true) {// 该功能有待完善。有可能出现 0010
// ftpFiles = ftpClient.listFiles(dir); // ftpClient.mlistDir();//
// // 列出当前目录下的文件数
//
// nums = ftpFiles.length;
// i++;
// if (nums - 1 >= 10000) {
// // 001文件夹中图片个数达到1万时,创建文件夹002,依次类推。
// System.out.println("当前工作目录:" +
// ftpClient.printWorkingDirectory());
//
// ftpClient.changeToParentDirectory();
//
// // if (ftpClient.cwd("00" + i) == 250) {
// // dir = "root/" + strDate + "/" + "00" + i;
// // } else {
// // ftpClient.makeDirectory("00" + i);
// // ftpClient.cwd("00" + i);
// // dir = "root/" + strDate + "/" + "00" + i;
// // break;
// // }
// } else {
// break;
// }
// }
System.out.println(ftpClient.changeWorkingDirectory(dir));
//设置FTP文件类型
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//ftpClient.storeFileStream(remote)
if (ftpClient.storeFile(fileName, ins)) {
file = dir + "/" + fileName;
} else {
System.out.println("指定文件上传失败!此目录下该用户无权上传文件!");
return false;
}
ftpClient.logout();
return true;
} catch (SocketException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
你可以自已申请一个FTP服务器地址,帐号,密码,端口进行测试。
(完,待续......)