目錄
一、如何利用正則表達式找到img標簽 compile編譯編制
二、下載圖片的方法
三、socket實現網絡聊天
1.inputStreamReader ?字節的讀取流--->字符的讀取流
2.outputStreamWriter 字節的寫入流--->字符的寫入流
一、如何利用正則表達式找到img標簽 compile編譯編制
? ? ? int c=0;
?? ??? ?Pattern p = Pattern.compile("<img\\ssrc=\"([^>\"]+)\"\\s(width=\"\\d+\"\\sheight=\"\\d+\"\\s)?/>");//里面寫上正則
?? ??? ?Matcher m = p.matcher(sb);//匹配誰
?? ??? ?while(m.find()){
?? ??? ??? ?c++;
?? ??? ??? ?String s = m.group(1);
?? ??? ??? ?myload("file:///E:/1-0809/S1階段/6-html/site/"+s);
?? ??? ?}
二、下載圖片的方法
?public static void myload(String str){
?? ??? ?try {
?? ??? ??? ?URL url = new URL(str);
?? ??? ??? ?InputStream is = url.openStream();
?? ??? ??? ?String s = str.substring(str.lastIndexOf("/")+1);
?? ??? ??? ?File file = new File("f:\\圖片\\aa\\"+s);
?? ??? ??? ?FileOutputStream fos = new FileOutputStream(file);
?? ??? ??? ?BufferedOutputStream bos = new BufferedOutputStream(fos);
?? ??? ??? ?int n = -1;
?? ??? ??? ?while((n=is.read())!=-1){
?? ??? ??? ??? ?bos.write(n);
?? ??? ??? ??? ?bos.flush();
?? ??? ??? ?}
?? ??? ??? ?bos.close();
?? ??? ??? ?fos.close();
?? ??? ??? ?is.close();
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO: handle exception
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
三、socket實現網絡聊天
? * 1.啟動服務器
? * ? ? ?1.1等待客戶端上線
?* 2.客戶端連接服務器
?* 3.客戶端給服務器發送信息(寫)
?* 4.服務器接收客戶端的信息(讀)
?* 5.服務器給客戶端回復信息
?* 6.客戶端接收服務器的信息
? * 7.關閉所有連接
1.啟動服務器
?? ?ServerSocket ss = new ServerSocket(8899);
1.1等待客戶端上線
?? ?System.out.println("服務器已啟動,等待客戶端上線。。。。。。");
?? ?Socket sk = ss.accept();
?? ?System.out.println("客戶端已經上線,主機名為:"+sk.getInetAddress().getHostName());
2.客戶端連接服務器
?? ?Socket sk = new Socket(InetAddress.getByName("127.0.0.1"), 8899);
?? ?System.out.println("客戶端已上線~~~~~~");
3.客戶端給服務器發送信息
?? ??? ?//獲取網絡的字節寫入流
?? ??? ?OutputStream os = sk.getOutputStream();
?? ??? ?//轉為字符的寫入流
?? ??? ?OutputStreamWriter osw = new OutputStreamWriter(os);
?? ??? ?//包裝
?? ??? ?BufferedWriter bw = new BufferedWriter(osw);
?? ??? ?//由用戶自己輸入內容
?? ??? ?Scanner mys = new Scanner(System.in);
?? ??? ?System.out.println("請輸入你要發送的內容:");
?? ??? ?String sa = mys.next();
?? ??? ?//寫入網絡中
?? ??? ?bw.write(sa);
?? ??? ?//換行&&刷新
?? ??? ?bw.newLine();
?? ??? ?bw.flush();
4.服務器接收客戶端的信息
?? ??? ?//獲取網絡的讀取流
?? ??? ?InputStream is = sk.getInputStream();
?? ??? ?//轉換為字符的讀取流
?? ??? ?InputStreamReader isr = new InputStreamReader(is);
?? ??? ?//包裝
?? ??? ?BufferedReader br = new BufferedReader(isr);
?? ??? ?//讀取一行
?? ??? ?String sa = br.readLine();
?? ??? ?//打印客戶端發送過來的信息
?? ??? ?System.out.println("收到客戶端的信息:"+sa);