gpt4 book ai didi

java - 通过Web服务从服务器向客户端发送pdf文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:40:10 24 4
gpt4 key购买 nike

由于我是 Web 服务的新手,您能告诉我问题的答案吗?我的问题是

I want to implement a web service which sends pdf files to client as the client calls this web service.

请有人用好的代码或解释帮助我。

现在有人可以解决我的错误吗。

12-23 09:42:48.429: DEBUG/installd(32): DexInv: --- BEGIN '/data/app/vmdl33143.tmp' --- 12-23 09:42:51.708: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlPullParser;' has an earlier definition; blocking out
12-23 09:42:51.730: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/io/KXmlParser;' has an earlier definition; blocking out
12-23 09:42:51.749: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlSerializer;' has an earlier definition; blocking out
12-23 09:42:51.759: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/io/KXmlSerializer;' has an earlier definition; blocking out
12-23 09:42:51.779: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/kdom/Node;' has an earlier definition; blocking out
12-23 09:42:51.779: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/kdom/Document;' has an earlier definition; blocking out
12-23 09:42:51.819: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/kdom/Element;' has an earlier definition; blocking out
12-23 09:42:51.830: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/Wbxml;' has an earlier definition; blocking out
12-23 09:42:51.840: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/WbxmlParser;' has an earlier definition; blocking out
12-23 09:42:51.900: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/WbxmlSerializer;' has an earlier definition; blocking out
12-23 09:42:51.910: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/syncml/SyncML;' has an earlier definition; blocking out
12-23 09:42:51.910: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/wml/Wml;' has an earlier definition; blocking out
12-23 09:42:51.929: DEBUG/dalvikvm(328): DexOpt: 'Lorg/kxml2/wap/wv/WV;' has an earlier definition; blocking out
12-23 09:42:51.929: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlPullParserException;' has an earlier definition; blocking out
12-23 09:42:51.949: DEBUG/dalvikvm(328): DexOpt: 'Lorg/xmlpull/v1/XmlPullParserFactory;' has an earlier definition; blocking out
12-23 09:42:53.299: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.319: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.329: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.350: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.350: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.370: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.499: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.560: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
12-23 09:42:53.570: INFO/dalvikvm(328): DexOpt: not resolving ambiguous class 'Lorg/kxml2/io/KXmlSerializer;'
12-23 09:42:53.659: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParser;': multiple definitions
12-23 09:42:53.668: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/io/KXmlParser;': multiple definitions
12-23 09:42:53.668: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlSerializer;': multiple definitions
12-23 09:42:53.668: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/io/KXmlSerializer;': multiple definitions
12-23 09:42:53.680: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/kdom/Node;': multiple definitions
12-23 09:42:53.690: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/kdom/Document;': multiple definitions
12-23 09:42:53.709: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/kdom/Element;': multiple definitions
12-23 09:42:53.709: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/Wbxml;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/WbxmlParser;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/WbxmlSerializer;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/syncml/SyncML;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/wml/Wml;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/kxml2/wap/wv/WV;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParserException;': multiple definitions
12-23 09:42:53.719: DEBUG/dalvikvm(328): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParserFactory;': multiple definitions
12-23 09:42:53.829: DEBUG/dalvikvm(328): DexOpt: load 867ms, verify 1752ms, opt 108ms
12-23 09:42:54.029: DEBUG/installd(32): DexInv: --- END '/data/app/vmdl33143.tmp'(success) ---

这是android模拟器DDMS的错误。

现在我的网络服务和代码如下所示。

代码:-

package com.uks.webservice;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.Remote;

public interface SendPDF extends Remote {

public String[] sendPDF() throws FileNotFoundException, IOException,
Exception;
}

Web 服务的主类已创建:-

package com.uks.webservice;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;

import sun.misc.BASE64Encoder;

public class SendPDFImpl implements SendPDF {

@Override
public String[] sendPDF() {

String[] pdfFile = new String[2];
BASE64Encoder encoder = new BASE64Encoder();
File file = new File(
"C:/eclipse/workspace/AIPWebService/src/test.pdf");
DataHandler dh = new DataHandler(new FileDataSource(file));
String readLine = null;
byte[] data = new byte[(int) file.length()];
int offset = 0;
int numRead = 0;
InputStream stream = null;
try {
System.out.println("Web Service Called Successfully");
stream = dh.getInputStream();
// BufferedReader bufferedReader = new BufferedReader(
// new InputStreamReader(stream));
while (offset < data.length
&& (numRead = stream.read(data, offset, data.length
- offset)) >= 0) {
offset += numRead;
}
readLine = encoder.encode(data);
// while ((readLine = bufferedReader.readLine()) != null) {
System.out.println("'Reading File............................");
System.out.println("\n");
System.out.println("Data Reading Successful");

pdfFile[0] = file.getName();
pdfFile[1] = readLine;

stream.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return pdfFile;
}
}

网络服务:-

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://webservice.uks.com"
xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://webservice.uks.com"
xmlns:intf="http://webservice.uks.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--
WSDL created by Apache Axis version: 1.4 Built on Apr 22, 2006
(06:55:48 PDT)
-->
<wsdl:types>
<schema elementFormDefault="qualified" targetNamespace="http://webservice.uks.com"
xmlns="http://www.w3.org/2001/XMLSchema">
<element name="sendPDF">
<complexType />
</element>
<element name="sendPDFResponse">
<complexType>
<sequence>
<element maxOccurs="unbounded" name="sendPDFReturn" type="xsd:String" />
</sequence>
</complexType>
</element>
</schema>
</wsdl:types>

<wsdl:message name="sendPDFRequest">

<wsdl:part element="impl:sendPDF" name="parameters" />

</wsdl:message>

<wsdl:message name="sendPDFResponse">

<wsdl:part element="impl:sendPDFResponse" name="parameters" />

</wsdl:message>

<wsdl:portType name="SendPDFImpl">

<wsdl:operation name="sendPDF">

<wsdl:input message="impl:sendPDFRequest" name="sendPDFRequest" />

<wsdl:output message="impl:sendPDFResponse" name="sendPDFResponse" />

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name="SendPDFImplSoapBinding" type="impl:SendPDFImpl">

<wsdlsoap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />

<wsdl:operation name="sendPDF">

<wsdlsoap:operation soapAction="" />

<wsdl:input name="sendPDFRequest">

<wsdlsoap:body use="literal" />

</wsdl:input>

<wsdl:output name="sendPDFResponse">

<wsdlsoap:body use="literal" />

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name="SendPDFImplService">

<wsdl:port binding="impl:SendPDFImplSoapBinding" name="SendPDFImpl">

<wsdlsoap:address
location="http://localhost:8080/AIPWebService/services/SendPDFImpl" />

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

现在是我的 Android 代码。

package com.uks.android.webservice;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class PDFActivity extends Activity {
private final String METHOD_NAME = "sendPDF";
private final String NAMESPACE = "http://webservice.uks.com/";
private final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private final String URL = "http://192.168.1.123:8080/AIPWebService/services/SendPDFImpl";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textViewOne = (TextView) findViewById(R.id.textViewOne);
TextView textViewTwo = (TextView) findViewById(R.id.textViewTwo);
try {
SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(soapObject);

textViewOne.setText("Web Service Started");
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);

httpTransport.call(SOAP_ACTION, envelope);

SoapObject result = (SoapObject) envelope.getResponse();
textViewTwo.setText(result.toString());
// Object result = envelope.getResponse();
// FileOutputStream outputStream = openFileOutput(name, mode)

} catch (Exception e) {
e.printStackTrace();
}
}
}

请帮帮我。

提前致谢。

最佳答案

您需要将文件转换为字节数组并将其传递给服务器上的服务,执行相反的操作以取回文件。

关于java - 通过Web服务从服务器向客户端发送pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4507201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com