- //服务器
- public class Server {
- public static void main(String[] args) {
-
- ServerSocket ss = null;
- Socket s = null;
- try {
-
- ss = new ServerSocket(6565);
- BufferedInputStream bis = null;
- BufferedOutputStream bos = null;
- while(true){
- try{
- s = ss.accept();
- bis = new BufferedInputStream(s.getInputStream());
- bos = new BufferedOutputStream(new FileOutputStream("F:\\java带吗 "));
-
- byte[] buff = new byte[1024];
- int len = 0;
- while((len=bis.read(buff))>0){
- bos.write(buff, 0, len);
- }
- }finally{
- if(bis!=null){
- bis.close();
- }
- if(bos!=null){
- bis.close();
- }
- if(s!=null){
- s.close();
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- if(ss!=null){
- ss.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
复制代码- //客户端
- public class Client {
- public static void main(String[] args) {
- Socket s = null;
- BufferedOutputStream bos = null;
- BufferedInputStream bis = null;
- String filepath = "F:\\eclipse\\java代码";
- try {
- s = new Socket("127.0.0.1",6565);
- bis = new BufferedInputStream(new FileInputStream(filepath));
- bos = new BufferedOutputStream(s.getOutputStream());
- byte[] buff = new byte[1024];
- int len = 0;
- while((len=bis.read(buff))>0){
- bos.write(buff, 0, len);
- }
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally{
- try {
- if(bis!=null){
- bis.close();
- }
- } catch (IOException e2) {
- e2.printStackTrace();
- }
- try {
- if(bos!=null){
- bos.close();
- }
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- try {
- if(s!=null){
- s.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
复制代码
|
|