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

一、跟着老师敲的代码运行出来浏览器没有响应

package com.bjsxt.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;



public class Server3 {
	public static void main(String[] args) {
		String CRLF="/r/n";
		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=0;
			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(),"GBK"));
			bw.write(sb.toString());
			bw.flush();
			bw.close();
			
			 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		//(6)关闭流
		try {
			IOClose.CloseAll(is,client,server);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}
}

二、html代码

<html>
	<head>
		<title>我的第一个html</title>
	</head>
	<body>
		<h1>Hello World</h1>
		<form action="http://localhost:8888/index.html" method="post">
			<p>用户名:<input type="text" id="uname" name="username"</p>
			<p>密码:<input type="password" id="pwd" name="password"></p>
			<p><input type="submit" value="登陆"/></p>
		</form>
	</body>
</html>


三、运行截图

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3531楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 3532楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3534楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3535楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 3536楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3537楼

public class CopyDirectory{
	public static void main(String[] args) {
		copyDirectory(new File("G:\\电子大赛文件\\copyfile"),new File("D:\\copyfile"));
	}
	
	//复制文件夹的方法
	public static void copyDirectory(File src,File dir) {
		if(!dir.exists()) dir.mkdir(); //目的地是否有dir目录, 没有就创建一个文件夹
		File[] files = src.listFiles();
		
		for(File file:files) {
			if(file.isFile()) { //如果当前File对象是文件  则直接进行拷贝文件
				//new File(src +"\\" + file.getName()) 通过构造方法,创建每一个文件(目录)对象
				copyFile(new File(src +"\\" + file.getName()),new File(dir +"\\" + file.getName()));
			}else {  //如果当前File对象是目录 通过递归调用 实现多层级复制
				copyDirectory(new File(src +"\\" + file.getName()),new File(dir +"\\" + file.getName()));
			}
		}
	} 
	
	//复制一个文件
	/**
	 * @param src
	 * @param dir
	 */
	public static void copyFile(File src,File dir) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		try {
			bis = new BufferedInputStream(new FileInputStream(src));
			bos = new BufferedOutputStream(new FileOutputStream(dir));
			byte[] b = new byte[1024];  // 创建byte数组的中转站,缓存为1024byte
			int len;  // 接受read()方法的返回值
			 
			while((len = bis.read(b)) != -1) {
				bos.write(b); //写出byte数组的数据
				bos.flush();
			}

		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		if(bos == null) {
			try {
				bos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(bis == null) {
			try {
				bis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}

老师,在复制文件的代码中,对于

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

bos.write(b); 

bos.flush();

}

如果循环条件写成 while(len = bis.read(b) != -1) { }程序会报错。java程序的执行顺序是 从左到右、从上到下。去掉括号不影响程序执行顺序啊,为什么就会报错呢?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 3538楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程和并发编程(旧) 3539楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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