67,512
社区成员
发帖
与我相关
我的任务
分享
public void SendFile(String fileType,String filePath,Socket socketHandler) throws IOException
{
DataInputStream fileInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
DataOutputStream fileOutStream = new DataOutputStream(socketHandler.getOutputStream());
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true)
{
int read=0;
if(fileInputStream!=null)
{
read=fileInputStream.read(buf);
}
if(read==-1)
{
break;
}
fileOutStream.write(buf, 0, read);
}
fileOutStream.flush();
fileInputStream.close();
//socketHandler.close();
public void ReceiveFile(String fileSavePath,Socket socketHandler,String fileType,String fileName, int fileLength,boolean isUpload) throws IOException
{
String filePureName="";
long len=0;
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
if(fileName.lastIndexOf("\\")>0)
{
filePureName=fileName.substring(fileName.lastIndexOf("\\")+1);
}
else if(fileName.lastIndexOf("/")>0)
{
filePureName=fileName.substring(fileName.lastIndexOf("/")+1);
}
else
{
filePureName=fileName;
}
fileSavePath+=filePureName;
DataOutputStream fileOut = new DataOutputStream(new BufferedOutputStream(new BufferedOutputStream(new FileOutputStream(fileSavePath))));
DataInputStream inputStream = new DataInputStream(new BufferedInputStream(socketHandler.getInputStream()));
while (true)
{
int read=0;
if (inputStream!= null)
{
read = inputStream.read(buf);
}
len+=read;
fileOut.write(buf, 0, read);
if (read == -1 || fileLength==len) {
break;
}
}
fileOut.flush();
fileOut.close();
}
while(true){
int read=0;
if(read != -1){
read = inputStream.read(buf);
}else{
break;
}
len+=read;
fileOut.write(buf, 0, read);
if(fileLength != len){
continue;
}else{
break;
}
}
if(read == -1 || fileLength==len){ break;}
改为:
if(read != -1){
read = inputStream.read(buf);
}else{
break;
}
if(fileLength != len){
continue;
}else{
break;
}