Estou tentando fazer um upload de uma imagem na minha aplicacao com Struts, aki estao meus codigos:
CategoriaForm
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class CategoriaForm extends ActionForm
{
private FormFile imagem1;
public FormFile getImagem1() {
return imagem1;
}
public void setImagem1(FormFile imagem1) {
this.imagem1 = imagem1;
}
public void reset(ActionMapping arg0, HttpServletRequest arg1) {
// TODO Auto-generated method stub
super.reset(arg0, arg1);
this.imagem1 = null;
}
public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) {
// TODO Auto-generated method stub
return super.validate(arg0, arg1);
}
}
CategoriaAction
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileUpload;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import com.teste.CategoriaForm;
public class CategoriaAction extends DispatchAction {
public ActionForward adicionar(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
CategoriaForm cf = (CategoriaForm) form;
ActionErrors errors = new ActionErrors();
boolean isMultipart = FileUpload.isMultipartContent(request);
boolean uploadDone = false;
if (cf.getImagem1().getFileName() == null || cf.getImagem1().getFileName().trim().length() == 0){
errors.add("categoria", new ActionError("categoria.imagem1"));
saveErrors(request, errors);
}
if (isMultipart) {
String strFilePath = getServlet().getServletContext().getRealPath("/") + "\\img\\upload";
FormFile formFile = cf.getImagem1();
String strFileName = formFile.getFileName();
byte dataFile[] = formFile.getFileData();
File file = new File(strFilePath + "\\" + strFileName);
OutputStream out = new FileOutputStream(file);
out.write(dataFile);
out.close();
uploadDone = true;
}
else {
errors.add("categoria", new ActionError("categoria.requisicao_upload"));
saveErrors(request, errors);
}
if (!uploadDone) {
errors.add("categoria", new ActionError("categoria.upload_nao_realizado"));
saveErrors(request, errors);
}
if(errors.size() != 0){
return mapping.findForward("erro");
}
else{
}
return mapping.findForward("sucesso");
}
}
index.jsp
<html:form action='index.jsp?action=adicionar'>
<html:file property="imagem1" value="" />
</html:form>
E está me dando o seguinte erro abaixo. O que poderá ser? O erro está apontando para a linha onde está o primeiro if…na classe CategoriaAction
WARNING: Unhandled Exception thrown: class java.lang.NullPointerException
10/06/2007 10:27:27 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet action threw exception
java.lang.NullPointerException
at com.teste.CategoriaAction.incluir(CategoriaAction.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)
10/06/2007 10:27:27 org.apache.struts.util.PropertyMessageResources <init>
INFO: Initializing, config='org.apache.struts.taglib.html.LocalStrings', returnNull=true