我试图将多个图像从一个目录发送到服务器,但是在4个图像之后,循环结束并且服务器也因异常而停止。 “访问被拒绝”。 (即使我没有将文件写入服务器上的磁盘)。我当然在代码中做错了,如果有人帮助我解决此问题,将不胜感激。
即使计数为10,我的循环也只能运行4/10次。
这是我现在的程序流程:
客户:发送问候 服务器:您好 客户:发送图片数量(n) 服务器:读取图片数量并循环n次 -循环开始 客户:发送图片名称 服务器:读取映像名称 客户:发送图片 服务器:读取图像,以先前传递的名称保存 -循环结束
我的代码:
SERVER
package client;
package network;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
public class server {
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
public server() {
try {
// File outputfile = new File("C:\\test\\");
serverSocket = new ServerSocket(6002);
System.out.println("Server is Waiting for request...");
socket = serverSocket.accept();
System.out.println("Connected with: " + socket.getInetAddress());
dataInputStream = new DataInputStream(socket.getInputStream());
System.out.println("Server Read from client: " + dataInputStream.readUTF());
dataInputStream = new DataInputStream(socket.getInputStream());
int count = dataInputStream.readInt();
System.out.println("Num Files: " + count);
for (int x = 0; x <= count; x++) {
System.out.println("File: " + count);
System.out.println("Num: " + x);
BufferedImage out = ImageIO.read(socket.getInputStream());
System.out.println("Server: Image received from client.");
String filename = dataInputStream.readUTF();
System.out.println("Filename: " + filename);
ImageIO.write(out, "bmp", new File("C:\\test\\output\\" + filename));
System.out.println("Server: Image write successful.");
}
} catch (IOException e) {
}
}
public static void main(String[] args) {
server server = new server();
}
}
Here is my CLIENT CODE
package network;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
public class client {
Socket clientSocket = null;
OutputStream outputStream = null;
DataOutputStream dataOutputStream = null;
InputStream inputStream = null;
public client() {
try {
clientSocket = new Socket("127.0.0.1", 6002);
outputStream = clientSocket.getOutputStream();
dataOutputStream = new DataOutputStream(outputStream);
inputStream = clientSocket.getInputStream();
File path = null;
dataOutputStream.writeUTF("Hello");
System.out.println("Client to server says: Hello");
File folder = new File("C:\\test\\");
File[] listOfFiles = folder.listFiles();
int count = listOfFiles.length;
dataOutputStream.writeInt(count);
//System.out.println(count);
for (File file : listOfFiles) {
if (file.isFile()) {
System.out.println(file.getName());
String filename = file.getName();
//count = listOfFiles.length;
System.out.println(count);
System.out.println("Client: Reading file");
BufferedImage image = ImageIO.read(file);
System.out.println("Client: Read file");
ImageIO.write(image, "bmp", clientSocket.getOutputStream());
dataOutputStream.writeUTF(filename);
System.out.println("Client: Image sent to server");
}
}
} catch (IOException e) {
}
}
public static void main(String[] args) {
client client = new client();
}
}
Error Log
///client log:///
run:
Client to server says: Hello
canny_plane_1_8.bmp
10
Client: Reading file
Client: Read file
Client: Image sent to server
canny_plane_2_8.bmp
10
Client: Reading file
Client: Read file
Client: Image sent to server
canny_plane_3_8.bmp
10
Client: Reading file
Client: Read file
Client: Image sent to server
canny_plane_4_8.bmp
10
Client: Reading file
Client: Read file
Client: Image sent to server
canny_plane_5_8.bmp
10
Client: Reading file
Client: Read file
Client: Image sent to server
canny_plane_6_8.bmp
10
Client: Reading file
Client: Read file
Client: Image sent to server
canny_plane_7_8.bmp
10
Client: Reading file
Client: Read file
Client: Image sent to server
canny_plane_8_8.bmp
10
Client: Reading file
Client: Read file
Client: Image sent to server
canny_plane_9_8.bmp
10
Client: Reading file
Client: Read file
BUILD SUCCESSFUL (total time: 0 seconds)
///server log ///
run:
Server is Waiting for request...
Connected with: /127.0.0.1
Server Read from client: Hello
Num Files: 10
File: 10
Num: 0
Server: Image received from client.
Filename: canny_plane_1_8.bmp
Server: Image write successful.
File: 10
Num: 1
Server: Image received from client.
Filename: canny_plane_2_8.bmp
Server: Image write successful.
File: 10
Num: 2
Server: Image received from client.
Filename: canny_plane_3_8.bmp
Server: Image write successful.
File: 10
Num: 3
Server: Image received from client.
Filename: canny_plane_4_8.bmp
Server: Image write successful.
File: 10
Num: 4
Server: Image received from client.
Filename:
java.io.FileNotFoundException: C:\test\output (Access is denied)
at java.io.RandomAccessFile.open0(Native Method)
at java.io.RandomAccessFile.open(RandomAccessFile.java:316)
at java.io.RandomAccessFile.<init>(RandomAccessFile.java:243)
at javax.imageio.stream.FileImageOutputStream.<init>(FileImageOutputStream.java:69)
at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(FileImageOutputStreamSpi.java:55)
at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:419)
at javax.imageio.ImageIO.write(ImageIO.java:1530)
at client.server.<init>(server.java:43)
at client.server.main(server.java:52)
Exception in thread "main" java.lang.NullPointerException
at javax.imageio.ImageIO.write(ImageIO.java:1538)
at client.server.<init>(server.java:43)
at client.server.main(server.java:52)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 5 seconds)