java socket 服务器代码 自动接收文件并存储服务器 - 服务器多线程支持和多个客户端同时通信:
2017-05-05 19:46
489 查看
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());
}
}
}
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());
}
}
}
相关文章推荐
- 局域网聊天工具(多线程),支持客户端与客户端间通信,服务器负责信息的接收与发送(client)
- 局域网聊天工具(多线程),支持客户端与客户端间通信,服务器负责信息的接收与发送
- Qt5 基于TCP传输的发送/接收文件服务器(支持多客户端)
- 基于TCP网络通信的自动升级程序源码分析-客户端接收文件
- 文件接收网络对讲机C#服务器 Android客户端(二) C#服务器代码分析
- 基于TCP网络通信的自动升级程序源码分析-客户端接收文件
- 基于TCP网络通信的自动升级程序源码分析-客户端接收文件
- 服务器端接收来自多个客户端的文件,服务器要求用多线程
- Qt5 基于TCP传输的发送/接收文件服务器(支持多客户端)
- Qt5 基于TCP传输的发送/接收文件服务器(支持多客户端)
- Qt5 基于TCP传输的发送/接收文件服务器(支持多客户端)
- 基于Socket通信的BS结构文件服务器客户端的简易程序(3)
- 支持代码高亮、图片水印、远程文件自动上传的fckeditor
- 下载(二)客户端通过C# 代码直接从服务器下载文件到本地
- Java Socket进行客户端和服务器进行通信的简单例子
- 用c和mysql API写的简易操作mysql数据库的多线程服务器和客户端代码
- 基于Socket通信的BS结构文件服务器客户端的简易程序(2)
- Java Socket编程 文件传输(客户端从服务器下载一个文件)
- Asp.net 2.0 文件下载[支持多线程, 断点续传功能](示例代码下载)
- 下载(二)客户端通过C# 代码直接从服务器下载文件到本地