Java編程完成遍歷兩個MAC地址之間一切MAC的辦法。本站提示廣大學習愛好者:(Java編程完成遍歷兩個MAC地址之間一切MAC的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Java編程完成遍歷兩個MAC地址之間一切MAC的辦法正文
本文實例講述了Java編程完成遍歷兩個MAC地址之間一切MAC的辦法。分享給年夜家供年夜家參考,詳細以下:
在對發放的裝備停止後台治理時,許多時刻會用到裝備MAC這個字段,它可以標識獨一一個裝備。但是在數據庫批量的存儲MAC地址時,假如應用解析文本逐行添加的方法,不免會顯得操作龐雜,並且事前還需生成MAC地址文本。現實上MAC地址是依照十六進制一一遞增的,所以只須要給出一個區間便有能夠列舉出一切MAC地址。以下是筆者封裝的一個經由過程兩個MAC地址列舉區間內一切MAC的函數。
/** 輸入兩個MAC區間內的一切MAC地址 */
public static void countBetweenMac(String macStart, String macEnd){
long start = turnMacToLong(macStart);
long end = turnMacToLong(macEnd);
String prefix = macStart.substring(0,9);
String hex = null;
String suffix = null;
StringBuffer sb = null;
for(long i=start; i< end +1; i++){
hex = Long.toHexString(i);
suffix = hex.substring(hex.length()-6);
sb = new StringBuffer(suffix);
sb.insert(2, ":");
sb.insert(5, ":");
System.out.println(prefix + sb.toString());
}
}
/** 將MAC轉換成數字 */
public static long turnMacToLong(String MAC){
String hex = MAC.replaceAll("\\:", "");
long longMac = Long.parseLong(hex, 16);
return longMac;
}
另附盤算兩個MAC之間MAC中個數函數:
/** 盤算區間內MAC總數 */
public static long countMac1(String macStart, String macEnd){
String hexStart = macStart.replaceAll("\\:", "");
String hexEnd = macEnd.replaceAll("\\:", "");
long start = Long.parseLong(hexStart, 16);
long end = Long.parseLong(hexEnd, 16);
return end-start+1;
}
/** 盤算區間內MAC總數 */
public static long countMac(String macStart, String macEnd){
String[] start = macStart.split("\\:");
String[] end = macEnd.split("\\:");
int x,y,z;
int a,b,c;
x = Integer.parseInt(start[3],16);
y = Integer.parseInt(start[4],16);
z = Integer.parseInt(start[5],16);
a = Integer.parseInt(end[3],16);
b = Integer.parseInt(end[4],16);
c = Integer.parseInt(end[5],16);
return (a-x)*16*16*16 + (b-y)*16*16 + c-z+1;
}
願望本文所述對年夜家Java法式設計有所贊助。
aima">
<span > f0=ff;
if(ff.isFormField()){//這個為描寫的內容
String name=ff.getString("utf-8");//采取utf-8的編碼方法去讀
p.setDesc(name);//1 文件的描寫
}else{
String name=ff.getName();//取得文件本框外面的內容--->全部圖片的目次
//System.out.println("name:"+name);
String id=UtilsFactory.getUUid();
p.setId(id);//6
String dirs=UtilsFactory.getDir(id);//取得文件夾目次----應用uuid逐個打散了的
p.setDir(dirs);//2 打亂以後的目次
p.setDt(UtilsFactory.getDate());//3 時光
String relname=name.substring(name.lastIndexOf("/")+1);
p.setRelName(relname);//4 文件的真實名字
String ext=name.substring(name.lastIndexOf("."));
p.setExt(ext);//5 文件擴大名
p.setIp(request.getRemoteAddr());//7 IP
boo =MyDao.Add(p);//保留到xml文件中
if(boo){//保留勝利
path=path+"/"+p.getDir();
File f1 =new File(path);//斷定文件的存儲途徑能否存在,不存在就創立
if(!f1.exists()){
f1.mkdirs();
}
in=ff.getInputStream();
FileUtils.copyInputStreamToFile(in,new File(path+"/"+p.getId()+p.getExt()) );
}
}
}
} catch (FileUploadException e) {
boo=false;
}finally{
if(f0!=null){
f0.delete();//刪除暫時文件
}
}</span>
上傳除統計參數,我們須要將數據存儲的xml文件中,還須要將圖片存儲起來。等閱讀的時刻同一檢查。
後果圖:
同一閱讀
閱讀根本就是全體將xml文件外面的數據,讀出來,然後同一讀出來顯示。封裝在一個list中,將一切的photo數據封裝在list聚集中
//查詢一切的對象然後封裝成一個list對象前往給前端
public static List<Photo> getAll(){
List<Photo> list=new ArrayList<Photo>();
Document dom =DocumentFactory.getDocument();
Element root=dom.getRootElement();
Iterator it=root.elementIterator();//這是根節點遍歷器
while(it.hasNext()){
Element e=(Element) it.next();//找到節點
Photo p =new Photo();//每的photo地址紛歧樣,所以必需每次新開空間
p.setDesc(e.attributeValue("desc"));//文件描寫符
p.setDir(e.attributeValue("dir"));//文件目次
p.setDt(e.attributeValue("dt"));//時光
p.setExt(e.attributeValue("ext"));//文件擴大名
p.setId(e.attributeValue("id"));//uuid生成的id
p.setIp(e.attributeValue("ip"));
p.setRelName(e.attributeValue("relname"));
list.add(p);
}
return list;
}
詳細代碼:
//閱讀相冊須要把一切的文件讀出來。須要逐個去讀,所以須要去讀一切的xml文件
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
List<Photo> list=MyDao.getAll();//取得一切xml文件外面的內容,數據全體封裝到list中
String path=getServletContext().getContextPath();//進入web以後要采取絕對途徑能力拜訪的到
String ss=null;
String imgs=null;
String dt=null;
String relName;
String tt=null;
String str = "<table border=1px width='80%'><tr><th>相冊名</th><th>時光</th><th>圖片顯示</th><th>操作</th>";
out.write(str);
for(Photo p:list){
relName=p.getRelName();
dt=p.getDt();
imgs=path+"/upFile/"+p.getDir()+"/"+p.getId()+p.getExt();//完成的文件途徑加文件名
ss="<tr><td>"+relName+"</td><td>"+dt+"</td><td>"+"<a href='"+imgs+"'><img style='border:0px' width='100' height='100' src='"+imgs+"' alt='圖片'/></a></td>";
tt="<td><a href='DownFile?id="+p.getId()+"'>下載</a> <a href='MyDelelte?id="+p.getId()+"'>刪除圖片</a>"+"</td></tr>";//經由過程id來辨別他們直接的差別 可以停止刪除和下載
out.write(ss);
out.write(tt);
}
後果:
單個下載
下載在之前的上傳和下載中都說的很清晰了,點擊翻開鏈接: 《JavaEE完成前後台交互的文件上傳與下載》。
下載的代碼中須要留意:須要設置響應頭和文件名的傳輸
關於下載文件須要重要 起首須要讓閱讀器曉得 設置頭
* response.setContentType("application/force-download");//設置響應頭,告知閱讀器這是下載文件
* 第二就是設置文件名了
* response.setHeader("Content-Disposition","attachment;filename='"+relName+"'");//下載是那裡顯示的是本來文件名
上面是詳細的代碼:
response.setContentType("application/force-download");//設置響應頭,告知閱讀器這是下載文件
request.setCharacterEncoding("utf-8");
String id=request.getParameter("id");
Photo p=MyDao.getSingalByid(id);//經由過程id取得要下載的對象
//寫入真實名字
if(p!=null){
String relName1=p.getRelName();
String relName=URLEncoder.encode(relName1, "utf-8");
response.setHeader("Content-Disposition","attachment;filename='"+relName+"'");//下載是那裡顯示的是本來文件名
OutputStream out =response.getOutputStream();//寫文件時刻須要
//途徑
String path="/upFile/"+p.getDir()+"/"+p.getId()+p.getExt();
String path1 =getServletContext().getRealPath(path);
System.out.println(path1);//檢測
InputStream in=new FileInputStream(path1);
byte[] b=new byte[1024];
int len =0;
while((len=in.read(b))!=-1){
out.write(b, 0, len);
}
}else{
response.setContentType("utf-8");
PrintWriter pw =response.getWriter();
pw.write("文件不存在沒法下載");
}
}
後果圖:
刪除文件
刪除文件須要用到的技巧絕對其他功效
要婚配IP,ID如許能力讓刪除的時刻用權限
//刪除照片
public static Map<String , Object> deleteByid(String ip,String id) {
Map<String, Object> map =new HashMap<String, Object>();
Document dom =DocumentFactory.getDocument();
Element ele=(Element) dom.selectSingleNode("//photo[@id='"+id.trim()+"']");//xpath的應用
if(ele==null){
map.put("success", false);
map.put("msg", "曾經刪除");
return map;
}else{
String tempip=ele.attributeValue("ip");
if(!tempip.equals(ip)){
map.put("success", false);
map.put("msg", "你不克不及刪除他人的照片");
return map;
}else{
map.put("success", true);
//拜訪勝利後,把數據分裝成一個值對象,前往給邏輯層 我們如許直接刪除,只是xml文件外面的節點刪除,然則曾經存儲的文件是沒有刪除的
Photo p =new Photo();
p.setId(id);
p.setExt(ele.attributeValue("ext"));
p.setDir(ele.attributeValue("dir"));
map.put("photo", p);
//真實的數據刪除
ele.getParent().remove(ele);
DocumentFactory.Save();
return map;
}
}
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
String id =request.getParameter("id");//從客戶端傳過去的新聞
String ip =request.getRemoteAddr();
Map<String, Object> map =MyDao.deleteByid(ip,id);
if(map.get("success").equals(false)){//這都是刪除不勝利的
out.print("新聞為:"+map.get("success"));
}else{
Photo p =(Photo) map.get("photo");//依據photo外面的文件名和途徑刪除文件
String path=getServletContext().getRealPath("upFile");
String filename=path+"/"+p.getDir()+"/"+p.getId()+p.getExt();//文件的途徑包含文件名
System.out.println(filename);
File f=new File(filename);
if(f.exists()){
System.out.println(f.getName());
f.delete();//刪除文件
}
}
response.sendRedirect("LookPhoto");//重定向到顯示頁面
}
全部項目標下載鏈接: 《javaWEB完成相冊治理的簡略功效》
正在處於進修中,剛學到這個常識點,技巧能夠很落伍了,但也情願分享給年夜家,年夜家配合提高。