在套接字上编码文件

时间:2017-10-27 22:49:50

标签: android sockets encoding utf-8

我一直在这方面花钱,似乎无法解决这个问题。我是自学成才,对此不太熟悉,请原谅我,如果这是一个补救问题。我将数据从Android发送到.Net服务器。数据在编码时变得越来越糟,我知道我只是不确定如何修复。我正在使用此处的.Net Async服务器示例代码:Microsoft Async Sample

我的Android客户端代码是:

try {
            final Socket sock = new Socket();
            final int timeOut = (int) TimeUnit.SECONDS.toMillis(5); // 5 sec wait period
            sock.connect(new InetSocketAddress("localhost", 11000), timeOut);

            if (sock.isConnected()==true){
                BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));

                String FileName = "myfile.jpg";

                StringBuilder hd = new StringBuilder();
                try {
                    String FilePath= Environment.getExternalStorageDirectory() + "/mydir/" + FileName;
                    File file = new File(FilePath);
                    FileInputStream is = new FileInputStream(file);
                    byte[] chunk = new byte[40960];
                    int chunkLen = 0;
                    while ((chunkLen = is.read(chunk)) != -1) {
                        //String str = new String(Base64.encodeToString(chunk, Base64.NO_WRAP));
                        //String str = new String(chunk, "ASCII");
                        String str = new String(chunk, "UTF-8");
                        out.write(str);
                    }
                    //out.write(hd.toString());
                } catch (FileNotFoundException fnfE) {
                    // file not found, handle case
                } catch (IOException ioE) {
                    // problem reading, handle case
                }

                out.write("<EOF>");
                out.flush();

                StringBuilder returnString = new StringBuilder();
                String line;
                while ((line = in.readLine()) != null) {
                    returnString.append(line).append('\n');
                }

                out.close();
                in.close();
                sock.close();
            }else{
                sock.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

正如您在评论中所看到的,我尝试过base64和UTF-8。当我这样做时,我在服务器上遇到各种错误。如果我使用Base64,我不会遇到Base64错误(额外填充等)。 UTF8写入文件但它已损坏。当我将它全部作为一个Base64字符串发送时,它可以很好地工作,因为我使用了&#39; Dim data As Byte()= Convert.FromBase64String(FileData)&#39;但正如预期的那样,它会在Android中为大文件抛出内存错误,从而产生分块。我正在发送一些纯ASCII文本,所以我解析出非ASCII的东西来写文件。我是超级卡住,任何帮助将不胜感激。提前谢谢。

2 个答案:

答案 0 :(得分:0)

您根本不必编码。只需使用OutputStream将其直接写为字节。更简单,更快捷,更好。

答案 1 :(得分:-1)

我找到了答案。它太奇怪了,但现在才有意义。

 byte[] chunk = new byte[30000];
                int chunkLen = 0;
                while ((chunkLen = is.read(chunk)) != -1) {
                    String str = new String(Base64.encodeToString(chunk, Base64.NO_WRAP));
                    out.write(str);
                }

我不得不将块大小更改为3的倍数,然后我的base64编码工作得很好。在这里找到并投了票。谢谢'mjv'。 Link to the answer