Pergunta

Eu tentei enviar NSString através de wi-fi e ele funciona sem erros.Para NSString eu usei NSUTF8StringEncodingna iOS app & Java do lado do servidor.

Agora eu quero enviar NSData(imagens) através de wi-fi para o meu servidor java.Eu vou usar este código para enviar a imagem para um servidor 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;
        }
    }

A minha pergunta é, eu posso ler NSData em java server?Se eu não puder, qual é a solução?Devo usar o método de codificação?E também alguém pode me dar um exemplo de servidor Java de código para ler NSData ou imagens que de alguma forma....

Foi útil?

Solução

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`
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top