package com.jfc.ftp.service.impl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.charset.Charset;
import javax.swing.SwingUtilities;
import com.jfc.ftp.service.FTPService;
import com.jfc.ftp.tools.FTPThread;
import com.jfc.ftp.tools.LogTools;
import com.jfc.ftp.tools.SystemTools;
import com.jfc.ftp.util.Constant;
import com.jfc.ftp.util.FileUtil;
public class SocketFTPService implements FTPService{
public String host;
public int port;
public String user;
public String pswd;
public Socket ftpSocket;
public ServerSocket serverSocket;
public Socket dataSocket;
public InputStream serverInput;
public PrintStream serverOutput;
public BufferedReader serverReader;
public boolean passiveFlag = true;
/**
* 根据服务器地址、端口获得ftp链接
* @param host
* @param port
* @return
*/
public String getFTPConnect(String host, int port) {
try {
ftpSocket = new Socket(host, port);
ftpSocket.setSoTimeout(10000);
if(ftpSocket != null) {
this.host = host;
this.port = port;
}
serverOutput = new PrintStream(new BufferedOutputStream(ftpSocket.getOutputStream()), true, "GBK");
serverReader = new BufferedReader(new InputStreamReader(ftpSocket.getInputStream()));
} catch(SocketTimeoutException e) {
if (!ftpSocket.isClosed() && ftpSocket.isConnected())
System.out.println("读取数据超时!");
else
System.out.println("连接超时");
} catch(Exception e) {
e.printStackTrace();
LogTools.logger.error(e.getMessage());
}
return readRespond();
}
/**
* 执行登录
* @param user
* @param pswd
* @return
*/
public boolean doLoginFTP(String user, String pswd) {
String responseStr = doFTPCommand("USER " + user);
if(responseStr.length() > 0) {
responseStr = doFTPCommand("PASS " + pswd);
if(responseStr.length() > 0){
this.user = user;
this.pswd = pswd;
return true;
}
}
return false;
}
/**
* 得到指定路径下的文件列表
* @param dir
* @return
*/
public String getListFile(String dir){
//this.passiveFlag = passiveFlag;
try {
//if(dataSocket == null) {
dataSocket = getDataSocket();
//}
doFTPCommand("LIST -al " + dir);
BufferedReader data_br = null;
data_br = new BufferedReader(
new InputStreamReader(dataSocket.getInputStream(), Charset.forName("GBK")));
String line;
String lines_str = "";
while (true) {
line = data_br.readLine();
// 如果line为null,则说明读到了EOF。
if (line == null)
break;
lines_str += line + '\n';
}
if (lines_str == "")
lines_str += '\n';
return lines_str;
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
/**
* 下载指定文件<br>
* 用于文件夹的递归下载
* @param filepath
* @param savepath
*/
public void download(String filepath, String savepath) {
String filename = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length());
BufferedInputStream dataInput = null;
BufferedOutputStream dataOutput = null;
byte[] temp = new byte[Constant.TEMP_BYTE_LENGTH];
try {
doFTPCommand("TYPE I");
dataSocket = getDataSocket();
doFTPCommand("RETR " + filepath);
dataInput = new BufferedInputStream(dataSocket.getInputStream());
dataOutput = new BufferedOutputStream(new FileOutputStream(new File(savepath + File.separator + filename)));
int read = 0;
int hadRead = 0;
while((read = dataInput.read(temp, 0, temp.length)) != -1) {
dataOutput.write(temp, 0, read);
hadRead += read;
if(hadRead % 4096 == 0) {
dataOutput.flush();
}
}
dataOutput.flush();
dataOutput.close();
dataInput.close();
readRespond();
} catch (IOException e) {
e.printStackTrace();
try {
dataOutput.flush();
dataOutput.close();
dataInput.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
/**
* 下载指定文件<br>
* 用于下载普通文件,带进度条显示
* @param rowIndex
* @param filepath
* @param filesize
* @param savepath
*/
public void download(int rowIndex, String filepath, long filesize, String savepath) {
String filename = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length());
BufferedInputStream dataInput = null;
BufferedOutputStream dataOutput = null;
byte[] temp = new byte[Constant.TEMP_BYTE_LENGTH];
try {
doFTPCommand("TYPE I");
dataSocket = getDataSocket();
doFTPCommand("RETR " + filepath);
dataInput = new BufferedInputStream(dataSocket.getInputStream());
dataOutput = new BufferedOutputStream(new FileOutputStream(new File(savepath + File.separator + filename)));
int read = 0;
int hadRead = 0;
long startTime = System.currentTimeMillis();
SystemTools.addObserver();
while((read = dataInput.read(temp, 0, temp.length)) != -1) {
dataOutput.write(temp, 0, read);
hadRead += read;
if(hadRead % 4096 == 0) {
dataOutput.flush();
}
SystemTools.getCurrentSpeed(rowIndex, startTime, hadRead);
SystemTools.getPrice(rowIndex, hadRead, filesize);
SwingUtilities.invokeLater(SystemTools.updateProgressBarRunnable);
}
dataOutput.flush();
dataOutput.close();
dataInput.close();
SystemTools.getFinalSpeed(rowIndex, startTime, filesize, Constant.DOWNLOAD_TYPE_COMMON);
readRespond();
} catch (IOException e) {
e.printStackTrace();
try {
dataOutput.flush();
dataOutput.close();
dataInput.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
/**
* 使用断点续传方式下载文件<br>
* 首先查找要保存的路径下有无缓存文件(以.wfml为后缀)<br>
* 不存在 重新开始下载<br>
* 存在 继续<br>
* @param rowIndex
* @param filepath
* @param filesize
* @param savepath
*/
public void resumeBrokenTransfer(final FTPThread ftpThread, int rowIndex, String filepath, long filesize, String savepath){
String filename = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length());
BufferedInputStream dataInput = null;
BufferedOutputStream dataOutput = null;
File tempFile = new File(savepath + File.separator + filename + Constant.DOWNLOAD_TEMP_NAME);
long tempsize = tempFile.length();
byte[] temp = new byte[Constant.TEMP_BYTE_LENGTH];
try {
doFTPCommand("TYPE I");
dataSocket = getDataSocket();
//设定文件指针(下载位置)
doFTPCommand("REST " + tempsize);
doFTPCommand("RETR " + filepath);
dataInput = new BufferedInputStream(dataSocket.getInputStream());
dataOutput = new BufferedOutputStream(new FileOutputStream(tempFile, true));
int read = 0;
int hadRead = (int)tempsize;
ftpThread.hadRead = hadRead;
ftpThread.setStartTime(System.currentTimeMillis());
SystemTools.addObserver();
while(hadRead < filesize) {
if(ftpThread.getStatus() != Constant.THREAD_STATUS_RUNNABLE) {
ftpThread.setStartTime(System.currentTimeMillis());
}
while(ftpThread.getStatus() == Constant.THREAD_STATUS_RUNNABLE && (read = dataInput.read(temp, 0, temp.length)) != -1) {
dataOutput.write(temp, 0, read);
hadRead += read;
ftpThread.hadRead = hadRead;
if(hadRead % 4096 == 0) {
dataOutput.flush();
}
SystemTools.getCurrentSpeed(rowIndex, ftpT
没有合适的资源?快使用搜索试试~ 我知道了~
javaftpclient.zip_Java FTP上传_swing 上传_swing上传下载_swing文件上传
共169个文件
class:90个
java:42个
properties:11个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 61 浏览量
2022-09-24
17:41:26
上传
评论
收藏 1.24MB ZIP 举报
温馨提示
swing 页面,可用于ftp上传下载,可自己设置ftp地址,页面清晰简单,有上传滚动条,左边页面为本地磁盘,右边为ftp文件,有右键功能
资源推荐
资源详情
资源评论
收起资源包目录
javaftpclient.zip_Java FTP上传_swing 上传_swing上传下载_swing文件上传 (169个子文件)
jfc.log.2009-11-10 762B
jfc.log.2009-11-11 215B
jfc.log.2009-11-20 226B
jfc.log.2009-11-23 565B
jfc.log.2009-11-24 226B
jfc.log.2009-11-25 113B
SocketFTPService.class 11KB
SystemTools.class 10KB
FTPMainFrame.class 8KB
FTPThread.class 7KB
FTPTools.class 6KB
SocketFTPService$1.class 5KB
FileUtil.class 5KB
ResourcesConstant.class 4KB
FTPServerManagerDialog.class 4KB
FTPPopupMenuEvent$4.class 3KB
PidedIO.class 3KB
DownloadModel.class 3KB
FTPPopupMenuEvent$4$1.class 3KB
FrameTest.class 3KB
JDomUtil.class 3KB
FTPTableEvent$2.class 3KB
DialogTableEvent$1.class 3KB
DialogButtonEvent$1.class 3KB
FTPTableEvent$3.class 3KB
FTPPopupMenuEvent$6$1.class 3KB
FTPTableEvent$1.class 3KB
FTPButtonEvent.class 3KB
FTPMainMenu.class 3KB
FTPPopupMenu.class 2KB
StatusTableModel.class 2KB
ClientTableModel.class 2KB
FTPPopupMenuEvent$3$1.class 2KB
FTPPopupMenuEvent$1$1.class 2KB
ServerTableModel.class 2KB
FTPButtonEvent$4$1.class 2KB
FTPPopupMenuEvent$5.class 2KB
FTPButtonEvent$9$1.class 2KB
FTPPopupMenuEvent$3.class 2KB
FTPButtonEvent$10.class 2KB
FTPPopupMenuEvent.class 2KB
FTPMainFrameListener.class 2KB
FTPSiteModel.class 2KB
FileModel.class 2KB
PropertyUtil.class 2KB
Constant.class 2KB
FTPPopupMenuEvent$2.class 2KB
FTPTextFieldEvent.class 2KB
FTPButtonEvent$5.class 2KB
FTPButtonEvent$9.class 2KB
FTPButtonEvent$4.class 2KB
FTPButtonEvent$7.class 2KB
FTPTextFieldEvent$1.class 2KB
FTPButtonEvent$2.class 1KB
FrameTest$ProgressBarRenderer.class 1KB
FTPSiteTableModel.class 1KB
FTPTableEvent.class 1KB
ProgressBarRenderer.class 1KB
FTPPopupMenuEvent$7.class 1KB
FTPButtonEvent$6.class 1KB
FTPPopupMenuEvent$9.class 1KB
FTPPopupMenuEvent$6.class 1KB
FTPPopupMenuEvent$8.class 1KB
FrameTest$StatusTableModel.class 1KB
FTPButtonEvent$8.class 1KB
SystemTools$2.class 1KB
DialogButtonEvent.class 1KB
FrameTest$ProgressBarThread$1.class 1KB
FrameTest$ProgressBarThread.class 1KB
FTPMenuEvent$2.class 1KB
FTPButtonEvent$1.class 1KB
FrameTest$ProgressBarObserver.class 1KB
ProgressBarTools.class 1KB
FTPButtonEvent$3.class 1KB
FTPTextFieldEvent$2.class 1KB
FTPMenuEvent$1.class 1022B
FTPMenuEvent.class 1011B
FrameTest$AddButtonActionListener.class 1005B
OptionTableModel.class 996B
DialogButtonEvent$2.class 960B
SystemTools$1.class 948B
FTPService.class 923B
FTPPopupMenuEvent$1.class 890B
DialogTableEvent.class 875B
FrameTest$ProgressBarObservable.class 855B
FTPTest.class 855B
JavaFTPClient.class 846B
ProgressBarTools$1.class 738B
ProgressBarObservable.class 727B
ProgressBarObserver.class 696B
SpeedObservable.class 694B
SpeedObserver.class 667B
SwingTest.class 578B
LogTools.class 515B
Test.class 481B
FTPEvent.class 338B
.classpath 395B
Thumbs.db 8KB
desktop.ini 98B
ant.jar 699KB
共 169 条
- 1
- 2
资源评论
朱moyimi
- 粉丝: 61
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功