我把文件转换成类发出去 那边也能收到 也有字节数 可是写个程序把消息接收过来转换成文件就成了0字节到底为什么啊

解决方案 »

  1.   

    这是我毕业设计中的关于文件传输用的代码,你可以参考参考
    import java.io.*; 
    import java.net.*;
    import java.util.zip.*;
     
    public class FileServer extends Thread{
    private String fileName;
    private int port;
    private File file;
    private FileInputStream fis;
    private ServerSocket ss;
    private Socket client;
    private OutputStream os;
    private ZipOutputStream zos;
    private DataOutputStream dos;
    private byte[] buf;

    public FileServer(String _fileName, int _port){
    fileName=_fileName;
    port=_port;
    }

    public void run(){
    try{
    file=new File(fileName);
    fis=new FileInputStream(file);
    }
    catch(FileNotFoundException e1){
    System.out.println("错误1:文件未找到!");
    }

      try{
      ss=new ServerSocket(port);
    client=ss.accept();
    os=client.getOutputStream();
    zos=new ZipOutputStream(os);
    zos.setMethod(ZipOutputStream.DEFLATED);
    zos.putNextEntry(new ZipEntry("zip"));
    dos=new DataOutputStream(zos);
    buf=new byte[2048];
    int num=fis.read(buf);
    while(num!=(-1)){
      dos.write(buf,0,num);
    dos.flush();
    num=fis.read(buf);
    }
    fis.close();
    dos.close();
    }
    catch(IOException e2){
    System.out.println("错误2:IOException!"+e2);
    }
    }

    /*测试用代码
    public static void main(String[] args)throws Exception{
    FileServer fs=new FileServer("F:\\graduation project\\思路.txt",3108);
    fs.start();            
    }
    */
    }
      

  2.   

    import java.io.*;
    import java.net.*;
    import java.util.zip.*;
     
    public class FileClient extends Thread{
    private String fileName;
    private String location;
    private String serverAddress;
    private File file;
    private RandomAccessFile raf;
    private Socket server;
    private InputStream is;
    private ZipInputStream zis;
    private DataInputStream tmpDis,dis;
    private byte[] buf;
    private int port;

    public FileClient(String _serverAddress,int _port,String _location,String _fileName){
    serverAddress=_serverAddress;
    port=_port;
    location=_location;
    fileName=_fileName;
    }

    public void run(){
    try{
    file=new File(location+"\\"+fileName);
    file.createNewFile();
    raf=new RandomAccessFile(file,"rw");
    }
    catch(FileNotFoundException e1){
    System.out.println("错误1:文件未找到!");
    }
    catch(IOException e2){
    System.out.println("错误2:IOException!");
    }

      try{
    server=new Socket(serverAddress,port);
    is=server.getInputStream();
    tmpDis=new DataInputStream(is);
    zis=new ZipInputStream(tmpDis);
    zis.getNextEntry();
                dis=new DataInputStream(zis);  
    buf=new byte[2048];
    int num=dis.read(buf);              
    while(num!=(-1)){
    raf.write(buf,0,num);
    raf.skipBytes(num);
    num=dis.read(buf);
    }
    dis.close();
    raf.close();
    }
    catch(IOException e3){
    System.out.println("错误3:IOException!"+e3);
    }
    }
    /* 测试用代码
    public static void main(String[] args)throws Exception{
    FileClient fc=new FileClient(InetAddress.getLocalHost().getHostAddress(),"F:\\graduation project\\filetrans","newfile.txt");
    fc.start();            
    }
    */
    }可以传输文件,由于还在测试,可能还有问题未找出
    贴出来,大家共同讨论
      

  3.   

    楼上的你这是socket 通信,不是mq的消息通信吧!