设为首页 收藏本站
查看: 882|回复: 0

[经验分享] 用edtftpj实现ftp文件上传和下载

[复制链接]

尚未签到

发表于 2015-11-6 07:31:15 | 显示全部楼层 |阅读模式
  1、要实现ftp文件上传和下载,需要下载一个jar包,下载地址:http://download.iyunv.com/detail/harderxin/5489309
  2、实现代码:
  

package com.image.common.util;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FileTransferClient;
import com.enterprisedt.net.ftp.FileTransferInputStream;
import com.enterprisedt.net.ftp.FileTransferOutputStream;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* FTP
*
* @author Administrator
*
*/
public class FTPUnit {
public static final SimpleDateFormat YMDHMS_SDFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 初始化连接
* @param url FTP服务端IP地址  如,192.168.1.254
* @param port FTP服务端端口  如,21
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
*/
public static FileTransferClient getFileTransferClient(String url,Integer port,String username,String password) throws Exception {
FileTransferClient ftp = new FileTransferClient();
ftp.setRemoteHost(url);
ftp.setRemotePort(port);
ftp.setUserName(username);
ftp.setPassword(password);
ftp.getAdvancedSettings().setControlEncoding("UTF-8");
ftp.getAdvancedFTPSettings().setConnectMode(FTPConnectMode.PASV);// 被动模式,数据连接由客户端发起
ftp.setContentType(FTPTransferType.BINARY); // BINARY模式用来传送可执行文件,压缩文件,和图片文件.
ftp.connect();
return ftp;
}
/**
* 关闭FTP
*/
public static void closeFileTransferClient(FileTransferClient ftp)throws Exception{
if(ftp!=null && ftp.isConnected()){
ftp.disconnect();
}
}
/**
* 上传单个文件
* @param localFilePath 本地文件路径
* @param remoteFilePath 远程存放路径
* @param folderPath 存放文件的路径 /车型/车号/类型拼音/年/月/日/
*/
public static void upload(String localFilePath, String remoteFileName,String folderPath,FileTransferClient ftp) throws Exception {
createDirectory(ftp, folderPath);
ftp.uploadFile(localFilePath, folderPath + remoteFileName);
}
/**
* 上传多个文件
* @localFilePathArr 本地文件路径
* @remoteFilePathArr 远程服务器文件存放路径
* @folderPath 远程服务器存放文件的文件夹
* @ftp ftp客服端连接
*/
public static void upload(String[] localFilePathArr,String[] remoteFilePathArr, String folderPath,FileTransferClient ftp) throws Exception {
try{
createDirectory(ftp, folderPath);
for (int i = 0; i < localFilePathArr.length; i++) {
ftp.uploadFile(localFilePathArr, remoteFilePathArr);
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 下载单个文件
* @param localFilePath 本地文件路径
* @param remoteFilePath 远程存放路径
*/
public static void download(String localFilePath, String remoteFilePath,FileTransferClient ftp)throws Exception {
ftp.downloadFile(localFilePath, remoteFilePath);
}
/**
* 按宽度高度压缩图片文件
* @param oldFile 要进行压缩的文件全路径
* @param newFile 新文件
* @param width 宽度
* @param height 高度
* @param quality 质量 0.0 -- 1.0 越小,压缩后图像效果越差,图片越小
* @return 返回压缩后的文件的全路径
*/
public static void zipWidthHeightImageFile(String oldFile,String newFile, int width, int height,
float quality,FileTransferClient ftp) throws Exception{
if (oldFile == null) {
return ;
}
/** 对服务器上的临时文件进行处理 */
String tempPath = &quot;ftp://&quot;+ftp.getUserName()
+&quot;:&quot;+ftp.getPassword()
+&quot;@&quot;+ftp.getRemoteHost()
+&quot;:&quot;+ftp.getRemotePort()
+ oldFile;
FileTransferInputStream ftis = FileTransferClient.downloadURLStream(tempPath);
Image srcFile = ImageIO.read(ftis);
/** 宽,高设定 */
BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
/** 压缩之后存放位置 */
FileTransferOutputStream ftos = null;
try{
ftos = ftp.uploadStream(newFile);
}catch(Exception e){
e.printStackTrace();
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(ftos);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/** 压缩质量 */
jep.setQuality(quality, true);
encoder.encode(tag, jep);
ftis.close();
ftos.flush();
ftos.close();
}
/**
* 按宽度高度压缩图片文件
* @param oldFile 要进行压缩的文件全路径
* @param newFile 新文件
* @param width 宽度
* @param height 高度
* @param quality 质量 0.0 -- 1.0 越小,压缩后图像效果越差,图片越小
* @return 返回压缩后的文件的全路径
*/
public static void zipWidthHeightImageFile(String[] oldFiles,String[] newFiles, int width, int height,
float quality,FileTransferClient ftp) throws Exception{
String tempPath = &quot;ftp://&quot;+ftp.getUserName()
+&quot;:&quot;+ftp.getPassword()
+&quot;@&quot;+ftp.getRemoteHost()
+&quot;:&quot;+ftp.getRemotePort()
+ &quot;/&quot;;
FileTransferInputStream ftis = null;
FileTransferOutputStream ftos = null;
Image srcFile = null;
BufferedImage tag = null;
JPEGImageEncoder encoder = null;
JPEGEncodeParam jep = null;
for (int i=0;i<oldFiles.length;i++) {
ftis = FileTransferClient.downloadURLStream(tempPath+oldFiles);
srcFile = ImageIO.read(ftis);
/** 宽,高设定 */
tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
/** 压缩之后存放位置 */
ftos = ftp.uploadStream(newFiles);
encoder = JPEGCodec.createJPEGEncoder(ftos);
jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/** 压缩质量 */
jep.setQuality(quality, true);
encoder.encode(tag, jep);
ftis.close();
ftos.flush();
ftos.close();
}
}
/**
* 删除文件或文件夹
* @param remotePath 远程文件或文件夹路径
* @param type 0:文件   其他:文件夹
*/
public static void delete(String remotePath,int type,FileTransferClient ftp) throws Exception{
if(remotePath!=null && &quot;&quot;.equals(remotePath)){
if(type==0){
try{
ftp.deleteFile(remotePath);
}catch (Exception e) {
e.printStackTrace();
}
}else{
try{
ftp.deleteDirectory(remotePath);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 删除文件或文件夹
* @param remotePath 远程文件或文件夹路径
* @param type 0:文件   其他:文件夹
*/
public static void delete(String[] remotePaths,int type,FileTransferClient ftp) throws Exception{
if(type==0){
for (String path : remotePaths) {
if(path!=null && !&quot;&quot;.equals(path)){
try{
ftp.deleteFile(path);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}else{
for (String path : remotePaths) {
if(path!=null && &quot;&quot;.equals(path)){
try{
ftp.deleteFile(path);
}catch (Exception e) {
ftp.deleteDirectory(path);
}
}
}
}
}
/**
* 创建文件夹
* @param path 存放的文件夹路径 /车型/车号/类型拼音/年/月/日/
*/
private static void createDirectory(FileTransferClient ftpClient,
String folderPath) throws Exception {
if (ftpClient.directoryList(folderPath).length==0) {
String[] fileNames = folderPath.split(&quot;/&quot;);
String temp = &quot;&quot;;
for (int i = 1; i < fileNames.length; i++) {
temp = temp+&quot;/&quot;+fileNames;
if (ftpClient.directoryList(temp).length==0) {
ftpClient.createDirectory(temp+&quot;/&quot;);
} else {
continue;
}
}
}
}
/**
* 获取图片的名字
* @param localFilePath 上传图片路径 请以&quot;/&quot;结尾
* @return 返回Map imgurl 原图 preimgurl 压缩图
*/
public static Map<String, String[]> getImagePath(String[] localFilePath,String folderPath){
Map<String, String[]> urlMap = new HashMap<String, String[]>();
String[] imgurl = null;
String[] preimgurl = null;
if(localFilePath!=null && localFilePath.length!=0){
int size = localFilePath.length;
imgurl = new String[size];
preimgurl = new String[size];
String temp;
for (int i = 0; i < size; i++) {
temp = System.currentTimeMillis()+&quot;_&quot;+ (int)(Math.random()*1000000);;
imgurl = folderPath + temp + getFileSuffix(localFilePath);
preimgurl = folderPath + temp + &quot;s&quot; + getFileSuffix(localFilePath);
}
}
urlMap.put(&quot;imgurl&quot;, imgurl);
urlMap.put(&quot;preimgurl&quot;, preimgurl);
return urlMap;
}
/**
* 获取文件名
*
* @return
*/
public static String getFileSuffix(String filePath) {
String suffix = &quot;&quot;;
if (filePath != null && !&quot;&quot;.equals(filePath)) {
suffix = filePath.substring(filePath.lastIndexOf(&quot;.&quot;)).toLowerCase();
}
return suffix;
}
/**
* 获取图片的拍摄时间
* @param args
*/
public static String[] getImageCreateTime(String[] imgPaths,FileTransferClient ftp)throws Exception{
String[] createTimes = null;
if(imgPaths!=null && imgPaths.length!=0){
createTimes = new String[imgPaths.length];
Metadata metadata = null;
ExifSubIFDDirectory directory = null;
byte[] buf = null;
for (int i=0;i<imgPaths.length;i++) {
buf = ftp.downloadByteArray(imgPaths);
metadata = JpegMetadataReader.readMetadata(new BufferedInputStream(new ByteArrayInputStream(buf)));
directory = metadata.getDirectory(ExifSubIFDDirectory.class);
try{
createTimes = directory.getDescription(ExifSubIFDDirectory.TAG_DATETIME_ORIGINAL);
}catch (Exception e) {
createTimes = null;
}
}
}
return createTimes;
}
public static void main(String[] args) {
try {
FileTransferClient ftpClient = FTPUnit.getFileTransferClient(&quot;192.168.1.254&quot;,21,&quot;ftp&quot;,&quot;123456&quot;);
FileInputStream fis = new FileInputStream(new File(&quot;D:\\ip.log&quot;));
FileTransferOutputStream ftos = ftpClient.uploadStream(&quot;/test/ip.log&quot;);
byte[] bytes = new byte[1024];   
int c;   
while ((c = fis.read(bytes)) != -1) {   
ftos.write(bytes, 0, c);   
}   
ftos.flush();
ftos.close();
fis.close();
ftpClient.disconnect();
//String[] str=getImageCreateTime(new String[]{&quot;/DF4/3838/xunjian/2013/5/20/1369045589718_150510.jpg&quot;},ftpClient);
} catch (Exception e) {
e.printStackTrace();
}
}
}

  



版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-135599-1-1.html 上篇帖子: Linux下FTP服务器的安装(proftpd) 下篇帖子: FTP匿名入侵
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表