【发布时间】:2013-02-07 18:26:15
【问题描述】:
Array byte[] 包含照片的完美逐字节副本,当我尝试将 byte[] 转换为 String 并使用它写入文件时,它失败了。
我需要转换为字符串以便稍后通过套接字发送。
我的每个连接的处理程序都有一个 Socket (sock)、PrintWriter (out) 和 BufferedReader (in),然后我将 Socket 关联到 PrintWriter 和 BufferedReader。有了这个,我用 out.println 和 in.readLine 发送和接收字符串。
我该如何解决这个问题?
测试代码:
// getPhoto() returns byte[]
String photo = new String(getPhoto());
// Create file
DataOutputStream os = new DataOutputStream(new FileOutputStream("out1.jpg"));
// This makes imperfect copy of the photo
os.writeBytes(photo);
//This works perfectly basically it copies the image through byte[]
//os.write(getPhoto());
// Close the output stream
os.close();
【问题讨论】:
-
我不明白您为什么要将表示照片的字节数组转换为字符串。请解释一下。
-
已编辑,我希望它通过套接字发送字符串,使用 println。
标签: java string file bytearray