安卓初学者,现有Android给PC传送文件的源代码存在严重问题,请帮忙指正一下。 30
我想在电脑端用VCSOCKET接收文件,安卓发送文件。电脑端跟电脑端传送文件是正常的,但是安卓传送过来的文件,基本上都有问题。我测试过多种格式的文件传送,mp3跟压缩文件...
我想在电脑端用VC SOCKET接收文件,安卓发送文件。电脑端跟电脑端传送文件是正常的,但是安卓传送过来的文件,基本上都有问题。我测试过多种格式的文件传送,mp3跟压缩文件能够打开,其他格式的文件都有不同程度的数据丢失(比如手机上900kb,到电脑上只有898kb)。
public String SendFile(String fileName, String path, String ipAddress, int port){
try {
Socket name = new Socket(ipAddress, port);
OutputStream outputName = name.getOutputStream();
OutputStreamWriter outputWriter = new OutputStreamWriter(outputName);
BufferedWriter bwName = new BufferedWriter(outputWriter);
bwName.write(fileName);
bwName.close();
outputWriter.close();
outputName.close();
name.close();
Socket data = new Socket(ipAddress, port);
OutputStream outputData = data.getOutputStream();
FileInputStream fileInput = new FileInputStream(path);
int size = -1;
byte[] buffer = new byte[1024];
while((size = fileInput.read(buffer, 0, 1024)) != -1){
outputData.write(buffer, 0, size);
}
outputData.close();
fileInput.close();
data.close();
return fileName + " 发送完成";
} catch (Exception e) {
return "发送错误:\n" + e.getMessage();
}
}
//PC端代码///////////////////////////////////////////////////////////////
CFile file;
char buffer[BUFFERSIZE];//每次收,清空buffer?
if( 0==file.Open(savePathName,CFile::modeWrite |CFile::modeCreate| CFile::typeBinary) )
while( true )
{
finish=sock.Receive(buffer,BUFFERSIZE);
if(0==finish)
break;
if(SOCKET_ERROR==finish)
{
sock.Close();
sp.pCLAN->SetFocus();
return 0;
}
file.Write(buffer,finish);
step+=finish;
}
file.Close( );
sock.Close( ); 展开
public String SendFile(String fileName, String path, String ipAddress, int port){
try {
Socket name = new Socket(ipAddress, port);
OutputStream outputName = name.getOutputStream();
OutputStreamWriter outputWriter = new OutputStreamWriter(outputName);
BufferedWriter bwName = new BufferedWriter(outputWriter);
bwName.write(fileName);
bwName.close();
outputWriter.close();
outputName.close();
name.close();
Socket data = new Socket(ipAddress, port);
OutputStream outputData = data.getOutputStream();
FileInputStream fileInput = new FileInputStream(path);
int size = -1;
byte[] buffer = new byte[1024];
while((size = fileInput.read(buffer, 0, 1024)) != -1){
outputData.write(buffer, 0, size);
}
outputData.close();
fileInput.close();
data.close();
return fileName + " 发送完成";
} catch (Exception e) {
return "发送错误:\n" + e.getMessage();
}
}
//PC端代码///////////////////////////////////////////////////////////////
CFile file;
char buffer[BUFFERSIZE];//每次收,清空buffer?
if( 0==file.Open(savePathName,CFile::modeWrite |CFile::modeCreate| CFile::typeBinary) )
while( true )
{
finish=sock.Receive(buffer,BUFFERSIZE);
if(0==finish)
break;
if(SOCKET_ERROR==finish)
{
sock.Close();
sp.pCLAN->SetFocus();
return 0;
}
file.Write(buffer,finish);
step+=finish;
}
file.Close( );
sock.Close( ); 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询