我有一个客户端应该从服务器下载一个用AES加密的图像文件(我不确定我是否正确实现了这一点)。但是,使用以下代码时,服务器会卡在“发送图像”打印消息上,而不会转到“完成”打印消息。换句话说,我无法下载图像文件。为什么会这样?
Server.java
public class Server {
public static void main (String [] args ) throws InvalidKeyException, IOException, Exception {
ServerSocket serverSocket = new ServerSocket(15123);
Socket socket = null;
//start of AES
//Generate AES Key
int sizeofkey = 128;
KeyGenerator kg = KeyGenerator.getInstance("AES");
//initialize with sizeofkey
kg.init(sizeofkey);
Key mykey = kg.generateKey();
System.out.println("AES Key Generated");
//create cipher object & initialize with generated key
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, mykey);
while(true) {
socket = serverSocket.accept();
System.out.println("Accepted connection : " + socket.getRemoteSocketAddress().toString() + " <-> /127.0.0.1:15123" );
OutputStream sos = socket.getOutputStream();
// get the image from a webcam
URL myimage = new URL("http://183.76.13.58:80/SnapshotJPEG?Resolution=640x480");
//Picture in string format
//String plainpic = null;
//Picture in byte array format
byte[] plainpic = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
//in = new DataInputStream(myimage.openStream());
is = myimage.openStream ();
byte[] byteChunk = new byte[4096];
int n;
while ( (n = is.read(byteChunk)) > 0 ) {
baos.write(byteChunk, 0, n);
}
//Picture in string format
//plainpic=Base64.getUrlEncoder().encodeToString(baos.toByteArray());
//Picture in byte array format
plainpic=baos.toByteArray();
}
catch (Exception ee) {
System.out.println("Check internet connection please");
socket.close();
return;
}
byte[] cipherpic = cipher.doFinal(plainpic);
DateFormat dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println("Sending image " + dateFormat.format(date));
try {
while (true) {
sos.write(cipherpic);
}
}
catch (EOFException ee) {
System.out.println("-------------- Done ----------");
is.close();
}
sos.flush();
sos.close();
socket.close();
}
}
}
Client.java
public class Client {
public static void main(String [] args) throws IOException {
String fname = "image.jpg";
SSLSocket sslsocket = new Socket("127.0.0.1",15123);
DataInputStream in = null;
try{ in = new DataInputStream(sslsocket.getInputStream()); }
catch (Exception ee)
{ System.out.println("Check connection please");
sslsocket.close(); return;
}
FileOutputStream fos = new FileOutputStream(fname);
try
{
while (true)
fos.write(in.readByte());
}
catch (EOFException ee)
{ System.out.println("File transfer complete");
in.close();
}
fos.flush();
fos.close();
sslsocket.close();
public static String asHex (byte buf[]) {
//Obtain a StringBuffer object
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
// Return result string in Hexadecimal format
return strbuf.toString();
}
}
答案 0 :(得分:0)
查看服务器中的内容:
try {
while (true) {
sos.write(cipherpic);
}
}
catch (EOFException ee) {
System.out.println("-------------- Done ----------");
is.close();
}
你永远不会得到EOFException,所以你永远在写cipherpic。