FileUpload using Socket in java : <p>when i try upload file from client device to server using socket in java. it get uploaded in server but the problem is that when i tried to open it show some error.(for example:if i upload photos from client to server,it get uploaded but when i tried to open it show error "<strong>unsupported file format</strong>".</p>

<p>clientSide:</p>

<pre><code>DataOutputStream dos = new DataOutputStream(tmp.getClientSocket().getOutputStream());
    BufferedInputStream fileReader=new BufferedInputStream(new FileInputStream(f));
    byte[] buffer = new byte[1024];
    int byteRead=0;
    while((byteRead=fileReader.read(buffer))!=-1) {
        dos.write(buffer,0,byteRead);
        dos.flush();
        System.out.println("uploading "+byteRead);
    }

    fileReader.close();


    System.out.println("uploading file completed");
</code></pre>

<p>serverSide:</p>

<pre><code>String filename=this.input.readLine();
    System.out.println("file to be upload is "+filename);
    InputStream inputByte=socket.getInputStream();
    BufferedInputStream input=new BufferedInputStream(inputByte);

    String response=this.input.readLine();
    if(response.equals("start")) {

    BufferedOutputStream outputFile=new BufferedOutputStream(new FileOutputStream(projectDirPath+"\\"+filename));
    byte[] buffer =new byte[1024];
    int byteRead=0;

    while((byteRead=input.read(buffer))!=-1) {

        outputFile.write(buffer,0,byteRead);
        outputFile.flush();
        System.out.println("uploading "+byteRead);
    }
    outputFile.close();

    }else {
        System.out.println("nothing to upload");
    }
</code></pre>
