有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

socket我应该使用哪个流在java中通过网络传输自定义文件,如jpeg或mp3?

我试着用java编写一个服务器和一个客户机来传输文件。但是接收到的文件很奇怪,看起来充满了奇怪的字节,无法用任何应用程序打开,但其大小与源文件完全相同。 我不知道问题出在哪里!是关于编码吗

服务器端:

import java.net.*;
import java.io.*;


public class MyServer {


public static void main(String[] args) {
    char sp = File.separatorChar;
    String home = System.getProperty("user.home");
    try {
        ServerSocket server = new ServerSocket(5776);
        while (true){
            Socket connection = server.accept();
            try {
                String FileName=home+sp+"33.jpg";
                File inputFile = new File(FileName);
                FileInputStream Fin = new FileInputStream(inputFile);
                long fileLength = inputFile.length();
                byte[] bt= new byte[(int) fileLength];
                DataOutputStream Oout = new DataOutputStream(connection.getOutputStream());
                Oout.writeLong(fileLength);
                Oout.write(bt);
                Oout.flush();
                Oout.close();
                connection.close();
            } catch (IOException ex) {}
            finally {
                try {
                    if(connection!= null) connection.close();
                } catch (IOException e) {}
            }
        }
    } catch (IOException e) {

        System.err.println("There is a server on port 5776");
    }

}

}

客户端:

import java.net.*;
import java.io.*;

public class Client {


public static void main(String[] args) {
    byte[] IP={(byte)192,(byte)168,1,7};
    char sp = File.separatorChar;
    String home = System.getProperty("user.home");
    String SharingPathString = home+sp+"File Sharing";
    String FileName = SharingPathString+sp+"file.jpg";
    try {
        InetAddress add = InetAddress.getByAddress(IP);
        Socket theSocket= new Socket("Shayan-8",5776);
        DataInputStream in= new DataInputStream(theSocket.getInputStream());
        final long FileLength = in.readLong();
        System.out.println("FileLength: "+FileLength);
        File SharingPath = new File(SharingPathString);
        if(!SharingPath.exists())
            SharingPath.mkdir();
        File outputFile = new File(FileName);
        if(outputFile.exists())
            outputFile.delete();
        //outputFile.createNewFile();
        FileOutputStream Fos = new FileOutputStream(FileName);
        DataOutputStream dos = new DataOutputStream(Fos);
        byte[] buffer = new byte[100];
        int count=0;
        long current=0;
        while(current < FileLength && (count=in.read(buffer,0,(int)Math.min(FileLength-current, buffer.length)))!=-1)
            {Fos.write(buffer, 0, count);
                current+=count;
                }
//      while((count=in.read())!=-1)
//          dos.write(count);
        dos.close();
        Fos.close();
    } catch (UnknownHostException uhe) {
        System.err.println(uhe);
    } catch (IOException e) {
        System.err.println(e);
        }


}

}

共 (1) 个答案

  1. # 1 楼答案

    你还没有把任何东西读入bt[]。因此,您正在写入正确的空字节数