用socket實現的檔案伺服器 3

2021-08-22 01:41:46 字數 3647 閱讀 5316

//主題帖cache規則:讀則加入,否則不需要加入

private static cache topiccache = new cache(300*1000*1000);

//回帖cache

private static cache replycache = new cache(300*1000*1000);

/*** 取主題貼,首先應該從cache中取

* * @param forumid

* @param topicid

* @return

*/public string gettopiccontent(int forumid, int topicid)

// 如果cache中沒有,就從檔案中讀

tryin.close();

//加入cache

topiccache.add(forumid+"_"+topicid,s2);

return s2;

}catch (exception e)

return "";

}/**

* 取回貼,首先應該從cache中取

* * @param forumid

* @param topicid

* @return

*/public list getreplylist(int forumid, int topicid)

// 如果cache中沒有,就從檔案中讀

list list = new arraylist();// 回貼的檔案記錄的list,不需要分解。分解工作在client來完成

tryin.close();

//加入到cache中

replycache.add(forumid+"_"+topicid,list);

}catch (exception e)

return list;

}/**

* 發帖

* * @param forumid

* @param topicid

* @return

*/public void addtopic(int forumid, int topicid, string content)

//寫入檔案

printwriter out = new printwriter(new filewriter(gettopicfile(forumid,topicid)));

out.write(content);

out.close();

}catch (exception e)

}/**

* 續貼

* @param forumid

* @param topicid

* @param content

*/public void resumetopic(int forumid, int topicid, string content)

//寫入檔案

content=gettopiccontent(forumid,topicid)+content;

printwriter out = new printwriter(new filewriter(filepath));

out.write(content);

out.close();

//如果cache中有,則需要更新

object cachecontent=topiccache.get(forumid+"_"+topicid);

if(cachecontent!=null)

}catch (exception e)

}/**

* 回貼,如果沒有回過貼,要新建檔案;如果回過,要續加回貼

* @param forumid

* @param topicid

* @return

*/public void addreply(int forumid, int topicid,string content,string qq,string replytime)

else//以前回過貼,從檔案中讀出回貼數,並修改為+1

//修改回貼數

dataoutputstream out=new dataoutputstream(new bufferedoutputstream(new fileoutputstream(replycountpath)));

out.writeint(replycount);

out.close();

//寫入檔案

randomaccessfile rf=new randomaccessfile(filepath,"rw");

rf.seek(rf.length());

string str=new integer(replycount)+"\0x1e"+qq+"\0x1e"+replytime+"\0x1e"+content+"\n";

rf.write(str.getbytes());

rf.close();

}catch (exception e) }

/*** 刪主題貼和它的回貼

* @param forumid

* @param topicid

* @param content

*/public void deletetopic(int forumid, int topicid)

//如果cache中有,則需要刪除

object cachecontent=topiccache.get(forumid+"_"+topicid);

if(cachecontent!=null)

//刪除回貼

file reply=new file(getreplyfile(forumid,topicid));

if(reply.exists())

file replycount=new file(getreplycountfile(forumid,topicid));

if(replycount.exists())

//如果cache中有,需要刪除

object replylist=replycache.get(forumid+"_"+topicid);

if(replylist!=null)

}catch (exception e)

}/**

* 刪某個回貼

* @param forumid

* @param topicid

* @param content

*/public void deletereply(int forumid, int topicid,int replyid)

else }

rfbak.close();

rf.close();

//原檔案刪除

file file=new file(filepath);

file.delete();

//新檔案改名

file file2=new file(filepath+".bak");

file2.renameto(new file(filepath));

//如果cache中有,需要刪除

object replylist=replycache.get(forumid+"_"+topicid);

if(replylist!=null)

}catch (exception e)

}

用socket實現的檔案伺服器 5

資料報內容解析 author hongsoft public class packetparserthread extends thread public void run else catch ioexception e finally catch ioexception e 中間全部用ox1e分...

用socket實現的檔案伺服器 3

主題帖cache規則 讀則加入,否則不需要加入 private static cache topiccache new cache 300 1000 1000 回帖cache private static cache replycache new cache 300 1000 1000 取主題貼,首...

用socket實現的檔案伺服器 5

資料報內容解析 author hongsoft public class packetparserthread extends thread public void run else catch ioexception e finally catch ioexception e 中間全部用ox1e分...