Java服务器,iOS客户端:如何发送图像?

时间:2014-08-08 08:52:11

标签: java ios image base64 nsdata

我正在开展一个项目,我必须在iOS设备(客户端)和java服务器之间传输图像。 我只是想知道什么是最佳实践,因为我目前的方法无处可去:(

iOS客户端:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

NSInputStream *inputStream;
NSOutputStream *outputStream;

- (IBAction)sendButton:(id)sender {

    NSLog(@"Sending something....");

    [self sendMessage];

}

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *website = [NSURL URLWithString:@"http://192.168.178.34"];

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL,CFBridgingRetain([website host]),50666, &readStream, &writeStream);

    inputStream = (__bridge_transfer NSInputStream *)readStream;
    outputStream = (__bridge_transfer NSOutputStream *)writeStream;
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream open];
    [inputStream open];

    CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
    CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);

}


- (void) sendMessage {


   NSString *baseString =  [UIImagePNGRepresentation([UIImage imageNamed:@"testimage.png"]) base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

   if ([outputStream hasSpaceAvailable]) {

       NSData *data = [[NSData alloc] initWithData:[baseString dataUsingEncoding:NSASCIIStringEncoding]];
       [outputStream write:[data bytes] maxLength:[data length]];

   }

    NSLog(@"String sent: %@", baseString);

        do{
            uint8_t buffer[4096];
            long bytes = [inputStream read:buffer maxLength:sizeof(buffer)];
            NSString *output = [[NSString alloc] initWithBytes:buffer length:bytes encoding:NSUTF8StringEncoding];
            NSLog(@"%@",output);
        } while ([inputStream hasBytesAvailable]);
}

@end

Java Server:

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.charset.Charset;

import javax.imageio.ImageIO;

import sun.misc.BASE64Decoder;

class SocketConnection extends Thread {
    InputStream input;
    PrintWriter output;
    Socket socket;
    String readString;

    public SocketConnection(Socket socket) {
        super("Thread 1");
        this.socket = socket;

        System.out.println("Just connected to "
                + socket.getRemoteSocketAddress());

        try {
            input = socket.getInputStream();
            output = new PrintWriter(new OutputStreamWriter(
                    socket.getOutputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {

        try {
            byte array[] = new byte[4096];
            while (true) {
                do {

                    readString = new String(array).trim();
                    System.out.println(readString);


                } while (input.available() != 0);

                String sendString = new String("Thank you iPhone! Received!".getBytes(),
                        Charset.forName("UTF-8"));
                output.write(sendString);
                output.flush();

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        try{
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] decodedBytes = decoder.decodeBuffer(readString);

         String uploadFile = "test.png";

         BufferedImage image = ImageIO.read(new ByteArrayInputStream(decodedBytes));
         if (image == null) {
              System.out.println("Buffered Image is null");
          }
         File f = new File(uploadFile);

          ImageIO.write(image, "png", f);
        }
        catch (IOException e){
            e.printStackTrace();
            }


    }

}

0 个答案:

没有答案