通过Socket或HTTP在Android设备和桌面之间传输文件

时间:2012-04-21 03:21:16

标签: java android file sockets transmission

我有自定义socket客户端服务器数据(文件或文本)传输代码。现在当我传输二进制文件时,一些字节转换为超出范围的字符。所以我用十六进制字符串发送它们。这样可行。但对于另一个问题,这不是解决方案。这也存在性能问题。

我从Java code To convert byte to Hexadecimal获得了帮助。

当我从网上下载图像时,同样的事情发生了。一些字节变成了别的东西。我已经按字节比较了字节数。 转换为字符串显示?而不是符号。我试过读者和字节数组输入流。我在网上尝试了所有的例子。我可以做的错误是什么?

我的代码将字节保存到文件:

void saveFile(String strFileName){  


 try{  
         URL url = new URL(strImageRoot + strFileName);  
         BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));  
         BufferedWriter bw = new BufferedWriter(new FileWriter(strImageDownloadPath + strFileName));  
         String line = null;  
         while ( (line = reader.readLine()) != null) {  
            bw.write(line);  
         }  
     }catch(FileNotFoundException fnfe){  
        System.out.println("FileNotFoundException occured!!!");  
     }catch(IOException ioe){  
     }catch(Exception e){  
        System.out.println("Exception occured : " + e);  
     }finally{  
        System.out.println("Image downloaded!!!");  
     }  
}   

3 个答案:

答案 0 :(得分:1)

我在构建Socket客户端服务器应用程序时遇到了类似的问题。字节将是一些奇怪的字符,我尝试各种各样的东西来尝试和比较它们。然后我遇到了一个讨论,其中some1向我指出我应该使用datainputstream,dataoutstream并让它进行到字节的转换。这完全适合我。我从来没有触及过这些字节。

答案 1 :(得分:1)

使用此代码

           File root = android.os.Environment.getExternalStorageDirectory();               

           File dir = new File (root.getAbsolutePath() + "/image");
           if(dir.exists()==false) {
                dir.mkdirs();
           }

           URL url = new URL("http://4.bp.blogspot.com/-zqJs1fVcfeY/TiZM7e-pFqI/AAAAAAAABjo/aKTtTDTCgKU/s1600/Final-Fantasy-X-Night-Sky-881.jpg");
           //URL url = new URL(DownloadUrl);
           //you can write here any link
           File file = new File(dir,"Final-Fantasy-X-Night-Sky-881.jpg");



           long startTime = System.currentTimeMillis();


            //Open a connection to that URL. 
           URLConnection ucon = url.openConnection();


            //* Define InputStreams to read from the URLConnection.

           InputStream is = ucon.getInputStream();
           BufferedInputStream bis = new BufferedInputStream(is);


            //* Read bytes to the Buffer until there is nothing more to read(-1).

           ByteArrayBuffer baf = new ByteArrayBuffer(6000);
           int current = 0;
           while ((current = bis.read()) != -1) {
              baf.append((byte) current);
           }


            //Convert the Bytes read to a String. 
           FileOutputStream fos = new FileOutputStream(file);
           fos.write(baf.toByteArray());
           fos.flush();
           fos.close();

答案 2 :(得分:1)

您应该借助以下链接:How to encode decode in base64 in Android

您可以通过编码到Base64中来将从文件获得的字节数组作为字符串发送。这也减少了传输的数据量。

在接收端,只需使用Base64解码字符串并获得字节数组即可。

然后,您可以使用@Deepak Swami解决方案在文件中保存字节。

我最近发现PHP服务API不知道什么是字节数组。任何String可以同时为字节流,因此API要求request参数中为Base64字符串。请查看帖子:

String to byte array in php

Passing base64 encoded strings in URL

因此,Base64非常重要,因为它还允许您还保存首选项中的字节数组,如果必须使用序列化在网络上发送文件数据,则可以提高性能。

快乐编码:-)