java socket 服务器代码 自动接收文件并存储服务器 - 服务器多线程支持和多个客户端同时通信:

187 篇文章 0 订阅
38 篇文章 1 订阅
import java.io.*;
import java.net.*;


public class FileUploadServerExec {
private static Socket socketMain = null;
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(666);
ss.setSoTimeout(0);
System.out.println("accept超时时间设置为: " + ss.getSoTimeout());
while (true) {
socketMain = ss.accept();
new Thread() {
public void run() {
try {
// Socket socketThread = new Socket(socketMain.getInetAddress(), socketMain.getPort());
Socket socketThread = socketMain;
InputStream is = socketThread.getInputStream();
byte[] arb = new byte[1024];
int iReadCount = 0;
int iFileLength = 0;
String strFileName = "";
String strFolderName = "C:\\user_data\\21MyTempFile\\NetServerFolder\\";
String strTemp = "";
int iTemp = 0;
iReadCount = is.read(arb); // 读取一次 ,读文件大小(b)和文件名信息 如
// 657897&&aaa.txt//   这样,其中最后两个字符是分隔字符,其后面的都是文件内容
// 这样的字符串
if (iReadCount == 0) {
System.out.println("读入的文件大小名称字节长度为0,程序退出");
socketThread.close();
return;
}
strTemp = new String(arb, 0, iReadCount);
// strTemp = strTemp.trim();
// System.out.println("接收到的文件信息字符串是: " + strTemp);
iTemp = strTemp.indexOf("//");
if(iTemp<=0){
System.out.println("读入的文件大小名称数据不包含//,或者其在数据开头,程序退出");
socketThread.close();
return;
}
boolean bExistRedundantData=false;
String strTotalString="";
if(iTemp!=strTemp.length()-2){
bExistRedundantData=true;
strTotalString=strTemp.substring(0,iTemp+2);
}
strTemp=strTemp.substring(0,iTemp);
System.out.println("接收到的文件信息字符串是: " + strTemp);
System.out.flush();
iTemp = strTemp.indexOf("&&");
if (iTemp <= 0 || iTemp >= strTemp.length() - 2) {
System.out.println("读入的文件大小和文件名格式错误2,程序退出");
socketThread.close();
return;
}
iFileLength = Integer.parseInt(strTemp.substring(0, iTemp));
strFileName = strTemp.substring(iTemp + 2);
if (iFileLength <= 0 || strFileName.length() == 0) {
System.out.println("读入的文件大小和文件名格式错误3,程序退出");
socketThread.close();
return;
}
System.out.println("开始接收文件" + strFileName + ",文件长度为" + iFileLength + "字节");
FileOutputStream fos = new FileOutputStream(strFolderName + strFileName);
if(bExistRedundantData){
// fos.write(strTotalString.getBytes());
int iNameCount=strTotalString.getBytes().length;
fos.write(arb, iNameCount, iReadCount-iNameCount);
System.out.println("首次接收到的数据中包含文件内容数据");
System.out.flush();
}
iTemp = 0;
while ((iReadCount = is.read(arb)) >= 0 && iTemp < iFileLength) {
fos.write(arb, 0, iReadCount);
iTemp += iReadCount;
// System.out.println("接收到一次数据");
// System.out.flush();
}
if (iTemp == iFileLength)
System.out.println("接收文件" + strFileName + "完全成功,共收到" + iFileLength + "字节");
else
System.out.println("接收文件" + strFileName + "部分成功,共收到" + iTemp + "字节");
socketThread.close();
System.out.flush();
fos.flush();
fos.close();
System.out.println("接收数据线程退出");
System.out.flush();
} catch (Exception e) {
System.out.println("接收数据线程抛出异常,类型是: " + e.getClass().getName());
System.out.println("异常信息是: " + e.getMessage());
}
}
}.start();
}
} catch (Exception e) {
System.out.println("主线程抛出异常,类型是: " + e.getClass().getName());
System.out.println("异常信息是: " + e.getMessage());
}
}
}




import java.io.*;
import java.net.*;


public class FileUploadUserExec {
public static void main(String[] args) {
try{
Socket socket=new Socket(InetAddress.getByAddress(new byte[]{127,0,0,1}),666);
System.out.println("创建socket成功");
File file=new File("C:\\user_data\\21MyTempFile\\3.jpg");
FileInputStream fis=new FileInputStream(file);
int iFileLength=(int)file.length();
String strFileName=file.getName();
String strTemp=iFileLength+"&&"+strFileName+"//";
OutputStream os=socket.getOutputStream();
os.write(strTemp.getBytes());
os.flush(); 
byte []arb=new byte[1024];
int iReadCount=0;
int iTotalReadCount=0;
while((iReadCount=fis.read(arb))>=0){
os.write(arb, 0, iReadCount);
iTotalReadCount+=iReadCount;
}
if(iTotalReadCount==iFileLength)
System.out.println("发送完成,发送的字节数和文件大小相同");
else
System.out.println("发送完成,发送的字节数和文件大小不同");
System.out.flush();
fis.close();
os.close();
socket.close();
}
catch(Exception e){
System.out.println("抛出异常,异常类型是: "+e.getClass().getName());
System.out.println("异常信息是: "+e.getMessage());
}
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值