java点对点传输文件代码 100
急!实现传输文件功能。cs。其他地方找到的错误不是一两个。要无错误的,谢谢个位。我运行后效果好可以追加分数。麻烦各位!...
急! 实现传输文件功能。cs。其他地方找到的错误不是一两个。要无错误的,谢谢个位。我运行后效果好可以追加分数。 麻烦各位!
展开
4个回答
展开全部
//在我电脑运行没问题,把E:/EKI.txt传送到D:/EKI.txt你可以换成其它文件
//先运行Server,然后client,共三个class有问题QQ23400262
package ch.socket.file;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends Thread {
public static int port = 6789;
public static String host = "127.0.0.1";
private static ServerSocket server = null;
public void run() {
if (server == null) {
try {
// 1、新建ServerSocket实例
server = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("服务器启动...");
while (true) {
try {
// 2、访问ServerSocket实例的accept方法取得一个客户端Socket对象
Socket client = server.accept();
if (client == null)
continue;
new SocketConnection(client, "D:\\").start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new Server().start();
}
}
package ch.socket.file;
import java.io.*;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
private Socket client;
private boolean connected;
public boolean isConnected() {
return connected;
}
public void setConnected(boolean connected) {
this.connected = connected;
}
public Client(String host, int port) {
try {
// 1、新建Socket对象
client = new Socket(host, port);
System.out.println("服务器连接成功!");
this.connected = true;
} catch (UnknownHostException e) {
this.connected = false;
close();
} catch (IOException e) {
System.out.println("服务器连接失败!");
this.connected = false;
close();
}
}
/**
* 将文件内容发送出去
*
* @param filepath
* 文件的全路径名
*/
public void sendFile(String filepath) {
DataOutputStream out = null;
DataInputStream reader = null;
try {
if (client == null)
return;
File file = new File(filepath);
reader = new DataInputStream(new BufferedInputStream(
new FileInputStream(file)));
// 2、将文件内容写到Socket的输出流中
out = new DataOutputStream(client.getOutputStream());
out.writeUTF(file.getName()); // 附带文件名
int bufferSize = 2048; // 2K
byte[] buf = new byte[bufferSize];
int read = 0;
while ((read = reader.read(buf)) != -1) {
out.write(buf, 0, read);
}
out.flush();
} catch (IOException ex) {
ex.printStackTrace();
close();
} finally {
try {
reader.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭Socket
*/
public void close() {
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client(Server.host, Server.port);
if (client.isConnected()) {
client.sendFile("E:\\EKI.txt");
client.close();
}
}
}
package ch.socket.file;
import java.net.Socket;
import java.io.*;
public class SocketConnection extends Thread{
private Socket client;
private String filepath;
public SocketConnection(Socket client, String filepath){
this.client = client;
this.filepath = filepath;
}
public void run(){
if(client == null) return;
DataInputStream in = null;
DataOutputStream writer = null;
try{
//3、访问Socket对象的getInputStream方法取得客户端发送过来的数据流
in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
String fileName = in.readUTF(); //取得附带的文件名
if(filepath.endsWith("/") == false && filepath.endsWith("\\") == false){
filepath += "\\";
}
filepath += fileName;
//4、将数据流写到文件中
writer = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(new File(filepath)))));
int bufferSize = 2048;
byte[] buf = new byte[bufferSize];
int read = 0;
while((read=in.read(buf)) != -1){
writer.write(buf, 0, read);
}
writer.flush();
System.out.println("数据接收完毕");
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
in.close();
writer.close();
client.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
//先运行Server,然后client,共三个class有问题QQ23400262
package ch.socket.file;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server extends Thread {
public static int port = 6789;
public static String host = "127.0.0.1";
private static ServerSocket server = null;
public void run() {
if (server == null) {
try {
// 1、新建ServerSocket实例
server = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("服务器启动...");
while (true) {
try {
// 2、访问ServerSocket实例的accept方法取得一个客户端Socket对象
Socket client = server.accept();
if (client == null)
continue;
new SocketConnection(client, "D:\\").start();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new Server().start();
}
}
package ch.socket.file;
import java.io.*;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
private Socket client;
private boolean connected;
public boolean isConnected() {
return connected;
}
public void setConnected(boolean connected) {
this.connected = connected;
}
public Client(String host, int port) {
try {
// 1、新建Socket对象
client = new Socket(host, port);
System.out.println("服务器连接成功!");
this.connected = true;
} catch (UnknownHostException e) {
this.connected = false;
close();
} catch (IOException e) {
System.out.println("服务器连接失败!");
this.connected = false;
close();
}
}
/**
* 将文件内容发送出去
*
* @param filepath
* 文件的全路径名
*/
public void sendFile(String filepath) {
DataOutputStream out = null;
DataInputStream reader = null;
try {
if (client == null)
return;
File file = new File(filepath);
reader = new DataInputStream(new BufferedInputStream(
new FileInputStream(file)));
// 2、将文件内容写到Socket的输出流中
out = new DataOutputStream(client.getOutputStream());
out.writeUTF(file.getName()); // 附带文件名
int bufferSize = 2048; // 2K
byte[] buf = new byte[bufferSize];
int read = 0;
while ((read = reader.read(buf)) != -1) {
out.write(buf, 0, read);
}
out.flush();
} catch (IOException ex) {
ex.printStackTrace();
close();
} finally {
try {
reader.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关闭Socket
*/
public void close() {
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client = new Client(Server.host, Server.port);
if (client.isConnected()) {
client.sendFile("E:\\EKI.txt");
client.close();
}
}
}
package ch.socket.file;
import java.net.Socket;
import java.io.*;
public class SocketConnection extends Thread{
private Socket client;
private String filepath;
public SocketConnection(Socket client, String filepath){
this.client = client;
this.filepath = filepath;
}
public void run(){
if(client == null) return;
DataInputStream in = null;
DataOutputStream writer = null;
try{
//3、访问Socket对象的getInputStream方法取得客户端发送过来的数据流
in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
String fileName = in.readUTF(); //取得附带的文件名
if(filepath.endsWith("/") == false && filepath.endsWith("\\") == false){
filepath += "\\";
}
filepath += fileName;
//4、将数据流写到文件中
writer = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(new File(filepath)))));
int bufferSize = 2048;
byte[] buf = new byte[bufferSize];
int read = 0;
while((read=in.read(buf)) != -1){
writer.write(buf, 0, read);
}
writer.flush();
System.out.println("数据接收完毕");
}catch(IOException ex){
ex.printStackTrace();
}finally{
try{
in.close();
writer.close();
client.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
展开全部
实现思路:在知道远程服务器的文件存储路径的情况下,之后通过IO形式进行上传文件即可。
* 上传文件
*
* @param fileName
* @param plainFilePath 文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上传文件开始");
Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("检查文件路径是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
备注:只需要修改上传的服务器地址、用户名、密码即可进行服务器访问上传。根据实际需要修改即可。
* 上传文件
*
* @param fileName
* @param plainFilePath 文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上传文件开始");
Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("检查文件路径是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}
备注:只需要修改上传的服务器地址、用户名、密码即可进行服务器访问上传。根据实际需要修改即可。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我有个,但是窗口方面还有点问题,没来及做。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你肯定学电子信息的,搞不好就和我是一个班。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询