Java 如何发现如果文件位于目录中,为什么会引发FileNotFoundException?
这是服务器端代码:Java 如何发现如果文件位于目录中,为什么会引发FileNotFoundException?,java,android,Java,Android,这是服务器端代码: 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;
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;
}
}
清单权限是必需的,所以我写了
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.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 android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.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)
W/System.err:java.io.FileNotFoundException:/storage/emulated/0/123.jpg(没有这样的文件或目录)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(FileInputStream.java:146)
位于MainActivity$MessageSender.doInBackground(MainActivity.java:167)
位于MainActivity$MessageSender.doInBackground(MainActivity.java:148)
位于android.os.AsyncTask$2.call(AsyncTask.java:305)
位于java.util.concurrent.FutureTask.run(FutureTask.java:237)
在android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
位于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)
您正在从错误路径读取文件
错误告诉你
没有这样的文件或目录
在这条路上
/storage/emulated/0/123.jpg
因此,只需将您的读取路径更改为正确的路径。打印您尝试使用的文件的整个路径。您将文件保存在
D://anmolrishte\u files//
中,并尝试从/storage/emulated/0/
访问它。使用此环境保存文件。getExternalStorageDirectory()
。如果我的回答有帮助,请将其标记为已验证以帮助他人。fos=new FileOutputStream(“D://anmolrishte_files/”+o.toString());此路径用于将我从客户端收到的文件添加到我的计算机/storage/emulated/0/123.jpg,这是我手机外部目录的路径,其中是save image“123.jpg”,我正在服务器的这条路径上向服务器发送。。。。fos=新文件输出流(“D://anmolrishte_files/”+o.toString());您要发送到服务器的图片是您以前使用应用程序拍摄的图片,还是仅仅是现有图片@gouravmanujayeah…此图片存在于我的设备或外部存储器中,我将在单击按钮后直接发送到服务器…从您的设备尝试查找您的图片,查看图片的详细信息,并复制详细信息告诉您的路径@gouravmanujain在我的图片的详细信息中,我可以看到路径是..../storage/FE17-386F/123.jpg…我在…Environment.getExternalStorageDirectory(),“/123.jpg”的位置上也使用了此路径…但它不起作用
/storage/emulated/0/123.jpg