Android的文件名添加到字节流 [英] android add filename to bytestream

查看:228
本文介绍了Android的文件名添加到字节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过socket发送文件中的机器人。我想用字节流中添加文件名,然后将它发送到服务器。我怎么做?然后我如何单独接收端的文件名?
这是code发送文件:

  Log.i(SocketOP,SENDFILE-1);
            文件f =新的文件(路径);
            字符串文件名= path.substring(path.lastIndexOf(/)+ 1);
            的System.out.println(文件名:+文件名);
            fin.filename =〜+文件名;
            的BufferedOutputStream出=新的BufferedOutputStream(socket.getOutputStream());            的FileInputStream FILEIN =新的FileInputStream(F);
            Log.i(SocketOP,SENDFILE-2);
            字节[]缓冲区=新的字节[(INT)f.length()];
            的System.out.println(SO SENDFILE f.length();+ f.length());
            INT读取动作= 0;
            而((读取动作= fileIn.read(缓冲液))大于0){
                out.write(缓冲液,0,buffer.length);
                的System.out.println(SO SENDFILE+读取动作+文件名);
            }
            了out.flush();
            out.close();
            fileIn.close();
            Log.i(SocketOP,SENDFILE-3);


解决方案

如果这是你自己的协议,那么你创建的两个部分(文件名和数据)分开的数据包。你需要通过一个特定的边界明确表示分离。

在服务器上,因为你理解了协议,服务器将读取回整个数据包,它会根据给定的边界上的文件名和数据分开。

MIME数据格式使用正是这种数据交换和HTTP协议的广泛应用。如果您使用相同的MIME数据格式,另一个好处是,你可以使用第三方库连接code和德code的数据,如的 HttpMime

下面是粗糙code。使用MIME数据对数据进行格式化,并通过Socket发送

 文件f =新的文件(路径);
的BufferedOutputStream出=新的BufferedOutputStream(socket.getOutputStream());
字符串文件名= path.substring(path.lastIndexOf(/)+ 1);//创建一个多部分消息
MultipartEntity multipartContent =新MultipartEntity();//发送文件的InputStream数据
InputStreamBody ISB =新InputStreamBody(新的FileInputStream(F),图像/ JPEG,文件名);//添加键值对。关键的镜像文件是任意
multipartContent.addPart(镜像文件,ISB);multipartContent.writeTo(出);
了out.flush();
out.close();

请注意,您将需要<一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/MultipartEntity.html\"相对=nofollow> org.apache.http.entity.mime.MultipartEntity 和<一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/InputStreamBody.html\"相对=nofollow> org.apache.http.entity.mime.content.InputStreamBody 从HttpMime项目。在服务器上,你需要MIME解析器会回到文件名和所有字节的内容

要读取的InputStream后面的服务器上,你需要一个类来解析MIME消息。你不应该写自己的解析器MIME是一个受欢迎的消息格式已经除非你想了解MIME消息结构。

下面是使用示例code MimeBodyPart 即JavaMail的的一部分。

 
MimeMultipart的multiPartMessage =新MimeMultipart的(新的DataSource(){
    @覆盖
    公共字符串的getContentType(){
        //这可能是什么需要的话,这只是我的测试案例和插图
        返回到图像/ JPEG;
    }    @覆盖
    公众的InputStream的getInputStream()抛出IOException
        //插座是你从Socket.accept获得插座()
        的BufferedInputStream的InputStream =新的BufferedInputStream(socket.getInputStream());
        返回InputStream的;
    }    @覆盖
    公共字符串的getName(){
        返回socketDataSource
    }    @覆盖
    公众的OutputStream的getOutputStream()抛出IOException
        返回socket.getOutputStream();
    }
});//获取多部分消息的所述第一主体
BodyPart的正文部分= multiPartMessage.getBodyPart(0);//从消息获取的文件名后面
字符串文件名= bodyPart.getFileName();//获取后面的InputStream
InputStream的bodyInputStream = bodyPart.getInputStream();//你需要在这里做什么....

您可以从甲骨文网站其中也有对依赖的Java激活框架

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 filename on the receiving side? this is the code to send file :

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

解决方案

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.


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

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

这篇关于Android的文件名添加到字节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆