Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If this is your own protocol then you create a data packet that separate the two sections (filename and data). You need to denote clearly the separation via a particular boundary.</p> <p>On the server, since you understand the protocol, the server will read back the whole data packet and it will separate the filename and data based on the given boundary.</p> <p><a href="http://en.wikipedia.org/wiki/MIME" rel="nofollow">MIME data format</a> use exactly this kind of data exchange and widely use with HTTP protocol. If you use the same MIME Data Format, another advantage is you could use third party library to encode and decode your data such as <a href="http://hc.apache.org/httpcomponents-client-ga/httpmime/" rel="nofollow">HttpMime</a></p> <p>Below is the rough code to format the data using MIME data and send it through Socket</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>Note that you would need <a href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html" rel="nofollow">org.apache.http.entity.mime.MultipartEntity</a> and <a href="http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/InputStreamBody.html" rel="nofollow">org.apache.http.entity.mime.content.InputStreamBody</a> from HttpMime project. On the server, you need MIME parser that would get back the filename and all the bytes content</p> <p>To read the inputstream back on the server, you would need a class to parse the MIME message. You shouldn't have to write the parser yourself as MIME is a popular message format already unless you want to learn about the MIME message structure.</p> <p>Below is the sample code using <a href="http://javamail.kenai.com/nonav/javadocs/index.html?javax/mail/BodyPart.html" rel="nofollow">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> <p>You could download JavaMail from <a href="http://www.oracle.com/technetwork/java/index-138643.html" rel="nofollow">Oracle Website</a> which also has dependency on <a href="http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html" rel="nofollow">Java Activation Framework</a></p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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