gpt4 book ai didi

java - 通过 TCP 连接传输图像时出现问题

转载 作者:可可西里 更新时间:2023-11-01 02:45:34 24 4
gpt4 key购买 nike

<分区>

我在客户端服务器图像传输时出现奇怪的行为。目前服务器总是在从缓冲区中读取 32768 字节时停止,总文件大小为 36556 字节。 while 循环没有结束,但它不打印其中的任何 print 语句,也没有抛出异常。我已经尝试了几种不同大小的字节缓冲区,甚至比图像大小更大也不能解决问题。

客户端代码:

private static void writePhoto(Socket socket) throws IOException {

OutputStream outputStream = new BufferedOutputStream(socket.getOutputStream());

String file_path = "E:\\Eclipse\\Workspace\\DNI_PsuedoClient\\" +
"src\\main\\resources\\BuckyBadger.jpg";
try {
InputStream stream = new FileInputStream(file_path);
System.out.println(stream.available());
try {
byte[] buffer = new byte[1024];
int readData;
int i = 1;
while((readData=stream.read(buffer))!=-1){
System.out.println(readData + " " + i);
outputStream.write(buffer,0,readData);
i++;
}
System.out.println("done writing to buffer");
}catch (IOException e) {
System.err.println("File Error");
e.printStackTrace();
System.exit(-1);
}finally {
stream.close();
}
} finally {
}
}

服务器代码

private static java.io.File createFile(Socket client) throws IOException {

System.out.println("Starting to create file");
InputStream stream = new BufferedInputStream(client.getInputStream());

System.out.println("1");
// Create file from the inputStream
java.io.File Recieved_File = new java.io.File(thread_ID + ".jpg");
System.out.println(thread_ID + ".jpg");
System.out.println("2");

try {
OutputStream outputStream = new FileOutputStream(Recieved_File);
System.out.println("3");

try {
byte[] buffer = new byte[1024];
System.out.println("4");
int readData;
int i = 1;

while ((readData = stream.read(buffer)) != -1) {
System.out.println("start");
outputStream.write(buffer, 0, readData);
System.out.println(readData + " " + i + " " + (i * readData));
i++;
System.out.println("end while loop");
}
} finally {
System.out.println("5");
outputStream.close();
System.out.println("6");
}
} finally {
}
System.out.println("Created file");
return Recieved_File;
}

如您所见,我已多次尝试在服务器代码中使用打印语句来找出问题所在。任何见解都会非常有帮助。

为了进一步引用,我有完全相同的服务器代码用于与 Windows 手机进行交互,手机拍摄照片并将其上传到服务器。我现在正在更改代码以在多个线程上运行,并首先使用模拟交互的 Java 客户端在 Java 中对其进行测试。 java 客户端正在尝试将存储在驱动器上的照片发送到服务器。客户端将整张照片放入缓冲区时似乎工作正常。

我写了一个协议(protocol),这里是:

public DNI_Protocol(Socket socket, String threadID) {
client = socket;
thread_ID = threadID;
}

public String processInput(String theInput) {

String theOutput = null;
if (state == WAITING) {
System.out.println(theInput);
if (theInput.equals("Upload")) {
state = UPLOAD_PHOTO;
theOutput = "Send Photo";
} else if (theInput == "View") {
state = VIEW;
theOutput = "Send Request";
} else {
theOutput = "Waiting";
}
} else if (state == UPLOAD_PHOTO) {
// if (theInput != "Sending Photo") {
// TODO: Throw a state exception with a message
// return "exit";
// }
setupDrive();
try {
Image_Handle = createFile(client);
System.out.println("Uploading file");
uploadedFile = Drive_Interface.uploadFile(false, Image_Handle, drive);
System.out.println("file Uploaded");
google_ID = uploadedFile.getId();
System.out.println(google_ID);
Image_Handle.delete(); // We are done with the file so we can delete it
System.out.println("deleted file");
} catch (IOException e) {
e.printStackTrace();
}
theOutput = "Send Keywords";
state = UPLOAD_KEYWORDS;
} else if (state == UPLOAD_KEYWORDS) {
if (theInput != "Sending Keywords") {
// TODO: Throw a state exception with a message
return "exit";
}
// TODO: Add code to get keyword arraylist and upload information to
// the database
theOutput = "exit";
}
return theOutput;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com