Reputation: 17
i have this code that sends an text file from client and put it in server side how can I modify this code make it send an image file of type "png" instead of sending an text file .. how can i use bytes for this job
in the code it's copy the file content and put it into a string and then he wrote it again into a new string in the sever side the client file have more than one file and you will chose the text that you want to copy .
this is the server code
public static void main(String[] args){
try{
ServerSocket serverSocket = new ServerSocket(2012);
System.out.print("Running");
Socket socket= serverSocket.accept();
System.out.println("Client connected");
PrintWriter pw=new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
String dir = "C:\\Users\\alaqra senpai\\Desktop\\FTP";
File f=new File(dir);
File fl[]=f.listFiles();
Arrays.sort(fl);
int c=0;
for(int i=0;i<fl.length;i++){
if(fl[i].canRead() && (fl[i].toString().endsWith(".txt"))){
c++;
}
}
pw.println(" "+c+".txt file founde , listed A to z");
for (int i=0;i<fl.length;i++){
if((fl[i].toString().endsWith(".txt"))){
pw.println(" "+(i) + " "+ fl[i].getName() + " " +fl[i].length()+"byte");
}
}
pw.println("_");
pw.flush();
String tem=br.readLine();
int temp=Integer.parseInt(tem);
temp-=48;
System.out.println("index " +temp);
boolean files=false;
int index=0;
if(temp>=0 && temp<=fl.length){
files=true;
index=temp;
}else{
files=false;
}
if(files){
try{
File ff=new File(fl[index].getAbsolutePath());
FileReader fr=new FileReader(ff);
BufferedReader brf=new BufferedReader(fr);
String s;
while((s=brf.readLine())!=null){
pw.println(s);
}
pw.flush();
if(brf.readLine()!=null){
System.out.println("File read succeful,closing socket");
}
}catch(IOException e ){
System.err.println("error"+e);
}
}
br.close();
socket.close();
}catch(IOException e){
}
}
and this is the client code
public static void main(String[] args){
try {
//declaration and initialization client socket
Socket socket = new Socket(InetAddress.getByName("Localhost"), 2012);
//read and write on socket
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//read from console
BufferedReader bu = new BufferedReader(new InputStreamReader(System.in));
String s;
while ((br.read()) != '_') {
System.out.println(br.readLine());
}
System.out.println("enter file index no: ");
out.println(bu.read());
//force write buffer
out.flush();
//file receive process
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Recevie.png")));
while ((s = br.readLine()) != null) {
bw.write(s);
}
//force write buffer to server
bw.close();
if (br.readLine() == null) {
System.out.println("File Write Successful,Socket closing");
}
} catch (Exception e) {
System.out.println("Error" + e);
}
} catch (IOException ex) {
Logger.getLogger(client.class.getName()).log(Level.SEVERE, null, ex);
}
}
Upvotes: 1
Views: 334
Reputation: 781
Use FileInputStream and BufferedInputStream to as image is binary file. Check more on using streams here - http://tutorials.jenkov.com/java-io/index.html, nicely explained java io.
Upvotes: 1