Boa tarde tudo bem?
Pessoal é o seguinte, eu estou tentando fazer o upload de uma imagem, o meu codigo não dá erro nenhum, “roda certinho”, mas não salva a imagem.
Alguem sabe me dizer o pq?
Abaixo segue o metodo
public void upload() {
try {
if (fotoPerfil != null) {
//E necessario informar um local para gravar a imagem,
//sera na pasta upload dentro do nosso projeto
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String local = servletContext.getRealPath("fotoperfil");
//gravamos a imagem
InputStream input = fotoPerfil.getInputstream();
String somentenome = fotoPerfil.getFileName().substring(fotoPerfil.getFileName().lastIndexOf(File.separatorChar) + 1);
cliente.setPathImagemCliente(local + File.separator + somentenome);
FileOutputStream fos = new FileOutputStream(local + File.separator + somentenome);
System.out.println(fos);
int len;
byte[] buffer = new byte[1024];
while ((len = input.read(buffer)) != -1) {
fos.write(buffer, 0, len);
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
//fechamos o arquivo
input.close();
fos.flush();
fos.close();
}
} catch (Exception x) {
x.printStackTrace();
}
}