将.txt文件从客户端传输到server.getting eror FilenotFoundException

时间:2016-02-18 10:47:27

标签: java

Hello geniuses我试图将一个(.txt)文件从客户端发送到服务器并在下面收到此错误我已经推出了所有必需的文件。我尝试了所有类似.replaceAll(“//”,“\”)和还有更多,如果你们中任何人都明白这个问题请帮我做一下.java.io.FileNotFoundException:C:\ temp2Client.txt(访问被拒绝)我无法理解我正在给C:/temp/Client.txt文件和错误(C:\ temp2Client.txt)。这里我把我的代码请帮助感谢advanced.i从这里读取 http://www.coderpanda.com/java-socket-programming-file-transfer-through-socket-in-java/                 at java.io.FileOutputStream.open(Native Method)                 在java.io.FileOutputStream。(未知来源)                 在java.io.FileOutputStream。(未知来源)                 在Server.downloadFile(Server.java:48)                 在Server.main(Server.java:68)

        FileEvent.java

        import java.io.Serializable;
        public class FileEvent implements Serializable {
        public FileEvent(){}
        private static final long serialVersionUID = 1L;
        private String destinationDirectory;
         private String sourceDirectory;
            private String filename;
            private long fileSize;
            private byte[] fileData;
            private String status;
            public String getDestinationDirectory() {
            return destinationDirectory;
            }
            public void setDestinationDirectory(String destinationDirectory) {
                this.destinationDirectory = destinationDirectory;
                }
                public String getSourceDirectory() {
                return sourceDirectory;
            }

            public void setSourceDirectory(String sourceDirectory) {
                this.sourceDirectory = sourceDirectory;
            }

            public String getFilename() {
                return filename;
            }

            public void setFilename(String filename) {
                this.filename = filename;
            }

            public long getFileSize() {
                return fileSize;
            }

            public void setFileSize(long fileSize) {
                this.fileSize = fileSize;
            }

            public String getStatus() {
                return status;
        }    public void setStatus(String status) {
                this.status = status;
        }

            public byte[] getFileData() {
                return fileData;
        }

            public void setFileData(byte[] fileData) {
                this.fileData = fileData;
        }

        }


        Server.java

        import java.io.File;
        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.io.ObjectInputStream;
        import java.net.ServerSocket;
        import java.net.Socket;

        public class Server {
            private ServerSocket serverSocket;
            private Socket socket ;
            private ObjectInputStream inputStream ;
            private FileEvent fileEvent;
            private File dstFile ;
            private FileOutputStream fileOutputStream ;

            public Server() {

            }

            /**
             * Accepts socket connection
             */
            public void doConnect() {
                try {
                    serverSocket = new ServerSocket(1222);
                    socket = serverSocket.accept();
                    inputStream = new ObjectInputStream(socket.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            /**
             * Reading the FileEvent object and copying the file to disk.
             */
            public void downloadFile() {
                try {
                    fileEvent = (FileEvent) inputStream.readObject();
                    if (fileEvent.getStatus().equalsIgnoreCase("Error")) {
                        System.out.println("Error occurred ..So exiting");
                        System.exit(0);
                    }
                    String outputFile = fileEvent.getDestinationDirectory() + fileEvent.getFilename();
                    if (!new File(fileEvent.getDestinationDirectory()).exists()) {
                        new File(fileEvent.getDestinationDirectory()).mkdirs();
                    }
                    dstFile = new File(outputFile);
                    fileOutputStream = new FileOutputStream(dstFile);
                    fileOutputStream.write(fileEvent.getFileData());
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    System.out.println("Output file : " + outputFile + " is successfully saved ");
                    Thread.sleep(3000);
                    System.exit(0);

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            public static void main(String[] args) {
                  Server server = new Server();
                server.doConnect();
                server.downloadFile();
            }
        }


        Client.java

        import java.io.*;
        import java.net.Socket;
        import java.nio.file.Paths;

        public class Client {
            private Socket socket;
            private ObjectOutputStream outputStream;
            private boolean isConnected = false;
            private String sourceFilePath="C:/temp/Client.txt";


            private FileEvent fileEvent ;
            private String destinationPath ="C:/temp2";

            public Client() {


            }

            /**
             * Connect with server code running in local host or in any other host
             */
            public void connect() {
                while (!isConnected) {
                    try {
                        socket = new Socket("localHost", 1222);
                        outputStream = new ObjectOutputStream(socket.getOutputStream());
                        isConnected = true;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            /**
             * Sending FileEvent object.
             */
            public void sendFile() {
                fileEvent = new FileEvent();
                String fileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("/") + 1, sourceFilePath.length());
                String path = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/") + 1);
                fileEvent.setDestinationDirectory(destinationPath);
                fileEvent.setFilename(fileName);
                fileEvent.setSourceDirectory(sourceFilePath);
                File file = new File(sourceFilePath);
                if (file.isFile()) {
                    try {
                        DataInputStream diStream = new DataInputStream(new FileInputStream(file));
                        long len = (int) file.length();
                        byte[] fileBytes = new byte[(int) len];
                        int read = 0;
                        int numRead = 0;
                        while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read,
                                fileBytes.length - read)) >= 0) {
                            read = read + numRead;
                        }
                        fileEvent.setFileSize(len);
                        fileEvent.setFileData(fileBytes);
                        fileEvent.setStatus("Success");
                    } catch (Exception e) {
                        e.printStackTrace();
                        fileEvent.setStatus("Error");
                    }
                } else {
                    System.out.println("path specified is not pointing to a file");
                    fileEvent.setStatus("Error");
                }
                //Now writing the FileEvent object to socket
                try {
                    outputStream.writeObject(fileEvent);
                    System.out.println("Done...Going to exit");
                    Thread.sleep(3000);
                    System.exit(0);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            public static void main(String[] args) {
                Client client = new Client();
                client.connect();
                client.sendFile();
            }
        }

0 个答案:

没有答案