在套接字上读取JPEG流会产生空字符

时间:2014-03-07 08:42:31

标签: java android sockets inputstream fileinputstream

我正在使用此代码在.jpg上阅读InputStream文件,但我在收到一些文字后收到了 NULNUL ... n 信息流。我正在阅读这个文件link to file和我收到的文件链接,链接是Written File link

    while ((ret = input.read(imageCharArray)) != -1) {
    packet.append(new String(imageCharArray, 0, ret));

    totRead += ret;
            imageCharArray = new char[4096];
       }


   file = new File(
     Environment.getExternalStorageDirectory()
       + "/FileName_/"
       + m_httpParser.filename + ".jpg");
   PrintWriter printWriter = new PrintWriter(file);
   // outputStream = new FileOutputStream(file);  //also Used FileoutputStream for writting
   // outputStream.write(packet.toString().getBytes());//
   // ,
   printWriter.write(packet.toString());
   // outputStream.close();
   printWriter.close();
 }

我也尝试了FileoutputStream,但在我的代码中也注意到了这一点。

修改  我也用过这个。我有一个内容长度字段,我正在阅读和写作

    byte[] bytes = new byte[1024];
int totalReadLength = 0;

// read untill we have bytes
while ((read = inputStream.read(bytes)) != -1
        && contentLength >= (totalReadLength)) {

    outputStream.write(bytes, 0, read);
    totalReadLength += read;
    System.out.println(" read size ======= "
            + read + " totalReadLength = "
            + totalReadLength);

}

3 个答案:

答案 0 :(得分:2)

String不是二进制数据的容器,PrintWriter不是编写它的方法。摆脱所有, all,字节和字符串之间的转换,反之亦然,,只需使用输入和输出流传输字节:

while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}

如果需要约束从输入读取的字节数,则必须在调用read()之前执行,并且还必须正确约束read():

while (total < length && (count = in.read(buffer, 0, length-total > buffer.length ? buffer.length: (int)(length-total))) > 0)
{
    total += count;
    out.write(buffer, 0, count);
}

答案 1 :(得分:1)

我在Nexus4中对它进行了测试,它对我有用。以下是我尝试过的代码片段:

public void saveImage(String urlPath)throws Exception{ 
        String fileName = "kumar.jpg";
        File folder = new File("/sdcard/MyImages/");
        // have the object build the directory structure, if needed.
        folder.mkdirs();


        final File output = new File(folder,
                fileName);
        if (output.exists()) {
            output.delete();
        }

        InputStream stream = null;
        FileOutputStream fos = null;
        try {

            URL url = new URL(urlPath);
            stream = url.openConnection().getInputStream();
//          InputStreamReader reader = new InputStreamReader(stream);
            DataInputStream dis = new DataInputStream(url.openConnection().getInputStream());
            byte[] fileData = new byte[url.openConnection().getContentLength()];
             for (int x = 0; x < fileData.length; x++) { // fill byte array with bytes from the data input stream
                    fileData[x] = dis.readByte();

                }
             dis.close();
            fos = new FileOutputStream(output.getPath());
            fos.write(fileData);



        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

只需在后台线程中调用上述函数并传递您的网址即可。它肯定会起作用。如果有帮助,请告诉我。

答案 2 :(得分:-1)

您可以查看以下代码。

 destinationFile = new File(
 Environment.getExternalStorageDirectory()
   + "/FileName_/"
   + m_httpParser.filename + ".jpg");

BufferedOutputStream buffer = new BufferedOutputStream(new FileOutputStream(destinationFile));
byte byt[] = new byte[1024];
int i;
for (long l = 0L; (i = input.read(byt)) != -1; l += i ) {
   buffer.write(byt, 0, i);
}
buffer.close();