关于Java文件传输 大家好,我是新手,最近在做一个聊天的小程序。经过测试后,在单机环境下,传输文件没有任何问题,但是在局域网环境下就会出现数据问题。我用的是TCP协议,照理不会丢失数据请高手帮忙,小弟万分感激! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你能说的再细一点吗,我现在只能猜测了...你是不是用了int data=0;while((data=inputstream.read())!=-1){ outputstream.write(data);}或者类似的,发现一直停在读取的位置,即读不到文件尾.如果是的话,建议你写数据时用writeUTF(),读数据时用readUTF(),反正是小聊天程序,牺牲点速度,把话当字符串传吧.如果你用我上面写的那样的代码传数据,传文件一端不关闭socket这一端是读不到文件尾的.而用writeUTF(),readUTF()不会有那样的麻烦.网络聊天小程序我们前两天也做了,上面说的那种问题是我遇到的唯一问题,如果是其他问题,麻烦你把情况说的详细点,把代码拷过来更好. 有些变量你就不要管了,主要是buffer bytesRead bytesReaded/* * 向目标用户发送文件的线程 */ class SendFileThServer extends Thread { private Socket sock_File; private FileTransefer ftServer; private File file; private String str_Desc; byte[] buffer; private long size; int bytesRead = 0; long bytesReaded = 0; int percent = 0; DataInputStream dis = null; DataOutputStream dos = null; public SendFileThServer(Socket sock_File, FileTransefer ftServer, long size, File file, String str_Desc) { super(); this.sock_File = sock_File; this.ftServer = ftServer; this.file = file; this.size = size; this.str_Desc = str_Desc; } public void run() { try { dis = new DataInputStream(new BufferedInputStream( new FileInputStream(file))); dos = new DataOutputStream(new BufferedOutputStream(sock_File .getOutputStream())); ftServer.jpb.setMinimum(0); ftServer.jpb.setMaximum((int) size); if (size % 1024 == 0) { buffer = new byte[1024]; } if (size < 1024) { buffer = new byte[(int) size]; } if (size > 1024) { buffer = new byte[1024]; } while ((bytesRead = dis.read(buffer)) != -1) { int result = (int) (size - bytesReaded); if (result < 1024) { buffer = new byte[result]; } dos.write(buffer); dos.flush(); bytesReaded = bytesRead + bytesReaded; ftServer.jpb.setValue((int) bytesReaded); percent = (int) ((float) bytesReaded / size * 100); ftServer.jl1.setText(str_Desc + " " + percent + "%"); System.out.println("server读到的缓冲字节数:" + bytesRead); System.out.println("server读取的字节数:" + bytesReaded); } if (bytesRead == 0) { System.out.println("server读到的缓冲字节数:" + bytesRead); System.out.println("server读取的字节数:" + bytesReaded); } if (bytesRead == -1) { ftServer.jl1.setText(str_Desc + " " + percent + "%" + " " + "发送完毕!"); System.out.println("server读到的缓冲字节数:" + bytesRead); System.out.println("server读取的字节数:" + bytesReaded); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (dos != null) dos.close(); if (dis != null) dis.close(); if (sock_File != null) sock_File.close(); } catch (Exception e1) { e1.printStackTrace(); } } ftServer.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { ftServer.dispose(); } }); } }/* * 目标用户接受文件 */ class ReadFileTh extends Thread { Socket sock_File; FileTransefer ftClient; String fileName; long size; File currentDirectory; String str_Desc; byte[] buffer; int bytesRead = 0; long bytesReaded = 0; int percent = 0; DataInputStream dis = null; DataOutputStream dos = null; public ReadFileTh(Socket sock_File, FileTransefer ftClient, long size, String fileName, File currentDirectory, String str_Desc) { super(); this.sock_File = sock_File; this.ftClient = ftClient; this.fileName = fileName; this.size = size; this.currentDirectory = currentDirectory; this.str_Desc = str_Desc; } public void run() { try { dos = new DataOutputStream(new BufferedOutputStream( new FileOutputStream(currentDirectory.getPath() + "/" + fileName))); dis = new DataInputStream(new BufferedInputStream(sock_File .getInputStream())); ftClient.jpb.setMinimum(0); ftClient.jpb.setMaximum((int) size); if (size % 1024 == 0) { buffer = new byte[1024]; } if (size < 1024) { buffer = new byte[(int) size]; } if (size > 1024) { buffer = new byte[1024]; } while ((bytesRead = dis.read(buffer)) != -1) { int result = (int) (size - bytesReaded); if (result < 1024) { buffer = new byte[result]; } dos.write(buffer); dos.flush(); bytesReaded = bytesReaded + bytesRead; ftClient.jpb.setValue((int) bytesReaded); percent = (int) ((float) bytesReaded / size * 100); ftClient.jl1.setText(str_Desc + " " + percent + "%"); System.out.println("client读到的缓冲字节数:" + bytesRead); System.out.println("client读取的字节数:" + bytesReaded); } if (bytesRead == -1) { ftClient.jl1.setText(str_Desc + " " + percent + "%" + " " + "接收完毕!"); System.out.println("client读到的缓冲字节数:" + bytesRead); System.out.println("client读取的字节数:" + bytesReaded); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (dos != null) dos.close(); if (dis != null) dis.close(); if (sock_File != null) sock_File.close(); } catch (Exception e1) { e1.printStackTrace(); } } ftClient.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { ftClient.dispose(); } }); } } 恩 有些变量你就不要管了,主要是buffer bytesRead bytesReaded/* * 向目标用户发送文件的线程 */ class SendFileThServer extends Thread { private Socket sock_File; private FileTransefer ftServer; private File file; private String str_Desc; byte[] buffer; private long size; int bytesRead = 0; long bytesReaded = 0; int percent = 0; DataInputStream dis = null; DataOutputStream dos = null; public SendFileThServer(Socket sock_File, FileTransefer ftServer, long size, File file, String str_Desc) { super(); this.sock_File = sock_File; this.ftServer = ftServer; this.file = file; this.size = size; this.str_Desc = str_Desc; } public void run() { try { dis = new DataInputStream(new BufferedInputStream( new FileInputStream(file))); dos = new DataOutputStream(new BufferedOutputStream(sock_File .getOutputStream())); ftServer.jpb.setMinimum(0); ftServer.jpb.setMaximum((int) size); if (size % 1024 == 0) { buffer = new byte[1024]; } if (size < 1024) { buffer = new byte[(int) size]; } if (size > 1024) { buffer = new byte[1024]; } while ((bytesRead = dis.read(buffer)) != -1) { int result = (int) (size - bytesReaded); if (result < 1024) { buffer = new byte[result]; } dos.write(buffer); dos.flush(); bytesReaded = bytesRead + bytesReaded; ftServer.jpb.setValue((int) bytesReaded); percent = (int) ((float) bytesReaded / size * 100); ftServer.jl1.setText(str_Desc + " " + percent + "%"); System.out.println("server读到的缓冲字节数:" + bytesRead); System.out.println("server读取的字节数:" + bytesReaded); } if (bytesRead == 0) { System.out.println("server读到的缓冲字节数:" + bytesRead); System.out.println("server读取的字节数:" + bytesReaded); } if (bytesRead == -1) { ftServer.jl1.setText(str_Desc + " " + percent + "%" + " " + "发送完毕!"); System.out.println("server读到的缓冲字节数:" + bytesRead); System.out.println("server读取的字节数:" + bytesReaded); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (dos != null) dos.close(); if (dis != null) dis.close(); if (sock_File != null) sock_File.close(); } catch (Exception e1) { e1.printStackTrace(); } } ftServer.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { ftServer.dispose(); } }); } }/* * 目标用户接受文件 */ class ReadFileTh extends Thread { Socket sock_File; FileTransefer ftClient; String fileName; long size; File currentDirectory; String str_Desc; byte[] buffer; int bytesRead = 0; long bytesReaded = 0; int percent = 0; DataInputStream dis = null; DataOutputStream dos = null; public ReadFileTh(Socket sock_File, FileTransefer ftClient, long size, String fileName, File currentDirectory, String str_Desc) { super(); this.sock_File = sock_File; this.ftClient = ftClient; this.fileName = fileName; this.size = size; this.currentDirectory = currentDirectory; this.str_Desc = str_Desc; } public void run() { try { dos = new DataOutputStream(new BufferedOutputStream( new FileOutputStream(currentDirectory.getPath() + "/" + fileName))); dis = new DataInputStream(new BufferedInputStream(sock_File .getInputStream())); ftClient.jpb.setMinimum(0); ftClient.jpb.setMaximum((int) size); if (size % 1024 == 0) { buffer = new byte[1024]; } if (size < 1024) { buffer = new byte[(int) size]; } if (size > 1024) { buffer = new byte[1024]; } while ((bytesRead = dis.read(buffer)) != -1) { int result = (int) (size - bytesReaded); if (result < 1024) { buffer = new byte[result]; } dos.write(buffer); dos.flush(); bytesReaded = bytesReaded + bytesRead; ftClient.jpb.setValue((int) bytesReaded); percent = (int) ((float) bytesReaded / size * 100); ftClient.jl1.setText(str_Desc + " " + percent + "%"); System.out.println("client读到的缓冲字节数:" + bytesRead); System.out.println("client读取的字节数:" + bytesReaded); } if (bytesRead == -1) { ftClient.jl1.setText(str_Desc + " " + percent + "%" + " " + "接收完毕!"); System.out.println("client读到的缓冲字节数:" + bytesRead); System.out.println("client读取的字节数:" + bytesReaded); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (dos != null) dos.close(); if (dis != null) dis.close(); if (sock_File != null) sock_File.close(); } catch (Exception e1) { e1.printStackTrace(); } } ftClient.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { ftClient.dispose(); } }); } } 如何能让代码变成软件? 关于object类 紧急求助~~~~~ 关于socket发送信息的问题,大侠帮我看看啊 请问:怎么写helloworld程序都会出错的,为什么?(在线等待) 修改Look And Feel 后如何使JFrame和ContentPanel面板上的控件更新 怎么把按钮,标签之类放在图片背景上面? 请教高手,关于内存泄漏。急! 在java官方网站上下载的java在线文档,怎么是一个超长文件名的,不知道用什么方式打开的文件,怎么回事? 求助生成随机数……[0,1] 问题 java 如何在jPanel中画图
你是不是用了int data=0;
while((data=inputstream.read())!=-1){
outputstream.write(data);
}或者类似的,发现一直停在读取的位置,即读不到文件尾.
如果是的话,建议你写数据时用writeUTF(),读数据时用readUTF(),反正是小聊天程序,牺牲点速度,把话当字符串传吧.
如果你用我上面写的那样的代码传数据,传文件一端不关闭socket这一端是读不到文件尾的.
而用writeUTF(),readUTF()不会有那样的麻烦.
网络聊天小程序我们前两天也做了,上面说的那种问题是我遇到的唯一问题,如果是其他问题,麻烦你把情况说的详细点,把代码拷过来更好.
/*
* 向目标用户发送文件的线程
*/
class SendFileThServer extends Thread {
private Socket sock_File;
private FileTransefer ftServer;
private File file;
private String str_Desc;
byte[] buffer;
private long size;
int bytesRead = 0;
long bytesReaded = 0;
int percent = 0;
DataInputStream dis = null;
DataOutputStream dos = null; public SendFileThServer(Socket sock_File, FileTransefer ftServer,
long size, File file, String str_Desc) {
super();
this.sock_File = sock_File;
this.ftServer = ftServer;
this.file = file;
this.size = size;
this.str_Desc = str_Desc;
} public void run() { try { dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(file)));
dos = new DataOutputStream(new BufferedOutputStream(sock_File
.getOutputStream())); ftServer.jpb.setMinimum(0);
ftServer.jpb.setMaximum((int) size); if (size % 1024 == 0) {
buffer = new byte[1024];
}
if (size < 1024) {
buffer = new byte[(int) size];
}
if (size > 1024) {
buffer = new byte[1024];
}
while ((bytesRead = dis.read(buffer)) != -1) {
int result = (int) (size - bytesReaded);
if (result < 1024) {
buffer = new byte[result];
} dos.write(buffer);
dos.flush();
bytesReaded = bytesRead + bytesReaded;
ftServer.jpb.setValue((int) bytesReaded);
percent = (int) ((float) bytesReaded / size * 100);
ftServer.jl1.setText(str_Desc + " " + percent + "%");
System.out.println("server读到的缓冲字节数:" + bytesRead);
System.out.println("server读取的字节数:" + bytesReaded); } if (bytesRead == 0) {
System.out.println("server读到的缓冲字节数:" + bytesRead);
System.out.println("server读取的字节数:" + bytesReaded);
} if (bytesRead == -1) {
ftServer.jl1.setText(str_Desc + " " + percent + "%"
+ " " + "发送完毕!");
System.out.println("server读到的缓冲字节数:" + bytesRead);
System.out.println("server读取的字节数:" + bytesReaded);
} } catch (IOException e) {
e.printStackTrace();
} finally { try {
if (dos != null)
dos.close();
if (dis != null)
dis.close();
if (sock_File != null)
sock_File.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
ftServer.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ftServer.dispose();
}
});
}
}
/*
* 目标用户接受文件
*/
class ReadFileTh extends Thread {
Socket sock_File;
FileTransefer ftClient;
String fileName;
long size;
File currentDirectory;
String str_Desc;
byte[] buffer;
int bytesRead = 0;
long bytesReaded = 0;
int percent = 0;
DataInputStream dis = null;
DataOutputStream dos = null; public ReadFileTh(Socket sock_File, FileTransefer ftClient, long size,
String fileName, File currentDirectory, String str_Desc) {
super();
this.sock_File = sock_File;
this.ftClient = ftClient;
this.fileName = fileName;
this.size = size;
this.currentDirectory = currentDirectory;
this.str_Desc = str_Desc;
} public void run() { try {
dos = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(currentDirectory.getPath() + "/"
+ fileName)));
dis = new DataInputStream(new BufferedInputStream(sock_File
.getInputStream())); ftClient.jpb.setMinimum(0);
ftClient.jpb.setMaximum((int) size); if (size % 1024 == 0) {
buffer = new byte[1024];
} if (size < 1024) {
buffer = new byte[(int) size];
}
if (size > 1024) {
buffer = new byte[1024];
} while ((bytesRead = dis.read(buffer)) != -1) {
int result = (int) (size - bytesReaded);
if (result < 1024) {
buffer = new byte[result];
}
dos.write(buffer);
dos.flush();
bytesReaded = bytesReaded + bytesRead;
ftClient.jpb.setValue((int) bytesReaded);
percent = (int) ((float) bytesReaded / size * 100);
ftClient.jl1.setText(str_Desc + " " + percent + "%");
System.out.println("client读到的缓冲字节数:" + bytesRead);
System.out.println("client读取的字节数:" + bytesReaded);
} if (bytesRead == -1) {
ftClient.jl1.setText(str_Desc + " " + percent + "%"
+ " " + "接收完毕!");
System.out.println("client读到的缓冲字节数:" + bytesRead);
System.out.println("client读取的字节数:" + bytesReaded);
} } catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dos != null)
dos.close();
if (dis != null)
dis.close();
if (sock_File != null)
sock_File.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
ftClient.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ftClient.dispose();
}
});
}
}
有些变量你就不要管了,主要是buffer bytesRead bytesReaded
/*
* 向目标用户发送文件的线程
*/
class SendFileThServer extends Thread {
private Socket sock_File;
private FileTransefer ftServer;
private File file;
private String str_Desc;
byte[] buffer;
private long size;
int bytesRead = 0;
long bytesReaded = 0;
int percent = 0;
DataInputStream dis = null;
DataOutputStream dos = null; public SendFileThServer(Socket sock_File, FileTransefer ftServer,
long size, File file, String str_Desc) {
super();
this.sock_File = sock_File;
this.ftServer = ftServer;
this.file = file;
this.size = size;
this.str_Desc = str_Desc;
} public void run() { try { dis = new DataInputStream(new BufferedInputStream(
new FileInputStream(file)));
dos = new DataOutputStream(new BufferedOutputStream(sock_File
.getOutputStream())); ftServer.jpb.setMinimum(0);
ftServer.jpb.setMaximum((int) size); if (size % 1024 == 0) {
buffer = new byte[1024];
}
if (size < 1024) {
buffer = new byte[(int) size];
}
if (size > 1024) {
buffer = new byte[1024];
}
while ((bytesRead = dis.read(buffer)) != -1) {
int result = (int) (size - bytesReaded);
if (result < 1024) {
buffer = new byte[result];
} dos.write(buffer);
dos.flush();
bytesReaded = bytesRead + bytesReaded;
ftServer.jpb.setValue((int) bytesReaded);
percent = (int) ((float) bytesReaded / size * 100);
ftServer.jl1.setText(str_Desc + " " + percent + "%");
System.out.println("server读到的缓冲字节数:" + bytesRead);
System.out.println("server读取的字节数:" + bytesReaded); } if (bytesRead == 0) {
System.out.println("server读到的缓冲字节数:" + bytesRead);
System.out.println("server读取的字节数:" + bytesReaded);
} if (bytesRead == -1) {
ftServer.jl1.setText(str_Desc + " " + percent + "%"
+ " " + "发送完毕!");
System.out.println("server读到的缓冲字节数:" + bytesRead);
System.out.println("server读取的字节数:" + bytesReaded);
} } catch (IOException e) {
e.printStackTrace();
} finally { try {
if (dos != null)
dos.close();
if (dis != null)
dis.close();
if (sock_File != null)
sock_File.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
ftServer.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ftServer.dispose();
}
});
}
}
/*
* 目标用户接受文件
*/
class ReadFileTh extends Thread {
Socket sock_File;
FileTransefer ftClient;
String fileName;
long size;
File currentDirectory;
String str_Desc;
byte[] buffer;
int bytesRead = 0;
long bytesReaded = 0;
int percent = 0;
DataInputStream dis = null;
DataOutputStream dos = null; public ReadFileTh(Socket sock_File, FileTransefer ftClient, long size,
String fileName, File currentDirectory, String str_Desc) {
super();
this.sock_File = sock_File;
this.ftClient = ftClient;
this.fileName = fileName;
this.size = size;
this.currentDirectory = currentDirectory;
this.str_Desc = str_Desc;
} public void run() { try {
dos = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(currentDirectory.getPath() + "/"
+ fileName)));
dis = new DataInputStream(new BufferedInputStream(sock_File
.getInputStream())); ftClient.jpb.setMinimum(0);
ftClient.jpb.setMaximum((int) size); if (size % 1024 == 0) {
buffer = new byte[1024];
} if (size < 1024) {
buffer = new byte[(int) size];
}
if (size > 1024) {
buffer = new byte[1024];
} while ((bytesRead = dis.read(buffer)) != -1) {
int result = (int) (size - bytesReaded);
if (result < 1024) {
buffer = new byte[result];
}
dos.write(buffer);
dos.flush();
bytesReaded = bytesReaded + bytesRead;
ftClient.jpb.setValue((int) bytesReaded);
percent = (int) ((float) bytesReaded / size * 100);
ftClient.jl1.setText(str_Desc + " " + percent + "%");
System.out.println("client读到的缓冲字节数:" + bytesRead);
System.out.println("client读取的字节数:" + bytesReaded);
} if (bytesRead == -1) {
ftClient.jl1.setText(str_Desc + " " + percent + "%"
+ " " + "接收完毕!");
System.out.println("client读到的缓冲字节数:" + bytesRead);
System.out.println("client读取的字节数:" + bytesReaded);
} } catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dos != null)
dos.close();
if (dis != null)
dis.close();
if (sock_File != null)
sock_File.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
ftClient.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ftClient.dispose();
}
});
}
}