本帖最后由 jiangyutc 于 2014-2-16 12:14 编辑
- package com.itheima;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- public class Test9_ServerSocket {
-
- public static void main(String[] args) throws IOException {
-
- //创建文件对象来并确定客户端上传的文件的存放地址
- File file = new File("..\\exam_入学\\文件\\复制.doc");
-
- //利用文件输出流在文件对象上写数据
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(file);
- } catch (FileNotFoundException e2) {
- // TODO Auto-generated catch block
- e2.printStackTrace();
- }
-
- //创建服务端的Socket流
- ServerSocket ss = null;
- try {
- ss = new ServerSocket(10000);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- //获取客户端的Socket流对象
- Socket s = null;
- try {
- s = ss.accept();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- //获取Socket流字符输入流,用来接受数据
- InputStream bs = null;
- try {
- bs = s.getInputStream();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- byte[] b = new byte[1024];
- int len = 0;
- while(!((len = bs.read(b)) == -1)){
-
- fos.write(b,0,len);
- }
-
- OutputStream os = s.getOutputStream();
- os.write("传输成功......".getBytes());
-
- try {
- s.close();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
-
- try {
- ss.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
复制代码
- package com.itheima;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class Test9_Socket {
- public static void main(String[] args) throws Exception {
-
- //创建文件对象
- File file = new File("..\\exam_入学\\文件\\java面试宝典2013版.doc");
- // File file = new File("D:\\1.txt");
- //创建文件对象输入流并抛出异常
- FileInputStream ios = null;
- try {
- ios = new FileInputStream(file);
- } catch (FileNotFoundException e2) {
- System.out.println("该文件不存在");
- }
-
-
- //因为服务端在本地主机上,所以要获取本地主机
- InetAddress ia = null;
- try {
- ia = InetAddress.getLocalHost();
- } catch (UnknownHostException e1) {
- System.out.println("如果找不到 host 的任何 IP 地址");
- }
- //获取本地的IP地址
- String ip = ia.getHostAddress();
- //根据服务器的IP地址和端口,创建Socket流用来连接服务器并抛出异常
- Socket s = null;
- try {
- s = new Socket(ip,10000);
- } catch (UnknownHostException e1) {
- System.out.println("如果找不到 host 的任何 IP 地址");
- } catch (IOException e1) {
- System.out.println("创建套接字时发生 I/O 错误");
- }
-
-
- //获取Socket流中的输出流,用来上传数据
- OutputStream os = null;
- try {
- os = s.getOutputStream();
- } catch (IOException e) {
- System.out.println("创建输出流时发生 I/O 错误或者没有连接套接字");
- }
-
- //创建数组用来提高上传效率
- byte[] b = new byte[1024];
-
- int len = 0;
- while(!((len = ios.read(b)) == -1)){
-
- try {
- os.write(b,0,len);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- byte[] b1 = new byte[1024];
- int len1 = 0;
- InputStream is = s.getInputStream();
- len = is.read(b);
- String text = new String(b1,0,len1);
- System.out.println(text);
-
- s.close();
-
- }
- }
复制代码
这上面的代码复制文件没问题,但是给客户端反馈信息的时候就卡着不动了。。。。必须要关闭客户端的控制台才能复制成功。。。。
怎么回事???
各位大神求教 |