Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6967349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:18:34+00:00 2026-05-27T16:18:34+00:00

i have a simple http client server. here client can download from the server.

  • 0

i have a simple http client server. here client can download from the server. From my code client can download the file. That’s basically means a file from server folder is transferred to the client successfully. What i want to do is to also show the downloaded file in the console. My question is how can i modify my code that is also shows the received file out the console.

My server:

public class Fileagent extends Thread
      {
            Socket client;

            DataInputStream din;
            DataOutputStream dout;
            ServerSocket soc;
            PrintWriter w;
            BufferedReader r;
 public Fileagent(Socket soc)
      {
               try
        {
                 client=soc;      
                 din=new DataInputStream(client.getInputStream());
                 dout=new DataOutputStream(client.getOutputStream());
                 w = new PrintWriter(client.getOutputStream(), true);
                 r= new BufferedReader(new InputStreamReader(client.getInputStream()));
                 BufferedReader con = new BufferedReader(new InputStreamReader(System.in));

          start();

         } ..................





            public void upload() throws Exception
         {  


           String file=din.readUTF();
           File f=new File(file);
           System.out.println("\nThe client requested to download the file"+" "+f);
           boolean exsists=check(f);

          if (exsists==false){
                System.out.println("File not found in server");
                out.writeUTF("ERROR");//if not found sends an ERROR message
             return;
            }
         else
            {
             dout.writeUTF("FOUND");//if found in server sends a FOUND message.

           FileInputStream fin=new FileInputStream(f);
          int ch;
            //reads and sends the file
         do
         {
          ch=fin.read();
           dout.writeUTF(String.valueOf(ch));
        }
        while(ch!=-1); 
        fin.close(); 
          dout.writeUTF("File Receive Successfully"); 
        }
         }


        public Boolean check(File file){

        if(file.exists())
        {
           return true;
         }
             else
       {

        return false;
        }
       }

The client code to download:

                  public void download(Socket s) throws Exception{


         DataInputStream din=new DataInputStream(s.getInputStream());
          DataOutputStream dout=new DataOutputStream(s.getOutputStream());
         BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
          BufferedReader con = new BufferedReader(new InputStreamReader(System.in));
          PrintWriter w = new PrintWriter(s.getOutputStream(), true);
          String request;
            System.out.print("Enter File Name :");



        request=con.readLine();
        dout.writeUTF(request);
       String msg=din.readUTF();


        if(msg.compareTo("ERROR")==0)
       {
          System.out.println("File not found on Server ...");
        return;
        }
       else if(msg.compareTo("FOUND")==0)    
  {
        System.out.println("Receiving File ...");
        File f=new File(request);
        String option;
        boolean exsists=check(f);
        if(exsists==true)
      {
        String Option;
          System.out.println("File Already Exists. Want to OverWrite (Y/N) ?");

         Option=con.readLine();   
           if(Option=="N") 
            {
         dout.flush();
           return; 
           }

         }


         FileOutputStream fileout=new FileOutputStream(f);
         int ch;
         String temp;
    do
   {
         temp=din.readUTF();
         ch=Integer.parseInt(temp);
         if(ch!=-1)
  {
    fileout.write(ch);     
   }
  }while(ch!=-1);
  fileout.close();
  System.out.println(din.readUTF());
   System.out.println("success");
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:18:35+00:00Added an answer on May 27, 2026 at 4:18 pm

    In your do while loop of client code, add System.out.write():

    do {
        temp=din.readUTF();
        ch=Integer.parseInt(temp);
    
        if (ch!=-1) {
            fileout.write(ch);     
            System.out.write(ch);
        }
    } while(ch!=-1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple web site ( http://www.kousenit.com/twitterfollowervalue ) that computes a quantity based
I have a simple test application (C# console application) that does an HTTP GET
I have built a simple menu in jQuery http://vanquish.websitewelcome.com/~toberua/ Here is a sample of
I have some content sliding here. http://www.smallsharptools.com/downloads/jQuery/Slider/slider.html The HTML structure is simple. There is
I have a simple server that looks something like this: void *run_thread(void *arg) {
I have a client that I'm wanting to move to a virtual private server.
Here is my requirement. I have a client that was sending a specific message
I have an Asp .net page like this simple one http://issamsoft.com/app2/page1.aspx and I want
I have a fairly simple addition to the HTTP standard. An ambitious goal I
I have a very simple bit of jQuery to retrieve my latest Tweet $.getJSON(http://twitter.com/statuses/user_timeline/username.json?count=1,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.