galera estoou com um problema para fazer upload na minha JSP. Estou usando o richfaces. segue o cód abaixo
Meu Bean
public void uploadImageComAjax(UploadEvent evento) throws FileNotFoundException {
FacesContext fc = FacesContext.getCurrentInstance();
ServletContext sc = (ServletContext) fc.getExternalContext().getContext();
String path = sc.getRealPath("/");
System.out.println("path " + path);
UploadItem item = evento.getUploadItem();
System.out.println("item " + item);
System.out.println(".getDataitem " + item.getData());
String filePathName = item.getFileName();
System.out.println("filePathName" + filePathName);
String fileName = "";
StringTokenizer st = new StringTokenizer(filePathName, "\\");
System.out.println("st " + st);
while (st.hasMoreElements()) {
fileName = st.nextToken();
System.out.println("fileName " + fileName);
}
path = path + "imagens/" + fileName;
System.out.println("path 2 " + path);
OutputStream out = new FileOutputStream(path);
try {
out.write(item.getData()); // aqui o item.getData() está vindo null
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
minha JSP
<h:outputText value="Foto: "/>
<rich:fileUpload id="foto" styleClass="campo" fileUploadListener="#{autores.uploadImageComAjax}" acceptedTypes="jpg, png" autoclear="true" />
Daí eu preciso salvar no banco. Eu salvaria a String “nome da foto” ou o “caminho”??, e deixar a imagem no diretorio imagens.
tah dando nullpointer nao estou entendendo pq está nulo.
alguem pode me ajudar?
problema resolvido em relação ao null q aparece, era uma configuração do ajax q estava no web.xml e tava dando o problema
porem nao está salvando no diretório algem poderia me dar um help???
public void uploadImageComAjax(UploadEvent evento) throws FileNotFoundException {
FacesContext fc = FacesContext.getCurrentInstance();
ServletContext sc = (ServletContext) fc.getExternalContext().getContext();
String path = sc.getRealPath("/");
System.out.println("path " + path);
UploadItem item = evento.getUploadItem();
String filePathName = item.getFileName();
System.out.println("filePathName" + filePathName);
String fileName = "";
StringTokenizer st = new StringTokenizer(filePathName, "\\");
System.out.println("st " + st);
while (st.hasMoreElements()) {
fileName = st.nextToken();
System.out.println("fileName " + fileName);
}
path = path + "imagens/" + fileName;
System.out.println("path 2 " + path);
/* OutputStream out = new FileOutputStream(path);
try {
out.write(item.getData());
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}*/
if (item.isTempFile()) {
InputStream in = new FileInputStream(item.getFile());
System.out.println("in " + in);
OutputStream out = new FileOutputStream(path);
System.out.println("out " + out);
byte[] buf = new byte[1024];
System.out.println("buf " + buf);
int len;
try {
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
System.out.println("while");
}
in.close();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
OutputStream out = new FileOutputStream(path);
//System.out.println("b " + b);
try {
out.write(item.getData());
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
Acredito q seja algum problema em relação as barras, pois estou usando linux.
alguem pode dar uma luz?:??
bom dia meu amigo, já tentasse File.separator?
eu fiz um aqui parecido com o seu, mas a imagem eu jogo no banco (postgresql)
tipo eu estava pensando em salvar apenas o nome da imagem no banco, e deixar a imagem no diretório. Porem nao sei ao certo como devo fazer pra salvar a imagem no banco.
Como seria pra salvar no banco?? Qual seria o tipo campo?
pow tentei aqui usando o File.separator mas nao salva em lugar nenhum nem no temp e nem no diretorio imagens
qual o banco que vc esta usando?
eu utilizei o postgresql ( img bytea )
e na classe ( model)
@Lob
@Column(name=“img”)
private byte[] img;
pow cara blz consegui salvar no banco mas vem pra resgatar ela e mostrar na JSP.
o q fiz nao deu certo ficou o numero binário lah
tem como ajudar?
faz o seguinte, coloca como resolvido e abre um novo post ok.