将文件从cordova App上传到Struts2服务

时间:2015-01-26 14:57:12

标签: android ios cordova struts2

美好的一天,到目前为止,我正在开发一个带有cordova的应用程序,我在上传时遇到问题,但是没有实现它的工作。

我在网络搜索中搜索以找到问题,但我无法理解,当cordova应用程序调用服务(struts2服务)时,文件始终为null并使服务崩溃。

这是cordova应用程序代码:

enviarLogs: function(archivo, url){
    log.info("inicio-fileManager.enviarArchivo");

    //obtenemos el archivo que deseamos enviar
    log.logFolder.getFile(archivo,{create: false, exclusive:false},
        function logCapturado(adjunto){
            log.info("fileManager.enviarArchivo: archivo encontrado, preparando envio");
            //notificaciones.alertar("Salida URI: "+encodeURI(adjunto.fullPath));
            //notificaciones.alertar("Salida URI: "+encodeURI(adjunto.toURL()));
            //var location = "/data/data/ve.com.megasoft.MobilePOS/files/logs/MobilePOSLOGS2015-01-26.txt";

            //opciones del archivo
            var fileOptions = new FileUploadOptions();
            fileOptions.fileKey ="myFile";
            fileOptions.fileName = archivo;
            fileOptions.mimeType = "text/plain";
            //fileOptions.chunkedMode = false;
            //fileOptions.headers = {
                //Connection: "close"
            //};

            //identificacion de procedencia del archivo
            var info = new Object();
            info.value1 = "test";
            info.value2 = "Case";
            fileOptions.params = info;

            //realizamos el envio del archivo
            var ft = new FileTransfer();
            ft.upload(
                //location,
                adjunto.toURL(),
                encodeURI(url),
                fileManager.success,
                fileManager.fail, 
                fileOptions,
                true
            );
        },
        function logNoCapturado(error){
            log.error("fileManager.enviarArchivo: Archivo no encontrado, cancelando envio");
            notificaciones.alertar(
                "no se encuentra el log solicitado, no se realizara el envio de informacion",
                undefined,
                "Error"
            );
        }
    );

    log.info("fin-fileManager.enviarArchivo");
}

以下是struts2服务中的操作:

package ve.com.megasoft;

import java.io.File;
import org.apache.commons.io.FileUtils;
import java.io.IOException; 

import com.opensymphony.xwork2.ActionSupport;

public class UploadFile extends ActionSupport{

private static final long serialVersionUID = 1L;
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath;

public String execute() {
  /* Copy file to a safe location */
  destPath = "C:\\test";

  try{
     System.out.println("Src File name: " + myFile);
     System.out.println("Dst File name: " + myFileFileName);

     File destFile  = new File(destPath, myFileFileName);
     FileUtils.copyFile(myFile, destFile);

  }catch(IOException e){
     e.printStackTrace();
     return ERROR;
  }

  return SUCCESS;
}
   public File getMyFile() {
      return myFile;
   }
   public void setMyFile(File myFile) {
      this.myFile = myFile;
   }
   public String getMyFileContentType() {
      return myFileContentType;
   }
   public void setMyFileContentType(String myFileContentType) {
      this.myFileContentType = myFileContentType;
   }
   public String getMyFileFileName() {
      return myFileFileName;
   }
   public void setMyFileFileName(String myFileFileName) {
      this.myFileFileName = myFileFileName;
   }

}

感谢您的帮助,希望您能帮助我。

编辑:添加了包:

<package name="default" extends="struts-default,json-default">
<!--         tipos de resultados -->
        <result-types>
            <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
            <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
        </result-types>

<!--         interceptores -->
        <interceptors>
            <interceptor-stack name="defaultStack">
                <interceptor-ref name="json">
                    <param name="enableSMD">true</param>

                 </interceptor-ref>
            </interceptor-stack>
        </interceptors>

        <action name="">
            <result>/WEB-INF/jsp/index.jsp</result>
        </action>
       <action name="strutsAction" class="ve.com.megasoft.StrutsAction">
          <result name="error">/WEB-INF/jsp/index.jsp</result>
          <result name="input">/WEB-INF/jsp/index.jsp</result>
          <result name="success">/WEB-INF/jsp/success.jsp</result>          
       </action>

<!--        <action name="upload" class="ve.com.megasoft.UploadFile"> -->
<!--           <result name="success">/WEB-INF/jsp/success.jsp</result> -->
<!--           <result name="error">/WEB-INF/jsp/error.jsp</result> -->
<!--       </action> -->
        <action name="upload" class="ve.com.megasoft.UploadFile">
            <result type="json" >
                <param name="excludeNullProperties">true</param>
            </result>
       </action>
    </package>

EDIT2:使用来自struts2 App的控制台params extracte进行更新

Defaulting to javax.servlet.context.tempdir
param: value1 Value: test
param: value2 Value: Case
ParamMap: {value1=[Ljava.lang.String;@527b280, value2=[Ljava.lang.String;@6bbe9081}
Attributo: __cleanup_recursion_counter Value: null
Attributo: struts.actionMapping Value: null
Attributo: struts.valueStack Value: null
content Length: 24514
content type: multipart/form-data; boundary=+++++
Src File name: null

输出来自ddms

upload file:///data/data/ve.com.megasoft.MobilePOS/files/logs/MobilePOSLOGS2015-01-26.txt to http://192.168.1.130:8080/LearnCodeStruct2/upload
fileKey: myFile
fileName: MobilePOSLOGS2015-01-26.txt
mimeType: multipart/form-data
params: {"value1":"test","value2":"Case"}
trustEveryone: true
chunkedMode: false
headers: {"Connection":"close"}
objectId: 3
httpMethod: POST
Content Length: 24514
Sent 24514 of 24514


<html>
<head>
    <title>Struts Problem Report</title>
    <style>
        pre {
            margin: 0;
            padding: 0;
        }    
    </style>
</head>
<body>
    <h2>Struts Problem Report</h2>
    <p>
    Struts has detected an unhandled exception:
    </p>


<div id="exception-info">
<table>
    <tr>
        <td><strong>Messages</strong>:</td>
        <td>
        </td>
    </tr>

</table>
</div>


<div id="stacktraces">
<hr />
<h3>Stacktraces</h3>
<div class="stacktrace" style="padding-left: 0em">
    <strong>java.lang.NullPointerException</strong>
    <div>
    <pre>
    java.io.File.&lt;init&gt;(Unknown Source)
    ve.com.megasoft.UploadFile.execute(UploadFile.java:67)
    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)
    ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:870)
    ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
    ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
    com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
    com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
    ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
    ognl.ASTMethod.getValueBody(ASTMethod.java:90)
    ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
    ognl.SimpleNode.getValue(SimpleNode.java:258)
    ognl.Ognl.getValue(Ognl.java:494)
    ognl.Ognl.getValue(Ognl.java:458)
    com.opensymphony.xwork2.ognl.OgnlUtil$2.execute(OgnlUtil.java:309)
    com.opensymphony.xwork2.ognl.OgnlUtil.compileAndExecute(OgnlUtil.java:340)
    com.opensymphony.xwork2.ognl.OgnlUtil.getValue(OgnlUtil.java:307)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:423)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:287)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:250)
    org.apache.struts2.json.JSONInterceptor.intercept(JSONInterceptor.java:172)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
    </pre>
    </div>
</div>
</div>

<div class="footer">
<hr />
<p>
You are seeing this page because development mode is enabled.  Development mode, or devMode, enables extra
debugging behaviors and reports to assist developers.  To disable this mode, set:
<pre>
  struts.devMode=false
</pre>
in your <code>WEB-INF/classes/struts.properties</code> file.
</p>
</div>
</body>
</html> 

0 个答案:

没有答案