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

public void start() {
    Menu menu = new Menu();
    TelNoteRegex regex = new TelNoteRegex();
    Operate operate = new Operate();
    File file = new File("/Users/JAVA/a.txt");
    ObjectInputStream ois = null;
    Object o = null;
    try {
        file.createNewFile();
        ois = new ObjectInputStream(new FileInputStream(file));
        if ((o = ois.readObject())==null){
            return;
        }else {
            operate.ObjectInputStreamDemo();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    while (true) {
        menu.mainMenu();
        int item = regex.menuItemValidate(1, 6);
        switch (item) {
            case 1:
                operate.addLogic();
                break;
            case 2:
                operate.searchLogic();
                break;
            case 3:
                operate.modifyLogic();
                break;
            case 4:
                operate.deleteLogic();
                break;
            case 5:
                operate.orderLogic();
                break;
            case 6:
                operate.ObjectOutputStreamDemo();
                System.exit(0);

        }
    }
}

我在这里做了判断了,为什么还会报EOF?

public void ObjectOutputStreamDemo(){
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(new FileOutputStream("/Users/JAVA/a.txt",true));
        for (Person temp:list){
            oos.writeObject(temp);
        }
        oos.flush();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try {
            if (oos!=null){
                oos.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
public void ObjectInputStreamDemo(){
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(new FileInputStream("/Users/JAVA/a.txt"));
        Person a = null;
        while(true){
            if ((a = (Person) ois.readObject())==null){
                break;
            }
            Person person =(Person) ois.readObject();
            this.list.add(person);
        }

    }catch (Exception e){
        e.printStackTrace();
    }finally {
        try {
            if(ois!=null){
                ois.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

StreamCorruptedException这个报错我该怎么修改呢,有时候if语句中还会报类型强制转换的错误是为什么呢

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 2330楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 2333楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2335楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器 2336楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 2338楼

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

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 基础深化和提高/手写服务器项目(旧) 2340楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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