如何在java中使用套接字编程发送多个图像并在服务器端写入特定路径?
How to send multiple images using socket programing in java and write on specific path on server side?
我想在 wifi 中的不同系统上从客户端向服务器端发送多个图像,并在服务器端将此文件写入特定位置。
提前致谢。
在Client.java文件中
public class Client {
public void send(String file_name){
try {
Socket socket = new Socket("IBM-PC", 3332);
File file = new File(file_name);
System.out.println(file_name);
ObjectInputStream ois = new ObjectInputStream(
socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(
socket.getOutputStream());
oos.writeObject(file.getName());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[Server.BUFFER_SIZE];
Integer bytesRead = 0;
while ((bytesRead = fis.read(buffer)) > 0) {
oos.writeObject(bytesRead);
oos.writeObject(Arrays.copyOf(buffer, buffer.length));
}
ois = null;
oos = null;
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Client c = new Client();
c.send("D://1.jpg"); // first image path
c.send("D://0.jpg"); // second image path
}
}
在Server.Java文件中
public class Server extends Thread {
public static final int PORT = 3332;
public static final int BUFFER_SIZE = 500102;
@Override
public void run() {
try {
ServerSocket 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();
if (o instanceof String) {
fos = new FileOutputStream("D:\temp\"+o.toString()); // Edit it for specific path
System.out.println("D:\temp\"+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();
}
}
First run server.java file and then run client.java file
希望对你有所帮助...
我想在 wifi 中的不同系统上从客户端向服务器端发送多个图像,并在服务器端将此文件写入特定位置。
提前致谢。
在Client.java文件中
public class Client {
public void send(String file_name){
try {
Socket socket = new Socket("IBM-PC", 3332);
File file = new File(file_name);
System.out.println(file_name);
ObjectInputStream ois = new ObjectInputStream(
socket.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(
socket.getOutputStream());
oos.writeObject(file.getName());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[Server.BUFFER_SIZE];
Integer bytesRead = 0;
while ((bytesRead = fis.read(buffer)) > 0) {
oos.writeObject(bytesRead);
oos.writeObject(Arrays.copyOf(buffer, buffer.length));
}
ois = null;
oos = null;
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Client c = new Client();
c.send("D://1.jpg"); // first image path
c.send("D://0.jpg"); // second image path
}
}
在Server.Java文件中
public class Server extends Thread {
public static final int PORT = 3332;
public static final int BUFFER_SIZE = 500102;
@Override
public void run() {
try {
ServerSocket 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();
if (o instanceof String) {
fos = new FileOutputStream("D:\temp\"+o.toString()); // Edit it for specific path
System.out.println("D:\temp\"+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();
}
}
First run server.java file and then run client.java file
希望对你有所帮助...