java 简单的Java文件传输程序问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6101916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple Java file transfer program problem
提问by dan
I am trying to do a simple file transfer from server to client.
我正在尝试从服务器到客户端进行简单的文件传输。
It needs to go like this:
它需要像这样:
Client asks for file.
Server sends the file to client.
客户要求提供文件。
服务器将文件发送给客户端。
Now in the code (down below) (this is the only code I found and it was hard to find) It sends me only a text file "good" (and even that only makes it to one line in the client). If i try to send any other file type (like image or rar file) it gets it corrupted.
现在在代码中(在下面)(这是我找到的唯一代码,很难找到)它只向我发送了一个“好”的文本文件(即使它只在客户端中出现一行)。如果我尝试发送任何其他文件类型(如图像或 rar 文件),它会损坏。
So, could some one please help me to find some working code (in Java) that can send and receive all types of files, or explain to me what the problem with this code is.
所以,有人可以帮我找到一些可以发送和接收所有类型文件的工作代码(Java),或者向我解释这段代码的问题是什么。
Server side:
服务器端:
public class FileServer {
public static void main(String args[])throws IOException
{
ServerSocket ss=null;
try
{
ss=new ServerSocket(8081);
}
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();
System.out.println("The requested file is : "+s);
File f=new File(s);
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();
}
}
}
Client Side:
客户端:
class MyClient {
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",8081);
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("c:\output");
FileOutputStream fs=new FileOutputStream(f1);
while((u=get.readLine())!=null)
{
byte jj[]=u.getBytes();
fs.write(jj);
}
fs.close();
System.out.println("File received");
s.close();
}
}
yesssssssssssssssssssssssssssssssssssssssssssssssssss at last...
是的ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
This is the changes
这是变化
client side.
客户端。
class MyClient {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",8081);
get=new BufferedReader(new InputStreamReader(s.getInputStream()));
put=new PrintWriter(s.getOutputStream(),true);
}
catch(Exception e)
{
System.exit(0);
}
InputStreamReader get2=new InputStreamReader(s.getInputStream());
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("c:\output");
FileOutputStream fs=new FileOutputStream(f1);
BufferedInputStream d=new BufferedInputStream(s.getInputStream());
BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(f1));
byte buffer[] = new byte[1024];
int read;
while((read = d.read(buffer))!=-1)
{
outStream.write(buffer, 0, read);
outStream.flush();
}
//while((u=get.readLine())!=null)
// {
// byte jj[]=u.getBytes();
// fs.write(jj);
//}
fs.close();
System.out.println("File received");
s.close();
}
}
Server Side.
服务器端。
public class FileServer {
public static void main(String args[])throws IOException
{
ServerSocket ss=null;
try
{
ss=new ServerSocket(8081);
}
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();
System.out.println("The requested file is : "+s);
File f=new File(s);
if(f.exists())
{
BufferedInputStream d=new BufferedInputStream(new FileInputStream(s));
BufferedOutputStream outStream = new BufferedOutputStream(cs.getOutputStream());
byte buffer[] = new byte[1024];
int read;
while((read = d.read(buffer))!=-1)
{
outStream.write(buffer, 0, read);
outStream.flush();
}
d.close();
System.out.println("File transfered");
cs.close();
ss.close();
}
}
}
Thanks a lot to all of you...
非常感谢你们所有人...
回答by Mikita Belahlazau
You shouldn't use reader in this case. Reader are supposed to deal when you read/writer character data (text) not binary. Use some kind of InputStream
在这种情况下,您不应该使用阅读器。当您读取/写入字符数据(文本)而不是二进制时,Reader 应该处理。使用某种 InputStream
http://download.oracle.com/javase/tutorial/i18n/text/stream.html
http://download.oracle.com/javase/tutorial/i18n/text/stream.html
Update:In you server part after f.exist():
更新:在 f.exist() 之后的服务器部分:
BufferedInputStream d=new BufferedInputStream(new FileInputStream(s));
BufferedOutputStream outStream = new BufferedOutputStream(cs.getOutputStream());
byte buffer[] = new byte[1024];
int read;
while((read = d.read(buffer))!=-1)
{
outStream.write(buffer, 0, read);
outStream.flush();
}
d.close();
System.out.println("File transfered");
cs.close();
ss.close();
回答by Kien Truong
Reader/Writer class is for character stream only, Reader doc. You should use BufferedInputStream/BufferedOutputStream or something like that for raw/binary data.
Reader/Writer 类仅用于字符流,Reader doc。您应该对原始/二进制数据使用 BufferedInputStream/BufferedOutputStream 或类似的东西。
回答by paradoxbomb
Yes but InpustStreamReader is still a Reader, which will try to convert the underlying bytes to characterdata not binarydata. Since you're just trying to send the raw bytes of a file, don't using anything with "reader" or "writer" in the name - use InputStream and OutputStream (and their subclasses) instead on both the server and client.
是的,但 InpustStreamReader 仍然是一个 Reader,它将尝试将底层字节转换为字符数据而不是二进制数据。由于您只是想发送文件的原始字节,因此不要在名称中使用任何带有“reader”或“writer”的东西——在服务器和客户端上使用 InputStream 和 OutputStream(及其子类)。