Java 无内容套接字编程的android图像接收
我创建了一个应用程序,通过套接字编程将图像从服务器(桌面)发送到客户端(android)……问题是我在客户端(android)获取文件,但没有内容 谁能告诉我有什么问题吗 客户端(Android)Java 无内容套接字编程的android图像接收,java,android,sockets,Java,Android,Sockets,我创建了一个应用程序,通过套接字编程将图像从服务器(桌面)发送到客户端(android)……问题是我在客户端(android)获取文件,但没有内容 谁能告诉我有什么问题吗 客户端(Android) 查看代码,我发现您希望接收一个文件,将其保存到外部存储器并返回该文件的位图。我想这就是你想要做的,但是你的代码,事实上,并没有做到这一点。如果需要,可以使用以下代码来完成该任务。首先,服务器发送4个字节,指示文件大小,然后是文件内容;客户端读取这4个字节,然后读取整个文件,并将读取的每个块保存到磁盘。
查看代码,我发现您希望接收一个文件,将其保存到外部存储器并返回该文件的位图。我想这就是你想要做的,但是你的代码,事实上,并没有做到这一点。如果需要,可以使用以下代码来完成该任务。首先,服务器发送4个字节,指示文件大小,然后是文件内容;客户端读取这4个字节,然后读取整个文件,并将读取的每个块保存到磁盘。最后,它将接收到的文件转换为位图并返回 客户端代码:
public Bitmap receiveFile(InputStream is) throws Exception
{
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.png";
String imageInSD = baseDir + File.separator + fileName;
System.out.println("FILE----------------->" + imageInSD);
// read first 4 bytes containing the file size
byte[] bSize = new byte[4];
is.read(bSize, 0, 4);
int filesize;
filesize = (int) (bSize[0] & 0xff) << 24 |
(int) (bSize[1] & 0xff) << 16 |
(int) (bSize[2] & 0xff) << 8 |
(int) (bSize[3] & 0xff);
int bytesRead;
// You may but don't have to read the whole file in memory
// 8k buffer is good enough
byte[] data = new byte[8 * 1024];
int bToRead;
FileOutputStream fos = new FileOutputStream(imageInSD);
BufferedOutputStream bos = new BufferedOutputStream(fos);
while (filesize > 0)
{
// EDIT: just in case there is more data in the stream.
if (filesize > data.length) bToRead=data.length;
else bToRead=filesize;
bytesRead = is.read(data, 0, bToRead);
if (bytesRead > 0)
{
bos.write(data, 0, bytesRead);
filesize -= bytesRead;
}
}
bos.close();
// I guess you want to return the received image as a Bitmap
Bitmap bmp = null;
FileInputStream fis = new FileInputStream(imageInSD);
try
{
bmp = BitmapFactory.decodeStream(fis);
}
catch (Exception e)
{
// in case of an error set it to null
bmp = null;
}
finally
{
fis.close();
}
return bmp;
}
在Java中复制流的正确方法如下:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
目前,您的代码:
read()
填充缓冲区。Javadoc中没有这样的内容read()
返回的结果,该结果除了是非常宝贵的计数外,还可能是-1表示EOSint
上面的代码不做任何这些假设,并且可以使用从1开始的任何缓冲区大小。只需滚动浏览,但您在行中读取的
是.read(data,0,data.length)
,但不使用在其中读取的字节数(不应该进入索引)?问题可能在于索引+=文件大小
应该是index+=bytesread代码>也考虑前面的注释。这个代码不起作用。您需要限制读取长度,以便永远不会读取超过filesize
。此外,所有的位移位都应该使用DataOutputStream.writeLong()
和DataInputStream.readLong()。
public Bitmap receiveFile(InputStream is) throws Exception
{
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.png";
String imageInSD = baseDir + File.separator + fileName;
System.out.println("FILE----------------->" + imageInSD);
// read first 4 bytes containing the file size
byte[] bSize = new byte[4];
is.read(bSize, 0, 4);
int filesize;
filesize = (int) (bSize[0] & 0xff) << 24 |
(int) (bSize[1] & 0xff) << 16 |
(int) (bSize[2] & 0xff) << 8 |
(int) (bSize[3] & 0xff);
int bytesRead;
// You may but don't have to read the whole file in memory
// 8k buffer is good enough
byte[] data = new byte[8 * 1024];
int bToRead;
FileOutputStream fos = new FileOutputStream(imageInSD);
BufferedOutputStream bos = new BufferedOutputStream(fos);
while (filesize > 0)
{
// EDIT: just in case there is more data in the stream.
if (filesize > data.length) bToRead=data.length;
else bToRead=filesize;
bytesRead = is.read(data, 0, bToRead);
if (bytesRead > 0)
{
bos.write(data, 0, bytesRead);
filesize -= bytesRead;
}
}
bos.close();
// I guess you want to return the received image as a Bitmap
Bitmap bmp = null;
FileInputStream fis = new FileInputStream(imageInSD);
try
{
bmp = BitmapFactory.decodeStream(fis);
}
catch (Exception e)
{
// in case of an error set it to null
bmp = null;
}
finally
{
fis.close();
}
return bmp;
}
public void send(OutputStream os) throws Exception
{
// sendfile
File myFile = new File("C:/div.png");
System.out.println("the file is read");
int fSize = (int) myFile.length();
byte[] bSize = new byte[4];
bSize[0] = (byte) ((fSize & 0xff000000) >> 24);
bSize[1] = (byte) ((fSize & 0x00ff0000) >> 16);
bSize[2] = (byte) ((fSize & 0x0000ff00) >> 8);
bSize[3] = (byte) (fSize & 0x000000ff);
// send 4 bytes containing the filesize
os.write(bSize, 0, 4);
byte[] mybytearray = new byte[(int) fSize];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
int bRead = bis.read(mybytearray, 0, mybytearray.length);
System.out.println("Sending...");
os.write(mybytearray, 0, bRead);
os.flush();
bis.close();
}
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}