求助java Socket传送文件的相关问题 各位,小弟最近在做一个socke发送文件的程序,服务器端接收到文件就写文件,客户端发送文件,当发送单个文件时没有问题,发送多个文件时会出现有前一个文件(文件A)结尾部分会写到下一个文件(文件B)的情况,但是文件A是完整的,麻烦各位指教一下是怎么回事?我用的是InputStream 读取的文件内容,OutputStream写入socket中的。麻烦各位指教一下啦!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public void sendFile(File mFile) { Log.d(TAG,"---------sendFile = " + mFile.getName() + "fileSize = " + mFile.length()); try { Thread.sleep(200); } catch (InterruptedException e2) { // TODO Auto-generated catch block e2.printStackTrace(); }// try {// socketFile.setTcpNoDelay(false);// } catch (SocketException e2) {// // TODO Auto-generated catch block// e2.printStackTrace();// } int i = 0; try { mInStrFile = new FileInputStream(mFile); int readSize = 0; int readFileSize = 0; byte data[] = new byte[1228]; int[] per = new int[2]; do { if(mRunning) { readSize = mInStrFile.read(data); if(readSize != -1) { { mOutStrFile.write(data, 0, readSize); } i++; } } else { Log.d(TAG, "---break"); break; }//更新界面// readTotalSize += readSize;// readFileSize += readSize;// Message msg = new Message();// msg.what = ConstData.MSG_AUTO_BACKUPS_PERCENT;// percent = (int) ((long)readTotalSize * 100 / backupsFileSize);// per[i % 2] = percent;// msg.arg1 = percent;// if(per[0] != per[1])// {// mHandler.sendMessage(msg);// } }while(readSize != -1); //socketFile.shutdownOutput(); //mOutStrFile.// socketFile.setTcpNoDelay(true); mOutStrFile.flush();// mInStrFile.close();// mInStrFile = null; Log.d(TAG, "*********" + mFile.getName() + " send succeed, fileSize is:" + readFileSize); //发送文件结束标志 sendMsg(ConstData.AMP_REQUEST_BACKUPS_FILE_END_LEN, ConstData.AMP_REQUEST_BACKUPS_FILE_END, ConstData.FILE_END_FLAG); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); Log.d(TAG,"-------fff"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.d(TAG,"-------iii " + e.toString()); } } 在一个循环中调用的sendFilefor(int i = 0; i < fileNum; i++){ sendFile(fileList(i));} 在一个循环中调用的sendFilefor(int i = 0; i < fileNum; i++){ sendFile(fileList(i));} android怎么样在两个activity中传递控件对象 请问;如何用POST方法上传文件? android应用签名编码格式 android 自动定位时用 NETWORK_PROVIDER方式获取经纬度 Android 横竖屏问题 想实现微信的录音按钮 key:btn imagebtn 关于android删除SD卡指定文件的问题 PC为Server端,手机为Client端,SOCKET如何填写IP问题 Android Dialog如何显示在指定控件的下面 安卓开发错误日志什么意思? if语句判断一直通过不了,我都想砸电脑了,求解大神指导,,, 小弟新手,请大神帮忙
public void sendFile(File mFile) { Log.d(TAG,"---------sendFile = " + mFile.getName() + "fileSize = " + mFile.length()); try { Thread.sleep(200); } catch (InterruptedException e2) { // TODO Auto-generated catch block e2.printStackTrace(); }// try {// socketFile.setTcpNoDelay(false);// } catch (SocketException e2) {// // TODO Auto-generated catch block// e2.printStackTrace();// } int i = 0; try { mInStrFile = new FileInputStream(mFile); int readSize = 0; int readFileSize = 0; byte data[] = new byte[1228]; int[] per = new int[2]; do { if(mRunning) { readSize = mInStrFile.read(data); if(readSize != -1) { { mOutStrFile.write(data, 0, readSize); } i++; } } else { Log.d(TAG, "---break"); break; }
//更新界面
// readTotalSize += readSize;// readFileSize += readSize;// Message msg = new Message();// msg.what = ConstData.MSG_AUTO_BACKUPS_PERCENT;// percent = (int) ((long)readTotalSize * 100 / backupsFileSize);// per[i % 2] = percent;// msg.arg1 = percent;// if(per[0] != per[1])// {// mHandler.sendMessage(msg);// } }while(readSize != -1); //socketFile.shutdownOutput(); //mOutStrFile.// socketFile.setTcpNoDelay(true); mOutStrFile.flush();// mInStrFile.close();// mInStrFile = null; Log.d(TAG, "*********" + mFile.getName() + " send succeed, fileSize is:" + readFileSize); //发送文件结束标志 sendMsg(ConstData.AMP_REQUEST_BACKUPS_FILE_END_LEN, ConstData.AMP_REQUEST_BACKUPS_FILE_END, ConstData.FILE_END_FLAG); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); Log.d(TAG,"-------fff"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Log.d(TAG,"-------iii " + e.toString()); } }
for(int i = 0; i < fileNum; i++)
{
sendFile(fileList(i));
}
在一个循环中调用的sendFilefor(int i = 0; i < fileNum; i++)
{
sendFile(fileList(i));
}