gpt4 book ai didi

java - 使用 java 套接字从服务器到客户端的文件传输。服务器端错误并且传输到客户端的文件为空

转载 作者:行者123 更新时间:2023-12-02 07:32:06 26 4
gpt4 key购买 nike

我有一个客户端和服务器,在客户端输入文件名,该文件名将在服务器端的预定义路径下检查,如果文件存在,它将被传输到客户端类似的预定义路径下。我有两个问题:

1)在服务器中,我无法比较给定预定义路径下的文件,因为它显示 FileNotFoundException(没有这样的文件/目录)。

2) 即使出现上述异常,文件也已传输且为空。

这是我的客户端和服务器:

客户端:

import java.io.*;
import java.net.*;
import java.util.*;
public class ft2client
{
public static void main(String srgs[])throws IOException
{
Socket s=null;
BufferedReader get=null;
PrintWriter put=null;
try
{
s=new Socket("127.0.0.1",8085);
get=new BufferedReader(new InputStreamReader(s.getInputStream()));
put=new PrintWriter(s.getOutputStream(),true);
}
catch(Exception e)
{
System.exit(0);
}
String u,f;
System.out.println("Enter the file name to transfer from server:");
DataInputStream dis=new DataInputStream(System.in);
f=dis.readLine();
put.println(f);
File f1=new File(f);
String str = "/home/user/";
FileOutputStream fs=new FileOutputStream(new File(str,f1.toString()));
while((u=get.readLine())!=null)
{
byte jj[]=u.getBytes();
fs.write(jj);
}
fs.close();
System.out.println("File received");
s.close();
}
}

服务器:

import java.io.*;
import java.net.*;
import java.util.*;
public class ft2server
{
public static void main(String args[])throws IOException
{
ServerSocket ss=null;
try
{
ss=new ServerSocket(8085);
}
catch(IOException e)
{
System.out.println("couldn't listen");
System.exit(0);
}
Socket cs=null;
try
{
cs=ss.accept();
System.out.println("Connection established"+cs);
}
catch(Exception e)
{
System.out.println("Accept failed");
System.exit(1);
}
PrintWriter put=new PrintWriter(cs.getOutputStream(),true);
BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));
String s=st.readLine();
String str = "/home/user/Desktop/";
String path = str + s;
System.out.println("The requested file is path: "+path);
System.out.println("The requested file is : "+s);
File f=new File(path);
if(f.exists())
{
BufferedReader d=new BufferedReader(new FileReader(s));
String line;
while((line=d.readLine())!=null)
{
put.write(line);
put.flush();
}
d.close();
System.out.println("File transfered");
cs.close();
ss.close();
}
}
}

最佳答案

对于二进制数据,您已经更改了读取器,因为它们只能读取字符,不能处理字节流。此外,readline 意味着读到行尾,在二进制文件中('\n')没有太大意义。
这是来自 printWriter 的文档

它不包含写入原始字节的方法,程序应使用未编码的字节流。

您现在想要的是使用字节数组并将它们写为如下所示的 block :

import java.io.*;
import java.net.*;
import java.util.*;
public class ft2server
{

public static void main(String args[])throws IOException
{
ServerSocket ss=null;
try
{
ss=new ServerSocket(8085);
}
catch(IOException e)
{
System.out.println("couldn't listen");
System.exit(0);
}
Socket cs=null;
try
{
cs=ss.accept();
System.out.println("Connection established"+cs);
}
catch(Exception e)
{
System.out.println("Accept failed");
System.exit(1);
}
BufferedOutputStream put=new BufferedOutputStream(cs.getOutputStream());
BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));
String s=st.readLine();
String str = "/home/milind/Desktop/";
String path = str + s;
System.out.println("The requested file is path: "+path);
System.out.println("The requested file is : "+s);
File f=new File(path);
if(f.isFile())
{
FileInputStream fis=new FileInputStream(f);


byte []buf=new byte[1024];
int read;
while((read=fis.read(buf,0,1024))!=-1)
{
put.write(buf,0,read);
put.flush();
}
//d.close();
System.out.println("File transfered");
cs.close();
ss.close();
}
}
}

客户端

import java.io.*;
import java.net.*;
import java.util.*;
public class ft2client
{
public static void main(String srgs[])throws IOException
{
Socket s=null;
BufferedInputStream get=null;
PrintWriter put=null;
try
{
s=new Socket("127.0.0.1",8085);
get=new BufferedInputStream(s.getInputStream());
put=new PrintWriter(s.getOutputStream(),true);

String f;
int u;
System.out.println("Enter the file name to transfer from server:");
DataInputStream dis=new DataInputStream(System.in);
f=dis.readLine();
put.println(f);
File f1=new File(f);
String str = "/home/milind/";
FileOutputStream fs=new FileOutputStream(new File(str,f1.toString()));
byte jj[]=new byte[1024];
while((u=get.read(jj,0,1024))!=-1)
{
fs.write(jj,0,u);
}
fs.close();
System.out.println("File received");
s.close();
}catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
}

关于java - 使用 java 套接字从服务器到客户端的文件传输。服务器端错误并且传输到客户端的文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12637882/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com