我创建了一个应用程序,通过套接字编程将图像从服务器(桌面)发送到客户端(android)............问题是我在客户端获取文件(android),但没有内容。
任何人都可以告诉我这是什么问题
客户端(Android)
DataInputStream dis=new DataInputStream(socket.getInputStream());
receiveFile(dis); // call method receiveFile()
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);
int filesize=6022386;
int bytesRead;
int current = 0;
byte [] data = new byte [filesize];
FileOutputStream fos = new FileOutputStream(imageInSD);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(data,0,data.length);
current = bytesRead;
int index = 0;
while (index < filesize)
{
bytesRead = is.read(data, index, filesize - index);
if (bytesRead < 0)
{
throw new IOException("Insufficient data in stream");
}
index += filesize;
}
bos.write(data, 0 , current);
bos.flush();
bos.close();
return null;
}
服务器(桌面)
send(socket.getOutputStream()); // call method send()
public void send(OutputStream os) throws Exception{
// sendfile
File myFile = new File ("C:/div.png");
System.out.println("the file is read");
byte [] mybytearray = new byte [(int)myFile.length()+1];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
}
答案 0 :(得分:0)
在Java中复制流的正确方法如下:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
}
目前您的代码:
read()
填充缓冲区。 Javadoc中没有任何内容可以这么说。read()
返回的结果,除了是无价值的计数外,还可以是-1表示EOS。int
。上面的代码没有做出这些假设,并且适用于从1开始的任何缓冲区大小。
答案 1 :(得分:-1)
查看您的代码,我发现您希望收到一个文件,将其保存到外部存储器并返回该文件的Bitmap。这就是我想你想做的事情,但是你的代码并没有这样做。如果需要,可以使用以下代码完成该任务。首先,服务器发送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;
}
服务器代码:
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();
}