1

我想显示客户端接收到的图像并在服务器上显示该图像..但我保留了图像部分稍后完成,最初只是接收输入流并打印它。

问题是它正在接受客户端请求但不打印解码的字符串,即decodedString = Base64.decodeBase64(base64Code);

这是服务器的代码

import java.net.*;
import java.io.*;

import org.apache.commons.codec.binary.Base64;
import java.awt.*;

import javax.swing.*;


public class server {
private static DataInputStream dataInputStream;


private static DataOutputStream dataOutputStream;

public static void main(String[] args) throws IOException {
// create socket
ServerSocket servsock = new ServerSocket(14789);

Socket sock = servsock.accept();
dataInputStream = new DataInputStream(sock.getInputStream());
dataOutputStream = new DataOutputStream(sock.getOutputStream());
System.out.println("Accepted connection : " + sock);


String base64Code = dataInputStream.readUTF();

byte[] decodedString = null;

decodedString =  Base64.decodeBase64(base64Code);
System.out.println("Image Successfully Manipulated!" + decodedString);

if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}



sock.close();
}


public static String encodeImage(byte[] imageByteArray) {
return Base64.encodeBase64String(imageByteArray);
}
}

任何帮助将不胜感激。

4

1 回答 1

0

你不能只打印字节数组,代码

System.out.println("Image Successfully Manipulated!" + decodedString);

打印类似的东西

Image Successfully Manipulated![B@1cd2e5f

如果要将字节数组打印为字符串,则应将字节数组转换为十六进制字符串,例如:

public static String byteArrayToHexString(byte[] b)
{
    StringBuffer sb = new StringBuffer(b.length * 2);
    for (int i = 0; i < b.length; i++)
    {
        int v = b[i] & 0xff;
        if (v < 16)
        {
            sb.append('0');
        }
        sb.append(Integer.toHexString(v));
    }
    return sb.toString().toUpperCase();
}
于 2012-12-08T21:35:47.647 回答