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) {
}
}
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) {
}
}