Talk to people.
I'm a beginner in Java and I'm doing a small Dynamic Web project using PrimeFaces, JSP, Hibernate and TomCat. Basically it is about several forms of registers and one of them is the register of users. The Domain, Bean, DAO, and the list, register, edit, and delete pages are already up and running, but the user table has a field for the picture, and that's where my problem is. I want to save only the path of the Photo (image) in the database, and in the user page I want to let them add their photo, of course. I read lots of things on Google how to do it using the p: fileUpload component, and I confess I got it in parts. The problem is that I want the images to be saved in the correct way, for example in a folder / images in my project.
The way I'm doing it now looks like this: No Bean
public void upload(FileUploadEvent event) {
try {
String realPath = FacesContext.getCurrentInstance()
.getExternalContext().getRealPath("/");
// Aqui cria o diretorio caso não exista
File file = new File(realPath + "/imagens/");
file.mkdirs();
byte[] arquivo = event.getFile().getContents();
String caminho = realPath + "/imagens/"
+ event.getFile().getFileName();
// esse trecho grava o arquivo no diretório
FileOutputStream fos = new FileOutputStream(caminho);
fos.write(arquivo);
fos.close();
pathImage = caminho;
System.out.println("caminho da imagem salva é = " + caminho);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}'
In the .xhtml file
<p:fileUpload fileUploadListener="#{checksPicosBean.upload}" fileLimit="1"
fileLimitMessage="Excedido Limite de arquivos"
cancelLabel="Cancelar" label="Arquivo" uploadLabel="Anexar"
invalidFileMessage="Somente arquivos .jpg, .png ou .gif"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" value="#{checksPicosBean.imagem}"
mode="advanced" skinSimple="true" />
The problem is that it is saving to a folder on my disk c: and not in a supposed server folder. What would be the correct way to make it happen? Is there any configuration in TomCat that has to be done? I plan to make these forms available on the web, so I'd like to know what the correct way is to make it work.