1

Hi I have this problem whit my code, i tried upload file to server and keep on database.

The first version was the same code without fileupload, and all worked good.

struts.xml

<action name="Crear" class="actions.AccionAnadirCliente">
    <interceptor-ref name="fileUpload">
        <param name="maximunSize">1048576</param>
        <param name="allowedTypes">image/png,image/gif,image/jpg,image/jpeg</param>
    </interceptor-ref>  
    <result name="input">/paginaBienvenida.jsp</result>
    <result name="success">/nuevoCliente.jsp</result>
</action>

now, the execute on accionAnadirCliente

public String execute() throws Exception {
    String filePath = "C:\\Users\\alumno\\Desktop\\DWES_JAVA\\imagenes";
    //crea un fichero
    File fileToCreate = new File(filePath, this.fileFileName);
    String rutaCompleta=filePath+ this.fileFileName;
    //volcado de datos.
    FileUtils.copyFile(this.file, fileToCreate);


    Cliente cliente = new Cliente();
    System.out.println(getNombre());

    cliente.setNombre(getNombre()); 
    cliente.setEdad(getEdad());
    cliente.setEmail(getEmail());
    cliente.setTelefono(getTelefono()); 
    cliente.setRuta(rutaCompleta);

    InterfaceClienteDAO clienteDAO = FactoriaDAO.getAlumnoDAO("MySQL");

    clienteDAO.insertarCliente(cliente);    

    return SUCCESS;

}

bean Cliente

private String nombre;
private String email;
private String telefono;
private int edad;
private String ruta;

and getters and setters.

ClienteDAO

public void insertarCliente(Cliente c){

    try {
        db.abrirConexion();
        String query = "insert into cliente values('" + c.getNombre() + "','"
                + c.getEmail() + "','" + c.getTelefono() + "',"
                + c.getEdad() + ","+c.getRuta()+" )";
        db.ejecutarUpdate(query);
        db.commit();
        db.cerrarConexion();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

nuevoCliente.jsp only contains information.

Now the exception.

Struts Problem Report

Struts has detected an unhandled exception:

Messages:   
Stacktraces

java.lang.NullPointerException
    java.io.File.(Unknown Source)
    actions.AccionAnadirCliente.execute(AccionAnadirCliente.java:38)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:325)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:562)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    java.lang.Thread.run(Unknown Source)

thanks, Ivan.

edit: the exception line is this

File fileToCreate = new File(filePath, this.fileFileName);
4

1 回答 1

1

您只包含文件上传拦截器,而您需要包含整个堆栈(默认堆栈,或您创建的自定义堆栈):

<action name="Crear" class="actions.AccionAnadirCliente">

    <interceptor-ref name="defaultStack">
        <param name="fileUpload.maximumSize">1048576</param>
        <param name="fileUpload.allowedTypes">
            image/png,image/gif,image/jpg,image/jpeg
        </param>
    </interceptor-ref>  

    <result name="input">/paginaBienvenida.jsp</result>
    <result name="success">/nuevoCliente.jsp</result>
</action>

您的第一个参数中也有一个错字,maximum不是maximun.

于 2014-02-14T17:14:42.823 回答