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

xiaoxiao2021-02-28  99

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()); } } }
转载请注明原文地址: https://www.6miu.com/read-40182.html

最新回复(0)