当图像发送到服务器时,我收到此错误“java.io.UTFDataFormatException:字节1869周围的格式错误的输入”

时间:2015-12-02 12:15:00

标签: java image networking server client

我有一个客户端服务器应用程序,图像发送成功,但由于标题中的错误,服务器无法完全接收它。这是我的代码:

服务器:public void upload()抛出IOException {

ws.Cells(1,1).Value

// **********************阅读图片********************* ************ //

        DataInputStream dis = new DataInputStream (sock.getInputStream());

        String str,extn = ""; 

        str = dis.readUTF();

        int flag=0,i; 

            for(i=0;i<str.length();i++) 
            { 

                if(str.charAt(i)=='.' || flag==1) 
                { 
                flag=1; 
                extn+=str.charAt(i); 
                }
            } 

运行方法:

           if(extn.equals(".jpg") || extn.equals(".gif")) 
              {             
                File file = new File("RecievedImage"+str); 
                FileOutputStream fout = new FileOutputStream(file);

                //receive and save image from client 
                byte[] readData = new byte[8]; 
                while((i = dis.read(readData)) != -1) 
                {
                    fout.write(readData, 0, i); 
                    if(flag==1) 
                    { 
                    System.out.println("Image Has Been Received"); 
                    flag=0; 
                    } 
                } 
            fout.flush(); 
            fout.close(); 

              }

           executorService.shutdown();
    } 

任何帮助?

0 个答案:

没有答案