Android将文件名添加到字节流



我试图通过套接字在android中发送文件。我想用字节流添加文件名,然后将其发送到服务器。我怎么做呢?然后我如何在接收端分开文件名?这是发送文件的代码:

 Log.i("SocketOP", "sendFILE-1");
            File  f = new File(path);
            String filename=path.substring(path.lastIndexOf("/")+1);
            System.out.println("filename:"+filename); 
            fin.filename = "~"+filename;
            BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );
            FileInputStream fileIn = new FileInputStream(f);
            Log.i("SocketOP", "sendFILE-2");
            byte [] buffer  = new byte [(int)f.length()];
            System.out.println("SO sendFile f.length();" + f.length());
            int bytesRead =0;
            while ((bytesRead = fileIn.read(buffer)) > 0) {
                out.write(buffer, 0, buffer.length);
                System.out.println("SO sendFile" + bytesRead +filename);
            }
            out.flush();
            out.close();
            fileIn.close();
            Log.i("SocketOP", "sendFILE-3");

如果这是您自己的协议,则创建一个数据包,将两个部分(filename和data)分开。您需要通过特定的边界清楚地表示分离。

在服务器端,由于您理解了协议,服务器将回读整个数据包,并根据给定的边界将文件名和数据分开。

MIME数据格式正是利用了这种数据交换方式,并与HTTP协议广泛使用。如果你使用相同的MIME数据格式,另一个好处是你可以使用第三方库来编码和解码你的数据,如HttpMime

下面是使用MIME数据格式化数据并通过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();

注意你需要从HttpMime项目中获取org.apache.http.entity.mime. multiparentenity和org.apache.http.entity.mime.content.InputStreamBody。在服务器上,您需要MIME解析器来返回文件名和所有字节内容

要在服务器上读取输入流,您需要一个类来解析MIME消息。您不应该自己编写解析器,因为MIME已经是一种流行的消息格式,除非您想了解MIME消息结构。

下面是使用JavaMail的一部分MimeBodyPart的示例代码。


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....

你可以从Oracle网站下载JavaMail,它也依赖于Java激活框架

最新更新