问题
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?
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.
回答1:
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:
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:
http://www.soapui.org/SOAP-and-WSDL/adding-headers-and-attachments.html
回答2:
Read the file as an stream and send it over the wire. That's all.
回答3:
At server side send encode string and Base64.decode file string
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(); } } }
来源:https://stackoverflow.com/questions/12571593/how-to-upload-a-file-to-a-soap-web-service