有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何发现如果文件位于目录中,为什么会引发FileNotFoundException?

这是服务器端代码:

import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server extends Thread {

    public static final int PORT = portno;
    public static final int BUFFER_SIZE = 100;
    ServerSocket serverSocket;

    @Override

    public void run() {
        try {
            serverSocket  = new ServerSocket(PORT);
            while (true) {
                Socket s = serverSocket.accept();
                saveFile(s);
            }
        } 

        catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void saveFile(Socket socket) throws Exception {

        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        FileOutputStream fos = null;

        byte[] buffer = new byte[BUFFER_SIZE];

        // 1. Read file name.
        Object o = ois.readObject();
        System.out.println("file name is:"+o.toString());
        if (o instanceof String) {
            fos = new FileOutputStream("D://anmolrishte_files//"+o.toString());
        } 
        else {
            throwException("Something is wrong");
        }

        // 2. Read file to the end.
        Integer bytesRead = 0;
        do {
            o = ois.readObject();
            if (!(o instanceof Integer)) {
                throwException("Something is wrong");
            }
            bytesRead = (Integer)o;
            o = ois.readObject();
            if (!(o instanceof byte[])) {
                throwException("Something is wrong");
            }
            buffer = (byte[])o;

            // 3. Write data to output file.
            fos.write(buffer, 0, bytesRead);

        } while (bytesRead == BUFFER_SIZE);

        System.out.println("File transfer success");

        fos.close();
        ois.close();
        oos.close();
    }

    public static void throwException(String message) throws Exception {
        throw new Exception(message);
    }

    public static void main(String[] args) {
        new Server().start();
    }*/

    public static final int PORT = portno;
    public static final int BUFFER_SIZE = 100;
    ServerSocket serverSocket;

    @Override
    public void run() {
        try {
            serverSocket = new ServerSocket(PORT);
            while (true) {
                Socket s = serverSocket.accept();
                saveFile(s); 
            }
        } 

        catch (Exception e) {
            e.printStackTrace();
        }
    }
   private void saveFile(Socket socket) throws Exception {

        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        FileOutputStream fos = null;

        byte [] buffer = new byte[BUFFER_SIZE];

        // 1. Read file name.
        Object o = ois.readObject();
        System.out.println("file name is:"+o.toString());
        if (o instanceof String) {    
            System.out.println("hello");
            fos = new FileOutputStream("D://anmolrishte_files//"+o.toString());
            // fos.write(b);
        } 

        else {
            throwException("Something is wrong");
        }

        // 2. Read file to the end.
        Integer bytesRead = 0;

        do {
            o = ois.readObject();

            if (!(o instanceof Integer)) {
                throwException("Something is wrong");
            }

            bytesRead = (Integer)o;
            System.out.println("hello1");
            o = ois.readObject();

            if (!(o instanceof byte[])) {
                throwException("Something is wrong");
            }

            buffer = (byte[])o;

            // 3. Write data to output file.
            fos.write(buffer, 0, bytesRead);

        } while (bytesRead == BUFFER_SIZE);

        System.out.println("File transfer success");

        fos.close();
        ois.close();
        oos.close();
    }

    public static void throwException(String message) throws Exception {
        throw new Exception(message);
    }

    public static void main(String[] args) {
        new Server().start();
    }
}  

这是客户端代码

public class MessageSender extends AsyncTask<Void,Void,Void> {

    File file;
    private static final String ipAddress = "address";
    private static final int port = portno;
    private  Socket socket;

    @Override
    protected Void doInBackground(Void... voids) {
        file = new File(Environment.getExternalStorageDirectory(), "/123.jpg");
        System.out.print(file);

        try {

            socket = new Socket(ipAddress, port);

            ObjectInputStream ois = null;
            ois = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream oos = null;
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(file.getName());
            FileInputStream fis = null;
            fis = new FileInputStream(file);

            byte[] buffer = new byte[100];
            Integer bytesRead = 0;

            while ((bytesRead = fis.read(buffer)) > 0) {

            }

            oos.writeObject(bytesRead);
            oos.writeObject(Arrays.copyOf(buffer, buffer.length));
            oos.close();
            ois.close();
        } 

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

        System.exit(0);
        return null;
    }
}

这个代码是onButtonClick,当我们点击按钮图像发送到服务器,所以我推这个代码

btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    MessageSender messageSender = new MessageSender();
    messageSender.execute();
}

清单权限是必需的,所以我写了

<uses-permission 安卓:name="安卓.permission.INTERNET"/>
<uses-permission 安卓:name="安卓.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission 安卓:name="安卓.permission.READ_EXTERNAL_STORAGE"/>

FileNotFoundException发生,但我不知道为什么。此错误在Android Studio中显示:

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/123.jpg (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at MainActivity$MessageSender.doInBackground(MainActivity.java:167)
    at MainActivity$MessageSender.doInBackground(MainActivity.java:148)
    at 安卓.os.AsyncTask$2.call(AsyncTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at 安卓.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at         java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
   04-09 11:06:01.166 4148-4204/W/System.err:     at java.lang.Thread.run(Thread.java:760)

共 (1) 个答案

  1. # 1 楼答案

    您正在从错误路径读取文件

    错误告诉你

    no such file or directory

    在这条路上

     /storage/emulated/0/123.jpg
    

    所以只要把你的阅读路径改成正确的路径就行了