写了一段关于上传图片的代码,编译运行都没有任何问题,但是传过去的图片双击后显示出来的是全黑,代码如下,请求大神指点:
import java.io.*;
import java.net.*;
class PicClient
{
public static void main(String[] args)throws Exception
{
Socket s = new Socket("192.168.1.103",10007);
FileInputStream fis = new FileInputStream("d:\\1.jpg");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
s.shutdownOutput();
InputStream in = s.getInputStream();/
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
class PicServer
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(10007);//建立服务端socket服务。并监听一个端口。
Socket s = ss.accept();
InputStream in = s.getInputStream();
FileOutputStream fos = new FileOutputStream("server.jpg");
byte[] buf = new byte[1024];
int len = 0;
while((len=in.read())!=-1)
{
fos.write(buf,0,len);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
ss.close();
}
} |