Skip to content Skip to sidebar Skip to footer

Android Add Filename To Bytestream

im trying to send a file in android through sockets. i want to add the filename with the bytestream and then send it to the server. how do i do that? and then how do i seperate the

Solution 1:

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.

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.

MIME data format 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 HttpMime

Below is the rough code to format the data using MIME data and send it through Socket

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();

Note that you would need org.apache.http.entity.mime.MultipartEntity and org.apache.http.entity.mime.content.InputStreamBody from HttpMime project. On the server, you need MIME parser that would get back the filename and all the bytes content

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.

Below is the sample code using MimeBodyPart that is part of JavaMail.

MimeMultipartmultiPartMessage=newMimeMultipart(newDataSource() {
    @Overridepublic String getContentType() {
        // this could be anything need be, this is just my test case and illustrationreturn"image/jpeg";
    }

    @Overridepublic InputStream getInputStream()throws IOException {
        // socket is the socket that you get from Socket.accept()BufferedInputStreaminputStream=newBufferedInputStream(socket.getInputStream());
        return inputStream;
    }

    @Overridepublic String getName() {
        return"socketDataSource";
    }

    @Overridepublic OutputStream getOutputStream()throws IOException {
        return socket.getOutputStream();
    }
});

// get the first body of the multipart messageBodyPartbodyPart= multiPartMessage.getBodyPart(0);

// get the filename back from the messageStringfilename= bodyPart.getFileName();

// get the inputstream backInputStreambodyInputStream= bodyPart.getInputStream();

// do what you need to do here....

You could download JavaMail from Oracle Website which also has dependency on Java Activation Framework

Post a Comment for "Android Add Filename To Bytestream"