java 如何将文件上传到 SOAP Web 服务?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12571593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upload a file to a SOAP web service?
提问by Kalina
I have a .p12 file that I want to upload to a SOAP web service, so that my application can make requests to get it later. How do I upload this file?
我有一个 .p12 文件,我想将其上传到 SOAP Web 服务,以便我的应用程序可以在以后发出请求以获取它。我如何上传这个文件?
I don't necessarily want this in my application code, since it's something I only want to do once. But, if it matters, I am using Java.
我不一定希望在我的应用程序代码中使用它,因为这是我只想做一次的事情。但是,如果重要的话,我正在使用 Java。
采纳答案by davidfmatheson
You should take a look at MTOM, if the service supports it. If it accepts file attachments, then it probably uses MTOM. Not sure what you mean by keeping it out of application code, but how you go about creating and sending an attachment depends on what web service platform you're using. Here's the latest documentation on sending attachments in Java's JAX-WS:
如果服务支持,您应该查看 MTOM。如果它接受文件附件,则它可能使用 MTOM。不确定将它保留在应用程序代码之外是什么意思,但是如何创建和发送附件取决于您使用的 Web 服务平台。这是有关在 Java 的 JAX-WS 中发送附件的最新文档:
http://metro.java.net/guide/ch06.html#binary-attachments-mtom
http://metro.java.net/guide/ch06.html#binary-attachments-mtom
If you want to see what is actually happening in a language-independent manner, then grab SoapUI and set it up to send your attachment with MTOM:
如果您想以独立于语言的方式查看实际发生的情况,请获取 SoapUI 并将其设置为使用 MTOM 发送您的附件:
http://www.soapui.org/SOAP-and-WSDL/adding-headers-and-attachments.html
http://www.soapui.org/SOAP-and-WSDL/adding-headers-and-attachments.html
回答by pruthwiraj.kadam
At server side send encode string and Base64.decode file string
在服务器端发送编码字符串和 Base64.decode 文件字符串
String strAttachmentCoded = ""; private int PICK_PDF_REQUEST = 1; Uri filePath; @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_PDF_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) { filePath = data.getData(); File uploadFile = new File(filePath.toString()); URI uri = URI.create(uploadFile.getPath()); try { if (uploadFile != null) { File uploadFile1 = new File(uri); FileInputStream objFileIS = new FileInputStream(uploadFile1); ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream(); byte[] byteBufferString = new byte[1024]; int readNum; readNum = objFileIS.read(byteBufferString); while (readNum != -1) { Log.v(" ", "" + readNum); objByteArrayOS.write(byteBufferString, 0, readNum); // system.out.println("read " + readNum + " bytes,"); readNum = objFileIS.read(byteBufferString); } byte[] byteBinaryData = Base64.encode(objByteArrayOS.toByteArray(), Base64.DEFAULT); strAttachmentCoded = String.valueOf(byteBinaryData); } } catch (Exception e) { e.printStackTrace(); } } }
String strAttachmentCoded = ""; private int PICK_PDF_REQUEST = 1; Uri filePath; @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_PDF_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) { filePath = data.getData(); File uploadFile = new File(filePath.toString()); URI uri = URI.create(uploadFile.getPath()); try { if (uploadFile != null) { File uploadFile1 = new File(uri); FileInputStream objFileIS = new FileInputStream(uploadFile1); ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream(); byte[] byteBufferString = new byte[1024]; int readNum; readNum = objFileIS.read(byteBufferString); while (readNum != -1) { Log.v(" ", "" + readNum); objByteArrayOS.write(byteBufferString, 0, readNum); // system.out.println("read " + readNum + " bytes,"); readNum = objFileIS.read(byteBufferString); } byte[] byteBinaryData = Base64.encode(objByteArrayOS.toByteArray(), Base64.DEFAULT); strAttachmentCoded = String.valueOf(byteBinaryData); } } catch (Exception e) { e.printStackTrace(); } } }
回答by Oscar
Read the file as an stream and send it over the wire. That's all.
将文件作为流读取并通过网络发送。就这样。