Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过套接字读取JPEG流会产生空字符_Java_Android_Sockets_Inputstream_Fileinputstream - Fatal编程技术网

Java 通过套接字读取JPEG流会产生空字符

Java 通过套接字读取JPEG流会产生空字符,java,android,sockets,inputstream,fileinputstream,Java,Android,Sockets,Inputstream,Fileinputstream,我正在使用此代码通过InputStream读取.jpg文件,但我在一些文本后收到numul…n流。我正在阅读这个文件和我收到的文件链接,链接是 我也尝试过FileoutputStream,但正如我的代码中所评论的那样,我也尝试过这样做 编辑 我也用过这个。我有一个我正在读写的内容长度字段 byte[] bytes = new byte[1024]; int totalReadLength = 0; // read untill we have bytes while ((read =

我正在使用此代码通过
InputStream
读取
.jpg
文件,但我在一些文本后收到numul…n流。我正在阅读这个文件和我收到的文件链接,链接是

我也尝试过
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);

}

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

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);
}
while(totalbuffer.length?buffer.length:(int)(length total))大于0)
{
总数+=计数;
out.write(缓冲区,0,计数);
}

您可以查看以下代码

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

我在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();
                }
            }
        }

    }
public void saveImage(字符串urlPath)引发异常{
字符串fileName=“kumar.jpg”;
文件夹=新文件(“/sdcard/MyImages/”);
//如果需要,让对象构建目录结构。
folder.mkdirs();
最终文件输出=新文件(文件夹、,
文件名);
if(output.exists()){
output.delete();
}
InputStream=null;
FileOutputStream=null;
试一试{
URL=新URL(URL路径);
stream=url.openConnection().getInputStream();
//InputStreamReader reader=新的InputStreamReader(流);
DataInputStream dis=新的DataInputStream(url.openConnection().getInputStream());
byte[]fileData=新字节[url.openConnection().getContentLength()];
对于(int x=0;x

只需在后台线程中调用上述函数并传递url。肯定会有用的。如果有帮助,请告诉我

您的读取链接不工作。如果您发送字节,它将显示您发送的数据类型,您将能够接收到另一侧显示您的socketserver的字节code@raj它的JPEG数据也比我强。我还建议使用ApacheIOUTIL库来实现这类功能:我曾经使用过这个库,但它又给了我一个损坏的jpeg文件。@你一定是在开玩笑吧。请指出这个答案的问题。@KanakSony那么你一定是误用了它。这是我43年来编程的每个复制操作的基础,包括17年的Java编程。发布您当前的代码。把它编辑成你的问题,你根本没用过。您现在在评论中发布的代码与我在这里提出的建议完全没有关系。这表明您的文件不会超过某个长限制。它不会这样做。它是一个只写变量,从零开始递增,在代码中的任何地方都不会被读取。如果要实现总大小限制,它也不会这样做,因为您必须在最后一次读取时限制读取大小。谢谢。。谢谢你给我提建议。试试这个。。我会让你知道的。当然,请继续:)
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();
                }
            }
        }

    }