Fitch_ 2016-01-28 12:50 采纳率: 0%
浏览 1517
已采纳

java socket 通信 服务端接收的文件大于客户端发出的文件 是什么鬼

//服务端
package Socket;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class Server2User extends Thread{
private Socket socket;
private InputStream is;
private BufferedInputStream bis;
private FileOutputStream fos;
private File file;
private byte name[] = new byte[1024];
private byte transport[] = new byte[1024];
private File path;

public Server2User(Socket socket)
{
    this.socket= socket;
}

public void run()
{
    try {
        is = socket.getInputStream();
        bis= new BufferedInputStream(is);
        bis.read(name);
        String filename = new String(name);
        filename = filename.trim();
        System.out.println("得到文件名"+filename);

         path = this.createFile(filename);
         System.out.println(path.getName());
        int length;
        fos = new FileOutputStream(path);
        int i = 0;
        while((length=bis.read(transport))!=-1)
        {
                this.write2file(transport);
                i++;
        }
        this.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


private void write2file(byte[] transport)
{
    try {
         fos.write(transport);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


private File createFile(String filename)
{

    try {
        file = new File("f:/test/"+filename);
        file.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return file;

}
private void close()
{

        try {
            if(is!=null)
                is.close();

            if(bis!=null)
                bis.close();
            if(fos!=null)
                fos.close();
            if(is!=null)
                is.close();
            if(socket!=null)
                socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

}
//客户端
package Socket;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.net.Socket;
import java.net.UnknownHostException;

public class ClientSocket_upload {
private Socket socket;
private OutputStream os;
private BufferedOutputStream bos;
private String path;
private FileInputStream fis;
private File file;
public ClientSocket_upload(String path)
{
this.path = path;
}
public void uploadfile(){
try {
//1.创建客户端Socket,指定服务器地址和端口
socket= new Socket("10.104.77.108", 1080);
//2.获取输出流,向服务器端发送信息
os = socket.getOutputStream();//字节输出流
bos = new BufferedOutputStream(os);
file = new File(path);
fis= new FileInputStream(file);
String name = file.getName();
byte[] temp = name.getBytes();
byte[] bname = new byte[1024];
byte[] by = new byte[1024];
for(int i = 0 ;i<=bname.length-1;i++)
{
if(i<=temp.length-1)
{
bname[i] = temp[i];
}
else
{
bname[i]=0;
}
}
bos.write(bname);

        while((fis.read(by))!=-1)
        {
            bos.write(by);
        }

        fis.close();
        bos.close();
        os.close();
        socket.close();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

  • 写回答

3条回答 默认 最新

  • 毕小宝 博客专家认证 2016-01-28 14:40
    关注

    你的代码没有问题,我测试了你的代码,传输的文件是几k的小文件没有问题,又传输一个300MB的大文件时也没有问题。
    我的测试代码是:自定义一个Server类.
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class Server {
    public static void main(String[] args) {
    try {
    ServerSocket server = new ServerSocket(1080);
    while(true){
    Socket socket = server.accept();
    new Server2User(socket).start();
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    客户端的main方法是:

        public static void main(String[] args) {
            ClientSocket_upload u = new ClientSocket_upload("D:/WoodWang/JavaSoft/eclipse-jee-mars-R-win32-x86_64.zip");
            u.uploadfile();
        }
    

    不知道你的测试代码是怎么写的呢?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
  • ¥15 SQL Server analysis services 服务启动失败
  • ¥15 用html创建一个个人网页,提供网页页面
  • ¥20 java项目连接SqlServer数据库报错
  • ¥15 基于面向对象的图书馆借阅管理系统
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥20 centos linux 7.9安装php8.2.18不支持mysqli模块的问题
  • ¥15 stata空间计量LM检验
  • ¥15 NAO机器人说出txt文本内容
  • ¥15 关于k8s node节点被释放后如何驱逐节点并添加新节点