📄 response.java
字号:
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.*;
import java.io.*;
public class Response
{
public String curUser[]=new String[10];
int p=0;
public String curUser11=new String();
public static final int ERROR=-1;
public static final int WELCOME=101;
InputStream input=null;
OutputStream output=null;
Request request;
boolean zz;
public void setRequest(Request request)
{
this.request=request;
}//初始化request对象
public Response(InputStream input,OutputStream output)
{
this.input=input;
this.output=output;
}//初始化input,output变量
public void sendMsg(String sendMsg) throws IOException
{
sendMsg+="\r\n";
try
{
output.write(sendMsg.getBytes(),0,sendMsg.length());
System.out.println(sendMsg);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}//发送响应给用户
public String getDomain(String forwardPath)
{
String domain=new String();
int index=forwardPath.indexOf('@');
if(index<0)
domain="";
else domain=forwardPath.substring (index+1,forwardPath.length());
return domain;
}//取得服务器域名
public boolean sendEventMsg(int evt)
{
try
{
switch(evt)
{
case WELCOME:
sendMsg("220 my simple mail transfer ready");//处理welcome命令
break;
case Request.HELO:
sendMsg("250 MySMTP");
break;//处理helo命令
case Request.MAIL:
sendMsg("250 OK ");
break;//处理mail命令
case Request.RCPT:
if(getDomain(request.getForwordPath()).equals(SmtpServer.domain))//判断是否为本地服务器
{
zz=true;
if(verify(request.getUser()))//判断用户是否存在于用户列表中
{
curUser[p]=request.getUser();
p++;//取得用户名
sendMsg("250 OK");
}
else sendMsg("550 No such user here.");
}
if(getDomain(request.getForwordPath()).equals(SmtpServer.domain2))
//局域网中的服务器域名
{
zz=false;
curUser11=request.getUser();
sendMsg("251 User not local;will forword to"+ getDomain(request.getForwordPath()));
}
break;
case Request.DATA://处理data命令
if(!curUser[0].equals(""))
{
sendMsg("354 ok,send it;end with <CRLF>.<CRLF>");
getMail();
sendMsg("250 ok");
}
else
sendMsg("550 send to nobody");
break;
case Request.RESET://处理reset命令
{
sendMsg("250 ok");
break;
}
case Request.NOOP://处理noop命令
{
sendMsg("200 NOOP command successful");
break;
}
case Request.QUIT://处理quit命令
{
request.clear();
sendMsg("221 my stmp service closing.....");
break;
}
default:
{
sendMsg("500 '"+ request.getRequestCmd()+ "': command not understood .");
break;
}//命令错误时将错误信息返回给用户
}
return true;
}
catch(Exception e)
{
System.out.println(e.toString());
return false;
}
}
private boolean verify(String user)//检查用户是否存在于用户列表中
{
boolean userexist=false;
String s;
BufferedReader in1=null;
try
{
in1=new BufferedReader(new FileReader("c:\\bj.txt"));
while((s=in1.readLine())!=null)
{
if(s.equals(user))
{
userexist=true;
break;
}
}
}
catch(Exception e)
{
System.out.println(e.toString());
System.exit(0);
}
try
{
in1.close();
}
catch(Exception e)
{
}
return userexist;
}
public void getMail()//取邮件
{
int BUFFER_SIZE=1024;
int x=0;
try
{
byte[] bytes=new byte[BUFFER_SIZE];
FileOutputStream fos=null;//文件输出楼
File qq=new File(SmtpServer.rootDir,curUser[0]);//创建邮件目录下的用户目录
if(!qq.exists())qq.mkdir();
try
{
int h;
int i;
File file=new File(qq,request.getRevrsePath()+String.valueOf(0)+".txt");
for( i=1;file.exists();i++)
file=new File(qq,request.getRevrsePath()+
String.valueOf(i)+".txt");//如果之前有邮件存在,则邮件名自加1以区别
i--;
x=i;
file.createNewFile();
fos=new FileOutputStream(file);
int ch=input.read(bytes,0,BUFFER_SIZE);
while(ch!=-1)//不断读取邮件内容,并且将内容保存起来
{
if(isEnd(bytes,ch))//如果最后一行是'.'则意味着邮件结束
{
if(ch>=5)fos.write(bytes,0,ch-5);
break;
}
fos.write(bytes,0,ch);
ch=input.read(bytes,0,BUFFER_SIZE);
}
File qq1=null;
File file1=null;
FileInputStream fin1=null;
fin1=new FileInputStream(file);
for(h=1;h<p;h++)
{
qq1=new File(SmtpServer.rootDir,curUser[h]);
if(!qq1.exists())qq1.mkdir();
file1=new File(qq1,request.getRevrsePath()+String.valueOf(0)+".txt");
for(int y=1;file1.exists();y++)
{file1=new File(qq1,request.getRevrsePath()+String.valueOf(y)+".txt"); }
file1.createNewFile();
fos=new FileOutputStream(file1);
int ch11=fin1.read(bytes,0,1024);
while(ch11!=-1)
{
fos.write(bytes,0,ch11);
ch11=fin1.read(bytes,0,1024);
}
}
}
catch(Exception e)
{
System.out.println(e.toString() );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -