问题
Here is a code snippet of both server side and client side through which a user can request a file from server. The server will send the file.
There's two problems:
- Server side sends empty file.
- When trying to run the code in Local area network it is giving ioexception
I don't understand why sever is sending empty file, please help.
SERVER SIDE CODE :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ftpserverclient.FileClientServer;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author Arnab
*/
public class FileServer {
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 path = 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())
{
BufferedReader d=new BufferedReader(new FileReader(f));
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 CODE :
package ftpserverclient.FileClientServer;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
* @author Arnab
*/
public class FileClient {
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);
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);
FileOutputStream fs=new FileOutputStream(new File("C:\\PictureDestination\\a.jpg"));
while((u=get.readLine())!=null)
{
System.out.println(u);
byte jj[]=u.getBytes();
fs.write(jj);
}
fs.close();
System.out.println("File received");
s.close();
}catch(Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
}
回答1:
At client side , you forgot to write put.fflush();
after put.println(f);
while sending file name to server.
回答2:
Finally i found this this thing working .
here are the changes.
changing String u,f;
to String f; int u;
and while((u=get.readLine())!=null)
{
System.out.println(u);
byte jj[]=u.getBytes();
fs.write(jj);
}
into
while((u=get.read(jj,0,1024))!=-1)
{
fs.write(jj,0,u);
}
fs.close();
but 1 problem still exists. It is not working ON LAN.
来源:https://stackoverflow.com/questions/21349796/cant-send-file-from-server-to-client-over-lan