为什么我服务端生成的图片总是比原来的图片大那么多,而且打开是错误的图片
服务端
public class PicServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8888);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println("ip:" + ip + "...connect");
//输入流,源头:客户端
InputStream bis = s.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
//输出流,目的:本地文件
OutputStream bos = new FileOutputStream("f:/newPic1.png");
while((len = bis.read(bytes)) != -1){
bos.write(bytes, 0, len);
}
//输出流,目的:客户端
OutputStream bos2 = s.getOutputStream();
bos2.write("上传完毕!".getBytes());
// bos.flush();
bos.close();
s.close();
ss.close();
}
}
**********************************************************************************
客户端
public class PicClient {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Socket s = new Socket(InetAddress.getLocalHost(), 8888);
//输入流,源头:图片
FileInputStream fis = new FileInputStream("f:/qq.jpg");
//输出流,目的:服务端
OutputStream bos = s.getOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while((len = fis.read()) != -1){
bos.write(bytes, 0, len);
}
s.shutdownOutput();
//输入流,源头:服务器
InputStream bis = s.getInputStream();
byte[] b = new byte[1024];
int lenIn = bis.read(b);
System.out.println(new String(b, 0, lenIn));
fis.close();
s.close();
}
}
|
|