我尝试通过 wifi 发送 NSString & 它没有错误。对于 NSString,我在 iOS 应用程序和 Java 服务器端都使用了 NSUTF8StringEncoding
。
现在我想通过 wifi 将 NSData(images) 发送到我的 java 服务器。我将使用此代码将图像发送到 Java 服务器。
NSData *newData = UIImagePNGRepresentation([UIImage imageNamed:@"Default.png"]);
int index = 0;
int totalLen = [newData length];
uint8_t buffer[1024];
uint8_t *readBytes = (uint8_t *)[newData bytes];
while (index < totalLen) {
if ([outputStream hasSpaceAvailable]) {
int indexLen = (1024>(totalLen-index))?(totalLen-index):1024;
(void)memcpy(buffer, readBytes, indexLen);
int written = [outputStream write:buffer maxLength:indexLen];
if (written < 0) {
break;
}
index += written;
readBytes += written;
}
}
我的问题是,我可以在 java 服务器中读取 NSData 吗?如果我不能,解决方案是什么?我应该使用编码方法吗? 还有人可以给我一个示例 Java 服务器代码来以某种方式读取 NSData 或图像....
最佳答案
private void saveFile(Socket socket, File receivingFile) {
try {
FileOutputStream wr = new FileOutputStream(receivingFile);
byte[] buffer = new byte[socket.getReceiveBufferSize()];
int bytesReceived = 0;
while ((bytesReceived = socket.getInputStream().read(buffer)) > 0) {
wr.write(buffer, 0, bytesReceived);
}
wr.close();
setLogMessege("SaveFile", "File Written");
socket.close();
} catch (IOException e) {
try {
socket.close();
} catch (IOException ex) {
Logger.getLogger(AcceptCameraImages.class.getName()).log(Level.SEVERE, null, ex);
}
setLogMessege("SaveFile", e.getMessage());
}`enter code here`
关于java - 通过wifi发送NSData到Java Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22048774/