我想显示客户端收到的图像并在服务器上显示该图像..但是我保留了图像部分以便稍后完成并且最初只接收输入流并打印它。
问题是它接受客户端请求但不打印decodedstring,即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);
}
}
任何帮助都将受到高度赞赏。
答案 0 :(得分:0)
你不能只打印字节数组,代码
System.out.println("Image Successfully Manipulated!" + decodedString);
打印类似
的内容Image Successfully Manipulated![B@1cd2e5f
如果要将字节数组打印为String,则应将字节数组转换为十六进制字符串,例如:
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();
}