本帖最后由 smart2586 于 2013-8-28 08:46 编辑
客户端- import java.io.*;
- import java.net.*;
- class PicC
- {
- public static void main(String[] args) throws Exception
- {
- Socket s = new Socket("192.168.0.100",10070);
-
- FileInputStream fis = new FileInputStream("c:\\1.jpg");
-
- OutputStream os = s.getOutputStream();
-
- BufferedReader brIn =
- new BufferedReader(new InputStreamReader(s.getInputStream()));
- byte[] by = new byte[1024*10];
-
- int len = 0;
-
- while((len=fis.read(by))!=-1)
- {
- os.write(by,0,len);
- }
- //告诉服务端数据已写完
- s.shutdownOutput();
-
- String str = brIn.readLine();
- so.p(str);
-
-
- }
- }
复制代码 服务端- public class ergs {
- public static void main(String[] args) throws Exception{
- ServerSocket ss = new ServerSocket(10070);
-
- Socket socket = ss.accept();
- String ip = socket.getInetAddress().getHostAddress();
- so.p(ip+"......connected");
-
- FileOutputStream fos = new FileOutputStream("c:\\1_1.jpg");
-
- InputStream in = socket.getInputStream();
-
- //向客户端反馈信息
- PrintWriter pw =
- new PrintWriter(socket.getOutputStream(),true);
- Date d = new Date();
-
- byte[] b =new byte[1024];
- int len = 0;
- //循环读取socket输入流中的数据
-
- while ((len = in.read(b))!=-1)//这句出异常 {
- //将数据写到目标文件
- fos.write(b, 0, len);
- }
- so.p(ip+":上传成功");
-
- pw.write("在 "+d.toString()+" 上传成功"+"\r\n");
- pw.close();
- }
- }
复制代码 服务端出问题了,异常 Exception in thread "main" java.net.SocketException: Connection reset朋友说是关流的问题,就把有些关流的代码删了,,,但问题好像不再那里
|