javasocket传输文件程序遇到一个bug问题
自己写了一个java socket复制文件,为什么只有这个程序关闭了,复制到的地方才能完整接收文件,在这之前,新地方的文件大小一直是0?
我最近在学习java网络编程,想着写一个复制文件的程序,从一个地方传到新地方,几次测试,程序执行完毕以后,新地方的文件大小一直是0字节,很奇怪,然后自己在线程中关闭了这个程序,新地方那个文件大小立即完整了,不知道是什么原因?
希望能帮我找出原因,是我哪一点没有理解到位吗?还是我写的程序哪里有一个bug?谢谢
下面是代码结构及代码情况
代码链接: link
这是项目结构
这是server端;
下面是FileProgre .java的全部代码:
package com.lyman.server;
import com.lyman.Gong.Go;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class FileProgre {
ServerSocket serverSocket;
public FileProgre() throws Exception{
serverSocket = new ServerSocket(8523);
}
private boolean flag = true;
public void createServer(){
Socket socket;
while (true) {
try {
System.out.println("serversocket已经准备好");
socket = serverSocket.accept();
System.out.println("创建serversocket成功:");
FileProgre.ProgreRunnable progre = new FileProgre.ProgreRunnable(socket);
Thread thread = new Thread(progre);
thread.start();
} catch (Exception e) {
System.out.println("创建serversocket有错误:" + e.getMessage());
}
}
}
class ProgreRunnable implements Runnable{
Socket socket;
OutputStream outputStream;
DataInputStream inputStream;
public ProgreRunnable( Socket socket){
this.socket = socket;
}
@Override
public void run() {
try{
Go.prin("正在等待客户端连接");
System.out.println("已有客户端连接:地址:"+socket.getInetAddress()+"端口:"+socket.getPort());
System.out.println("收到的信息:");
outputStream = socket.getOutputStream();
inputStream = new DataInputStream(socket.getInputStream());
String fileame= inputStream.readUTF();
long filelen =inputStream.readLong();
File path = new File("c:\\cee");
if (!path.exists()){
path.mkdir();
}
FileOutputStream fileOutputStream = new FileOutputStream(new File(path.getAbsolutePath()+File.separatorChar+fileame));
byte[] bytes = new byte[1024];
int nn=0;
int len=0;
//while (((nn = inputStream.read(bytes,0,bytes.length))!=-1)|(len==6082560)){
while (len <filelen ){
nn = inputStream.read(bytes,0,bytes.length);
len+=nn;
System.out.println("共读取了:"+len);
System.out.println("本次已经读取了:"+nn);
// writetofile(bytes,fileOutputStream);
fileOutputStream.write(bytes,0,nn);
fileOutputStream.flush();
}
System.out.println("文件传输完成名称:"+fileame+"大小为:"+filelen);
}catch (Exception e){
System.out.println("创建ProgreRunnable有错误:"+e.getMessage());
}finally {
System.out.println("即将关闭所有流:");
try {
if (outputStream!=null) {
outputStream.close();
}
if (inputStream!=null){
inputStream.close();
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
return;
}
}
}
public void writetofile(byte[] bytes,FileOutputStream fileOutputStream) throws Exception{
fileOutputStream.write(bytes);
fileOutputStream.flush();
}
}
下面是客户端的代码client.java:
package com.lyman.client;
import com.lyman.Gong.Go;
import java.io.File;
import java.io.InputStream;
import java.net.Socket;
public class Client {
public static void main(String[] args){
Socket socket = null;
try {
socket = new Socket("127.0.0.1",8523);
Go.prin("socket创建成功");
}catch (Exception e){
System.out.println("socket创建有误"+e.getMessage());
}
//System.out.println("请选择发送文字1,或者发送文件2");
// InputStream in = System.in;
doClient dcli = new doClient();
//dcli.progress(socket);
File file = new File("c:\\ce\\Redis-x64-3.2.100.msi");
if(file.exists()){
System.out.println("文件存在");
System.out.println("文件名称:"+file.getName()+"路径是:"+file.getAbsolutePath());
try {
dcli.profile(file,socket);
}catch (Exception e){
System.out.println("main函数有错误"+e.getMessage());
}
}else {
System.out.println("文件不存在");
}
}
}
下面是:doClient .java
package com.lyman.client;
import com.lyman.Gong.Go;
import java.io.*;
import java.net.Socket;
public class doClient {
private boolean str =true;
private boolean fil= true;
public void profile(File file,Socket socket) throws Exception{
System.out.printf("进入发送文件方法");
str=false;//进入文件发送,关闭文字发送
FileInputStream fileInputStream=null;
DataOutputStream outputStream =null;
long lenn;
long isread=0;
lenn = file.length();
byte[] bytes = new byte[1024];
int len=0;
try {
outputStream = new DataOutputStream(socket.getOutputStream());
fileInputStream = new FileInputStream(file);
outputStream.writeUTF(file.getName());
outputStream.writeLong(lenn);
System.out.println("======== 开始传输文件 ========");
while ((len=fileInputStream.read(bytes,0,bytes.length))!=-1){
outputStream.write(bytes,0, len);
outputStream.flush();
isread +=len;
System.out.println("已经读取了:"+isread+"字节"+",本次读取了"+len+"个字节");
System.out.println("已经发送:"+(100*isread/lenn)+"%");
}
System.out.println("======== 文件传输成功 ========");
}catch (Exception e){
System.out.println("progress有错误:"+e.getMessage());
}finally {
if (outputStream != null){
outputStream.close();
}
fileInputStream.close();
//socket.shutdownOutput();
socket.close();
System.out.println("客户端各种流已经关闭");
}
/*do{
try{
int n = fileInputStream.read(bytes);
isread +=n;
outputStream.write(bytes);
System.out.println("已经发送:"+100*(isread/len)+"%");
}catch (Exception e){
System.out.println("progress有错误:"+e.getMessage());
}
}while ((isread == len)&& fil);*/
}
public void progress(Socket socket){
fil=false;//进入蚊子发送,关闭文件发送
try{
OutputStream outputStream = socket.getOutputStream();
//File file = new File()
InputStream inputStream = socket.getInputStream();
PrintStream printStream = new PrintStream(outputStream);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
while (str){
printStream.printf("nihao,你好\n");
Go.prin("正在发送你好,nihao");
Go.prin(bufferedReader.readLine());
}
// String str = bufferedReader.readLine();
//System.out.println("收到的信息:"+str);
}catch (Exception e){
System.out.println("progress有错误:"+e.getMessage());
}
}
}