IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java ME Discussion :

Comment envoyer un fichier a un web service ?


Sujet :

Java ME

  1. #1
    Battosaiii
    Invité(e)
    Par défaut Comment envoyer un fichier a un web service ?
    Bonjour,
    Je dois modifier un vieux code realise avec Eclipse 2.1 .
    Je voudrais envoyer un fichier a un web service. Je sais comment envoyer un string facilement en J2me. En revanche j'ignore comment envoyer un fichier ?

    J'ai lu sur internet que on peut envoyer des fichiers en encodant les bytes en base64 mais cette solution est lourde. La communication devient lente.

    J'utilise le code suivant pour envoyer des string en java a un web service.
    COmment modifier ce code pour envoyer des fichiers ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
     
    	public String sendRequest() {
    		String outputResponse = "";
    		try {
    			URL url= new URL(WebServicePath); 
     
    			System.out.println("\nWeb Server:");
    			System.out.println(WebServicePath);
     
    			// open an HTTP connection to the web service
    			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
    			connection.setRequestMethod("POST");
    			connection.setDoOutput(true);
    			connection.setDoInput(true);
    			// make it a SOAP request
    			connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
    			connection.setRequestProperty("SOAPAction", XmlNamespace + MethodName);
     
    			// build the SOAP request for GetTime
    			//http://localhost/time/TimeService.asmx?op=GetTime
     
    			String strParameters = "";
    			for (int t = 0; t < ParamNames.size(); t++) {
    				String name = (String) ParamNames.elementAt(t);
    				String data = (String) ParamData.elementAt(t);
    				strParameters = strParameters + "<" + name + ">" + data + "</" + name + ">\n";
    			} 
     
    			String msg =
    			"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
    			"<soap:Envelope " +
    				//" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
    				//" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
    				" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
    				" <soap:Body>\n" +                
    				"   <" + MethodName + " xmlns=\"" + XmlNamespace +"\">\n" +
    					strParameters +        
    				"   </"+ MethodName +"> \n" +                
    				" </soap:Body>\n" +
    			"</soap:Envelope>";
     
    			// send the SOAP request
    			byte[] bytes = msg.getBytes();
    			connection.setRequestProperty("Content-length", String.valueOf(bytes.length));
    			// output
    			System.out.println("\nSOAP call:");
    			System.out.println("Content-type:"+connection.getRequestProperty("Content-type"));
    			System.out.println("Content-length:"+connection.getRequestProperty("Content-length"));
    			System.out.println("SOAPAction:"+connection.getRequestProperty("SOAPAction"));
    			System.out.println(msg);
     
    			OutputStream out = connection.getOutputStream();
    			out.write(bytes);
    			out.close();
    			// read and print the SOAP response
    			BufferedReader in = new BufferedReader(
    							new InputStreamReader(
    							connection.getInputStream()));
    			String inputLine;
     
     
    			System.out.println("\nServer response:");
    			while ((inputLine = in.readLine()) != null)
    			{
    				System.out.println(inputLine);
    				outputResponse+=inputLine;
    			}
    			in.close();
    		} catch (Exception e) {					
    			System.out.println("-- error:" + e.getMessage());
    			return null;
    		} 
    	  return outputResponse;
      }
    merci

  2. #2
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2010
    Messages : 6
    Points : 8
    Points
    8
    Par défaut
    Salut,

    Normalement, je pense qu'il faut utiliser l'array byte (byte []) to envoyer en fichier vers web service. Par exemple, tu définis une classe avec une méthode comme suit (dans le web service):

    public class SendingFile {

    public void send(byte [] b) {
    ....
    }
    }

    Je suppose que tu connaîs générer le web service depuis la classe en Java (si non vois l'Eclipse WTP). Après avoir généré le web service, tu reçois le fichier WSDL, tu peux générer des classes stub qui peut invoquer le web service en J2ME comme le link suivant :

    http://sourceforge.net/projects/ksoap2genstub/

    @+

  3. #3
    Battosaiii
    Invité(e)
    Par défaut
    Merci pour ta suggestion. Je viens de tester.
    Je travaille avec eclipse 2.1 et aussi Jdk 1.42.
    La doc indique minimum JDK 1.6.

    J'ai tente de faire :

    java -cp ksoap2-generating-stub-0.1-SNAPSHOT-jar-with-dependencies.jar;"%JAVA_HOME%\lib\tools.jar" ksoap2.generator.Wsdl2J2me -w "http://localhost:1522/RicohListFoldersSharepointService/Service.asmx?wsdl" -g .\generated

    J'ai plein d'erreur du meme type :
    ksoap2-generating-stub-0.1-SNAPSHOT-J2me-A
    ndroid\___tmp__gen__ksoap\org\tempuri\ServiceSoapStub.java:152: package org.apac
    he.axis.encoding does not exist
    org.apache.axis.encoding.SerializerFactory sf = (org
    .apache.axis.encoding.SerializerFactory)
    ^

    ndroid\___tmp__gen__ksoap\org\tempuri\ServiceSoapStub.java:152: package org.apache.axis.encoding does not exist
    org.apache.axis.encoding.SerializerFactory sf = (org
    .apache.axis.encoding.SerializerFactory)

    Comment resoudre ces erreurs ?

  4. #4
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2010
    Messages : 6
    Points : 8
    Points
    8
    Par défaut
    Tu peux me montrer le fichier WSDL ? Je vais corriger le code s'il existe encore une erreur.

    Kinh

  5. #5
    Battosaiii
    Invité(e)
    Par défaut
    Salut,

    Le fichier WSDL est genere automatiquement par visual studio 2008.
    Voici le WSDL code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
     
      <?xml version="1.0" encoding="utf-8" ?> 
    - <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    - <wsdl:types>
    - <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
    - <s:element name="SendFileByte">
    - <s:complexType>
    - <s:sequence>
      <s:element minOccurs="0" maxOccurs="1" name="folderID" type="s:string" /> 
      <s:element minOccurs="0" maxOccurs="1" name="bytes" type="s:base64Binary" /> 
      </s:sequence>
      </s:complexType>
      </s:element>
    - <s:element name="SendFileByteResponse">
    - <s:complexType>
    - <s:sequence>
      <s:element minOccurs="1" maxOccurs="1" name="SendFileByteResult" type="s:boolean" /> 
      </s:sequence>
      </s:complexType>
      </s:element>
      </s:schema>
      </wsdl:types>
    - <wsdl:message name="SendFileByteSoapIn">
      <wsdl:part name="parameters" element="tns:SendFileByte" /> 
      </wsdl:message>
    - <wsdl:message name="SendFileByteSoapOut">
      <wsdl:part name="parameters" element="tns:SendFileByteResponse" /> 
      </wsdl:message>
    - <wsdl:portType name="ServiceSoap">
    - <wsdl:operation name="SendFileByte">
      <wsdl:input message="tns:SendFileByteSoapIn" /> 
      <wsdl:output message="tns:SendFileByteSoapOut" /> 
      </wsdl:operation>
      </wsdl:portType>
    - <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
      <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    - <wsdl:operation name="SendFileByte">
      <soap:operation soapAction="http://tempuri.org/SendFileByte" style="document" /> 
    - <wsdl:input>
      <soap:body use="literal" /> 
      </wsdl:input>
    - <wsdl:output>
      <soap:body use="literal" /> 
      </wsdl:output>
      </wsdl:operation>
      </wsdl:binding>
    - <wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
      <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
    - <wsdl:operation name="SendFileByte">
      <soap12:operation soapAction="http://tempuri.org/SendFileByte" style="document" /> 
    - <wsdl:input>
      <soap12:body use="literal" /> 
      </wsdl:input>
    - <wsdl:output>
      <soap12:body use="literal" /> 
      </wsdl:output>
      </wsdl:operation>
      </wsdl:binding>
    - <wsdl:service name="Service">
    - <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
      <soap:address location="http://localhost:1522/RicohListFoldersSharepointService/Service.asmx" /> 
      </wsdl:port>
    - <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">
      <soap12:address location="http://localhost:1522/RicohListFoldersSharepointService/Service.asmx" /> 
      </wsdl:port>
      </wsdl:service>
      </wsdl:definitions>
    J'obtiens beaucoup d'erreurs . Voici les dernieres lignes d'erreurs :
    (j'ai supprime une partie du path car c'est trop long.

    \ksoap2-generating-stub-0.1-SNAPSHOT-J2me-A
    ndroid\___tmp__gen__ksoap\org\tempuri\ServiceSoapStub.java:123: package org.apac
    he.axis does not exist
    } catch (org.apache.axis.AxisFault axisFaultException) {
    ^
    Note: \ksoap2-generating-stub-0.1-SNAPSHOT-
    J2me-Android\___tmp__gen__ksoap\org\tempuri\ServiceLocator.java uses unchecked o
    r unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    71 errors
    Exception in thread "main" ksoap2.generator.GeneratorException: Build code: erro
    r
    at ksoap2.generator.WsCompiler.compile(WsCompiler.java:108)
    at ksoap2.generator.WsCompiler.run(WsCompiler.java:78)
    at ksoap2.generator.Wsdl2J2me.run(Wsdl2J2me.java:111)
    at ksoap2.generator.Wsdl2J2me.main(Wsdl2J2me.java:216)

  6. #6
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2010
    Messages : 6
    Points : 8
    Points
    8
    Par défaut
    Salut,

    Tu as raison. Je vais corriger cette erreur avant le weekend prochain. Je vais te signaler si possible.

    @+

  7. #7
    Futur Membre du Club
    Profil pro
    Inscrit en
    Avril 2010
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2010
    Messages : 6
    Points : 8
    Points
    8
    Par défaut
    Salut,

    Je pense que je viens de mettre a jour le fichier .zip. Est-ce que tu pourrais le re-essayer ?

    Merci d'avance,
    Kinh

Discussions similaires

  1. Comment envoyer un fichier vers une adresse web?
    Par iclic dans le forum VB 6 et antérieur
    Réponses: 2
    Dernier message: 11/06/2009, 12h00
  2. [Web Service][SOAP] Envoyer des fichiers volumineux via web services
    Par kaboume dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 06/12/2007, 11h06
  3. Réponses: 14
    Dernier message: 14/03/2007, 19h32
  4. Réponses: 2
    Dernier message: 22/06/2006, 12h09
  5. Serveur/Client UDP: comment envoyer un fichier avec mon code
    Par danje dans le forum Entrée/Sortie
    Réponses: 7
    Dernier message: 21/12/2005, 14h54

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo