Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to package the data to the stream through a some kind of data format. One way to do this is to use the common MIME data format which is commonly used to send attachment in email.</p> <p>I have answered other question related to sending binary via socket using this format in <a href="https://stackoverflow.com/questions/7374481/android-add-filename-to-bytestream/7374554#7374554">the following SO Question - android add filename to bytestream</a>. You could check the accepted answer for that question.</p> <p>For your reference, I just copied the code for sending and receiving through socket from that question below.</p> <pre><code>File f = new File(path); BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() ); String filename=path.substring(path.lastIndexOf("/")+1); // create a multipart message MultipartEntity multipartContent = new MultipartEntity(); // send the file inputstream as data InputStreamBody isb = new InputStreamBody(new FileInputStream(f), "image/jpeg", filename); // add key value pair. The key "imageFile" is arbitrary multipartContent.addPart("imageFile", isb); multipartContent.writeTo(out); out.flush(); out.close(); </code></pre> <p>And the code to read it back below using <a href="http://javamail.kenai.com/nonav/javadocs/index.html?javax/mail/BodyPart.html" rel="nofollow noreferrer">MimeBodyPart</a> that is part of JavaMail. </p> <pre><code> MimeMultipart multiPartMessage = new MimeMultipart(new DataSource() { @Override public String getContentType() { // this could be anything need be, this is just my test case and illustration return "image/jpeg"; } @Override public InputStream getInputStream() throws IOException { // socket is the socket that you get from Socket.accept() BufferedInputStream inputStream = new BufferedInputStream(socket.getInputStream()); return inputStream; } @Override public String getName() { return "socketDataSource"; } @Override public OutputStream getOutputStream() throws IOException { return socket.getOutputStream(); } }); // get the first body of the multipart message BodyPart bodyPart = multiPartMessage.getBodyPart(0); // get the filename back from the message String filename = bodyPart.getFileName(); // get the inputstream back InputStream bodyInputStream = bodyPart.getInputStream(); // do what you need to do here.... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload