Question

I tried to send NSString through wifi & it works without error. For NSString I used NSUTF8StringEncodingin both iOS app & Java server side.

Now I want to send NSData(images) through wifi to my java server. I'm going to use this code to send the image to java server.

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;
        }
    }

My question is, can I read NSData in java server? If I can't what is the solution? Should I use encoding method? And also can someone give me a sample Java server code to read NSData or images somehow....

Was it helpful?

Solution

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`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top