Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我应该使用哪个流通过java网络传输自定义文件(如jpeg或mp3)?_Java_Sockets_File Upload - Fatal编程技术网

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

我应该使用哪个流通过java网络传输自定义文件(如jpeg或mp3)?,java,sockets,file-upload,Java,Sockets,File Upload,我试图用java编写一个服务器和一个客户机来传输文件。但是接收到的文件很奇怪,看起来像是充满了奇怪的字节,它不会用任何应用程序打开,但是它的大小与源文件完全相同。 我不知道问题出在哪里!是关于编码吗 服务器端: import java.net.*; import java.io.*; public class MyServer { public static void main(String[] args) { char sp = File.separatorChar;

我试图用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);
        }


}

}
import java.net.*;
导入java.io.*;
公共类客户端{
公共静态void main(字符串[]args){
字节[]IP={(字节)192,(字节)168,1,7};
char sp=File.separatorChar;
字符串home=System.getProperty(“user.home”);
String SharingPathString=home+sp+“文件共享”;
字符串文件名=共享路径字符串+sp+“file.jpg”;
试一试{
InetAddress add=InetAddress.getByAddress(IP);
插座插座=新插座(“Shayan-8”,5776);
DataInputStream in=新的DataInputStream(theSocket.getInputStream());
final long FileLength=in.readLong();
System.out.println(“FileLength:+FileLength”);
文件共享路径=新文件(共享路径字符串);
如果(!SharingPath.exists())
SharingPath.mkdir();
文件输出文件=新文件(文件名);
if(outputFile.exists())
outputFile.delete();
//outputFile.createNewFile();
FileOutputStream Fos=新的FileOutputStream(文件名);
DataOutputStream dos=新的DataOutputStream(Fos);
字节[]缓冲区=新字节[100];
整数计数=0;
长电流=0;
而(当前
您还没有读入
bt[]
中的任何内容。因此,您正在写入正确的空字节数。

哦!是的,你说得对!我怎么忘了?!!真的非常感谢,它成功了:D@Sathish我不必测试你的代码就知道它错了。我读过它;我读过Javadoc;这个问题我已经回答了一百万次了。很明显,您还没有测试它是否有效。当你做了,回来,我会告诉你为什么,以及如何修复它。