Upload de Arquivo

Bom dia pessoal, estou tentando fazer upload de arquivo através de uns exemplo da net, mas sem sucesso, considerando que o POST está enviando a foto perfeitamente:

meu código é este:

[code]<%@page language=“java” import=“org.apache.commons.fileupload.FileItemFactory” %>
<%@page language=“java” import=“org.apache.commons.fileupload.disk.DiskFileItemFactory” %>
<%@page language=“java” import=“org.apache.commons.fileupload.servlet.ServletFileUpload” %>
<%@page language=“java” import=“org.apache.tomcat.util.http.fileupload.FileItem” %>
<%@page language=“java” import=“org.apache.tomcat.util.http.fileupload.FileUploadException” %>
<%@page language=“java” import=“java.io.File” %>
<%@page language=“java” import=“java.util.List” %>
<%@page language=“java” import=“java.util.Iterator” %>

<%
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);

try {
	List items = upload.parseRequest(request);
	Iterator iter = items.iterator();
	while (iter.hasNext()) {
		FileItem item = (FileItem) iter.next();

		if (item.getFieldName().equals("foto") ) {

			String nome = item.getName();
			StringBuffer bn = new StringBuffer();
			bn.append("imagens/");
			bn.append(nome);
			File uploadedFile = new File(bn.toString());
			item.write(uploadedFile);
		}
	}

} catch (FileUploadException fuex) {
	fuex.getMessage().toString();
} catch (Exception ex) {
	String json = "{msg:'" + ex.getMessage().toString() + "'}";
	
	out.println(json);  
}

%>[/code]

Está caindo aqui:

[code] catch (Exception ex) {
String json = “{msg:’” + ex.getMessage().toString() + “’}”;

	out.println(json);  
}[/code]

e o json retorna isso:

{msg:'org.apache.commons.fileupload.disk.DiskFileItem cannot be cast to org.apache.tomcat.util.http.fileupload.FileItem'}

Obrigado!

por acaso vc esta usando jsp???

Sim

eu usei estes exemplos pra fazer algo parecido pra mim um tempo atrás ve se te ajuda!!

http://javafree.uol.com.br/topic-10203-Upload-arquivos-em-um-JSP-com-o-Jakarta-CommonsFileUpload.html

http://javafree.uol.com.br/viewtopic.jbb?t=851262

faça como ele ao invés de

  FileItemFactory factory = new DiskFileItemFactory();  

use

ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory()); 

Olá Puka, ocorreu erros no construtor das classes:

[code]ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
ServletFileUpload upload = new ServletFileUpload(servletFileUpload);

try {
	List items = upload.parseRequest(request);
	Iterator iter = items.iterator();

…[/code]

Ocorre este erro:

[code]GRAVE: Servlet.service() for servlet [jsp] in context with path [/ExtJava] threw exception [Unable to compile class for JSP:

An error occurred at line: 12 in the jsp file: /teste.jsp
The constructor ServletFileUpload(DiskFileItemFactory) is undefined
9:
10:
11: <%
12: ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
13: ServletFileUpload upload = new ServletFileUpload(servletFileUpload);
14:
15: try {

An error occurred at line: 13 in the jsp file: /teste.jsp
The constructor ServletFileUpload(ServletFileUpload) is undefined
10:
11: <%
12: ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
13: ServletFileUpload upload = new ServletFileUpload(servletFileUpload);
14:
15: try {
16: List items = upload.parseRequest(request);

Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 12 in the jsp file: /teste.jsp
The constructor ServletFileUpload(DiskFileItemFactory) is undefined
9:
10:
11: <%
12: ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
13: ServletFileUpload upload = new ServletFileUpload(servletFileUpload);
14:
15: try {

An error occurred at line: 13 in the jsp file: /teste.jsp
The constructor ServletFileUpload(ServletFileUpload) is undefined
10:
11: <%
12: ServletFileUpload servletFileUpload = new ServletFileUpload(new DiskFileItemFactory());
13: ServletFileUpload upload = new ServletFileUpload(servletFileUpload);
14:
15: try {
16: List items = upload.parseRequest(request);

Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:95)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:367)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:345)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:332)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:594)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:342)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:550)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:380)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)[/code]

[quote=dedetrix]eu usei estes exemplos pra fazer algo parecido pra mim um tempo atrás ve se te ajuda!!

http://javafree.uol.com.br/topic-10203-Upload-arquivos-em-um-JSP-com-o-Jakarta-CommonsFileUpload.html

http://javafree.uol.com.br/viewtopic.jbb?t=851262[/quote]

Obrigado pelos exemplos, mas a maioria dos métodos estão depreciados.

É por isso que prefiro usar o richfaces…

E como usa isso? Existe algum tutorial? Obrigado!

Alguém possui algum tutorial funcional? Obrigado!