java - 通过wifi发送NSData到Java Server

标签 java ios iphone objective-c java-server

我尝试通过 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/

相关文章:

java - 尝试添加JLabel,但它卡在Jframe中间

ios - 在 iOS 上学习编码——swift 与 Objective C 的困境

ios - 为什么需要 .ipa 文件?

ios - 将项目转换为 ARC 后 xCode 提示

iphone - 添加自定义操作:@selector to UISwitch on TableViewCell

iPhone 与 CorePlot 的多点触控交互

iphone - 如何在 xcode 中检查 NSArray 和 NSDictionary 中的元素?

java - 关于 Java 中的树实现 - 问题

java - 在适配器中获取 Activity - Java 中的 Android studio

java - 使用 Java/Scala 从抓取的 JavaScript 文本中解码 unicode 字符表示