• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Java File sysytem



i have webcam which take a picture and saving in my c-drive....that pathe name i have register in my file server....but i got filenotfoundexception





package server;

import java.net.ServerSocket;
import java.io.*;
import java.net.Socket;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author Makhdoom
 * @version 1.0
 */
public class FileServer
    extends Thread {
  // private final String fileDir = "files" + "\\";
  private static ServerSocket serv;
  private static final int PORT = 3300;

  public void run() {
    try {

      serv = new ServerSocket(PORT);
      System.out.println("Opening port....\n");
      System.out.println("Waiting for Client");
    }
    catch (IOException ex) {
      System.out.print("Unable to attach to port...!");
      System.out.println(0);

    }

    do {
      try {
        Socket link = serv.accept(); // blokerende kald

        SendFile send = new SendFile(link);
        send.start();
      }
      catch (IOException e) {
        e.printStackTrace();
        System.out.println("Unable to disconnect!");
        System.exit(1);

      }

    }

    while (true);
  }

  public static void main(String args[]) {
    FileServer fs = new FileServer();
    fs.start();

  }

}


_________________________________________
package server;

import java.io.*;
import java.net.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class SendFile extends Thread
{

  private Socket link;

  DataInputStream diStream;
  DataOutputStream doStream;

  public SendFile(Socket socket) {
    this.link = socket;
  }

  public void run() {
    do {
      try {
        diStream = new DataInputStream(new BufferedInputStream(link.
            getInputStream()));

        doStream = new DataOutputStream(new BufferedOutputStream(link.
            getOutputStream()));

        Sendfile("c:\\priyanka.jpg", doStream);
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      System.out.println("Instanciate: server stream");
    }
    while (true);
  }

  /**
   * Sendfile
   */
  private void Sendfile(String FileName, DataOutputStream doStream) throws IOException {
    FileInputStream fileIn = new FileInputStream(FileName);
    byte[] buffer = new byte[2 * 1024];
    fileIn.read(buffer);
    fileIn.close();
    System.out.println("Sending picture in proces:" + buffer);
    doStream.write(buffer);
    doStream.flush();
    System.out.println("Picture send...");

  }
}

/*

  private  ContentType(String path) {
    if (path.endsWith(".gif"))
      return "image/gif";
  }
}
 */
_______________________________________


package server;

import java.net.Socket;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class FileClient
    extends Thread {
  private static String host = "127.0.0.1";
  private static int PORT = 3300;
  private Socket socket;
  private DataOutputStream out, fileStream;
  private DataInputStream PictureReceive;

  public FileClient(String host, int PORT) {
    this.host = host;
    this.PORT = PORT;

  }

  public void run() {
    Socket link = null;

    try {
      link = new Socket(host, PORT);
      System.out.println("Host: " + host + "PORT" + PORT);

      out = new DataOutputStream(new BufferedOutputStream(link.getOutputStream()));
      PictureReceive = new DataInputStream(new BufferedInputStream(link.
          getInputStream()));

      out.writeUTF("jpg");
      String fileName = PictureReceive.readUTF();

      byte[] buffer = (byte[]) fileName.getBytes();

      File pictures = new File("c:\\priyanka.jpg");

      DataOutputStream fileStream = new DataOutputStream(new FileOutputStream(
          pictures));
      fileStream.write(buffer);
      fileStream.close();

    }

    catch (IOException e) {
      System.out.println("Unable to connect!");
    }

  }

  public static void main(String args[])
  {

    FileClient client = new FileClient(host, PORT);
    client.run();
  }
  }








0
Maggieshah
Asked:
Maggieshah
  • 6
  • 5
  • 2
  • +1
2 Solutions
 
colr__Commented:
Before saving the file, use

theFile.exists(); to see if it exists or not.

If it doesnt, use

theFile.createNewFile();

to create it - you can then save your content into it.

colr__
0
 
hoomanvCommented:
first of all, priyanka.jpg should be exist in your C dirve
this will eliminate the FileNotFoundException
0
 
MaggieshahAuthor Commented:
the priynka.jpg eixsting in c file
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
colr__Commented:
Obviously if the file exists your not going to get FileNotFoundException, sicne the file exists!

colr__
0
 
MaggieshahAuthor Commented:
im still getting the exception
0
 
colr__Commented:
Insert my code posted above into your application and test it - this will definetaly solve the problem.

If the file already exists and you are unable to find it, then you MUST have your path set incrorrectly. Inserting the code ablove will guerentee that your program will save to the file - if it cant find it, it creatres it.

colr__
0
 
hoomanvCommented:
is that exactly priyanka.jpg or somthing else ?
0
 
hoomanvCommented:
> if it cant find it, it creatres it.
why create it. a FileServer serves some files, and if the file does not exist, it should return some error codes, not creating the file on the fly
0
 
colr__Commented:
Are you 100% sure that the file priyanka.jpg exists on your top level C drive?

Try changing the drive letter from lower case to upper case - I dont think this will affect it, but you never know.

colr__
0
 
colr__Commented:
>> why create it
Becuase if it doesnt exist, how can you possibly write to it?

Your right , the system should return error codes if the file doesnt exist -thats what the system is doing and the prblem we are trying to resolve. It is not possible to write to a file that doesnt exists, so you will either have to create the file then write, or make sure that you have your path correctly set.

colr__
0
 
hoomanvCommented:
> how can you possibly write to it?
he doesn't want to write to it. the exception is thrown in sendfile method
0
 
colr__Commented:
Gutted ; )

I still think the path is wrong though.

colr__
0
 
hoomanvCommented:
100% sure C:\priyanka.jpg does not exist. however this is the first problem you are encounterring
after that you'll soon get a lot of Exceptions

because the program logic is doing wrong
in the sendfile method you are sending the file in an infinite loop
but the FileClinet recieves only once and then it should close the the socket (you did not close it)
and this will cause a SocketException
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Well, all file-streams, sockets, etc should be closed in finally blocks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now