会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 107楼

老师,我这个没有相应成功。这是为啥呀.................


package com.bjsxt.server;


import java.io.BufferedWriter;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStreamWriter;

import java.net.ServerSocket;

import java.net.Socket;


public class Server3 {

public static void main(String[] args) {

String CRLF="\r\f";//换行

String BLANK=" ";

//(1)创建ServerSocket的对象

ServerSocket server=null;

//(2)监听是否有客户端发送请求

Socket client=null;

InputStream is=null;

 

try {

server = new ServerSocket(8888);

client = server.accept();

//获取来自浏览器的请求信息

is=client.getInputStream();

byte[] buf=new byte[20480];

int len=is.read(buf);

System.out.println(new String(buf,0,len));

/*对web浏览器的请求做出相应*/

StringBuilder sb=new StringBuilder();

StringBuilder sbContent=new StringBuilder();//相应的文本

sbContent.append("<html><head><title>相应结果</title></head>");

sbContent.append("<body>登陆成功</body></html>");

//(1)拼接响应头

sb.append("HTTP/1.1").append(BLANK).append(200).append(BLANK).append("OK");

sb.append(CRLF);//换行

sb.append("Content-Type:text/html;charset=utf-8");

sb.append(CRLF);//换行

sb.append("Content-Length:").append(sbContent.toString().getBytes().length).append(CRLF);

sb.append(CRLF);//换行,代表响应头与响应正文部门之间的空行

sb.append(sbContent);

//通过流输出

BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"utf-8"));

bw.write(sb.toString());

bw.flush();

bw.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally {

//(6)关闭流

IOClose.closeAll( is,client,server);

}

}

}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 108楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 111楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 112楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/智能电话本项目实战 113楼

/**
 *   发送信息的线程
 */
class Send extends Thread{
    private Socket socket;
    public Send(Socket socket){
        this.socket = socket;
    }

    /**
     * 发送信息的方法
     */
    private void sendMsg(){
        Scanner scanner = null;
        PrintWriter pw = null;
        try{
            // 创建Scanner对象,通过键盘输入获取要发送的信息
            scanner = new Scanner(System.in);
            // 创建字符输出流对象 用于向socket发送信息
            pw = new PrintWriter(this.socket.getOutputStream());
            while (true) {

                // 获取键盘输入的内容
                String msg = scanner.nextLine();
                // 将键盘输入的内容发送出去
                pw.println(msg);
                // 刷新
                pw.flush();
                if("exit".equals(msg)){
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(scanner != null){
                    scanner.close();
                }
                if(pw != null){
                    pw.close();
                }
                if (this.socket != null){
                    socket.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    @Override
    public void run() {
        this.sendMsg(); 
    }
}

/**
 *   接受信息的线程
 */
class Receive extends Thread{
    private Socket socket;
    public Receive(Socket socket){
        this.socket = socket;
    }

    /**
     *  接受信息的方法
     */
    private void receiveMsg(){
        BufferedReader br = null;
        try {
            // 创建用于接受对方发送信息的流对象
            br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while (true){
                String msg = br.readLine();
                System.out.println("他说:"+msg);
                if ("exit".equals(msg)){
                    break;
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(br != null){
                    br.close();
                }
                if(this.socket != null){
                    this.socket.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    @Override
    public void run() {
        this.receiveMsg();
    }
}

老师我添加了一个判断是否结束聊天的判断,但是这样写运行时只能是发送信息的一方关闭,接收的不会关闭

而且发送放会先报个异常再关闭

image.png报错行数是以下两个

image.png

image.png


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 114楼

微信图片_20230524144651.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 116楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 117楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 118楼

问题:copyFile方法为什么调用不了


package com.bjsxt.Homework;


import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;


public class Test {

public static void main(String[] args) {

File srcFile1 = new File("D:\\新建文件夹2");

File tarFile1 = new File("E:\\新建文件夹2");


CopyDir(srcFile1, tarFile1);

}


public static void CopyDir(File srcFile1,File tarFile1){

if(!(tarFile1.exists())){

tarFile1.mkdir();

}

File [] files=srcFile1.listFiles();

for (File file : files) {

if(file.isFile()){

copyFile(new File(srcFile1+"\\"+file.getName()), new File(tarFile1+"\\"+file.getName()));

}else{

CopyDir(new File(srcFile1+"\\"+file.getName()), new File(tarFile1+"\\"+file.getName()));

}

}

public static void copyFile(File srcFile,File tarFile){

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try {

bis = new BufferedInputStream(new FileInputStream(srcFile));

bos = new BufferedOutputStream(new FileOutputStream(tarFile));

byte [] buf=new byte[1024];

int len=0;

while((len=bis.read(buf))!=-1){

bos.write(buf, 0, len);

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

if(bos!=null){

bos.close();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

if(bis!=null){

bis.close();

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}


}


blob.png

blob.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 119楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 120楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637