Posts

Showing posts from April, 2016

Upload Hình Vào Thư Mục Có Đường Dẫn Tương Đối

 public void handleFileUpload(FileUploadEvent event) {         try { //đường dẫn đi vào thư mục img             String dirPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/img");             String d = dirPath.substring(0, 4);             File targetFolder = new File(dirPath);             OutputStream out; //inputstream để đọc đường dẫn             try (InputStream inputStream = event.getFile().getInputstream()) {                 out = new FileOutputStream(new File(targetFolder,                         event.getFile().getFileName()));                 setA("img/" + event.getFile().getFileName());                 int read = 0;                 byte[] bytes = new byte[1024];                 while ((read = inputStream.read(bytes)) != -1) {                     out.write(bytes, 0, read);                 }             }             out.flush();             out.close();         } catch (IOException e) {         }     }

CSS + Xhtml Nhìn Cũng Được AE Nào Cần Thì Lấy Sài

Image
Hình demo , tên class hay id phải đặt trùng vs css mới ra nhé ae , coi cho kĩ dễ bị quấn não lắm á //code .xhtml <?xml version='1.0' encoding='UTF-8' ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"       xmlns:h="http://xmlns.jcp.org/jsf/html"       xmlns:ui="http://xmlns.jcp.org/jsf/facelets">     <h:head>         <title>Home</title>         <link rel="stylesheet" href="homestyle.css"/>       <meta charset="utf-8" />     </h:head>     <h:body>         <header id="header">             <hgroup>                 <h1 class="site_title">                     <a href="list.xhtml">Website Admin</a>                 </h1>        

Hủy session trong Java

Hàm destroy session trong java , khi bạn tạo ra session thì catch đã nhớ password của bạn rồi nếu bạn muốn logout ra thì phải hủy session vừa tạo  code tham khảo như sau :  public String logout(){         HttpSession sessout=(HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);         sessout.invalidate(); // trả về trang chủ lúc chưa đăng nhập         return "index" ;             }

Đường dẫn tương đối trong Java

Đây ai muốn  lấy hình hay xóa hình lấy file hay bất cứ j mà cần đường dẫn tương đối thì đây , mình đã thử và thành công :    String dirPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/upload"); có ví dụ minh họa cho coi chơi luôn , cái này mình lấy đường dẫn tương đối để tìm thư mục chứa hình để xóa nhé : public void xoahinh(Object hinh) {         String tamhinh = "";         int id = (int) hinh;         try { // chuổi kết nối , nhớ add thư viện jdbc4.jar nhé             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");             Connection con = DriverManager.getConnection(ServerConnectString, "sa", "sa");             PreparedStatement comm = con.prepareStatement("select image from news where newsid = '" + id + "'");             ResultSet rs =comm.executeQuery();                         while(rs.next()){             tamhinh = rs.getString(1);