这是服务器端代码:
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 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)
您正在从错误的路径读取文件
错误告诉你
没有这样的文件或目录
在这条路上
/storage/emulated/0/123.jpg
因此,只需将您的阅读路径更改为正确的路径即可。