永发信息网

java中如何实现从客户端发送文件到服务器端?

答案:1  悬赏:40  手机版
解决时间 2021-03-17 09:01
java中如何实现从客户端发送文件到服务器端?
最佳答案
服务器端源码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;


ServerSocket ss = null;
Socket s = null;


File file = null;
FileOutputStream fos = null;


InputStream is = null;


byte[] buffer = new byte[4096 * 5];


String comm = null;


try {
ss = new ServerSocket(4004);
s = ss.accept();
} catch (IOException e) {
e.printStackTrace();
}


try {
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
comm = br.readLine();
} catch (IOException e) {
System.out.println("服务器与客户端断开连接");
}


int index = comm.indexOf("/#");


String xieyi = comm.substring(0, index);
if(!xieyi.equals("111")){
System.out.println("服务器收到的协议码不正确");
return;
}


comm = comm.substring(index + 2);
index = comm.indexOf("/#");
String filename = comm.substring(0, index).trim();
String filesize = comm.substring(index + 2).trim();


file = new File(filename);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("服务器端创建文件失败");
}
}else{

System.out.println("本路径已存在相同文件,进行覆盖");
}




try {

fos = new FileOutputStream(file);
long file_size = Long.parseLong(filesize);
is = s.getInputStream();

int size = 0;

long count = 0;


while(count < file_size){

size = is.read(buffer);


fos.write(buffer, 0, size);
fos.flush();


count += size;
System.out.println("服务器端接收到数据包,大小为" + size);
}

} catch (FileNotFoundException e) {
System.out.println("服务器写文件失败");
} catch (IOException e) {
System.out.println("服务器:客户端断开连接");
}finally{

try {
if(fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}//catch (IOException e)
}//finally

}//public static void main(String[] args)
}//public class ServerReceive

客户端源码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;



public class ClientSend {

public static void main(String[] args) {


Socket s = null;


File sendfile = new File("API.CHM");

FileInputStream fis = null;

byte[] buffer = new byte[4096 * 5];


OutputStream os = null;


if(!sendfile.exists()){
System.out.println("客户端:要发送的文件不存在");
return;
}


try {
s = new Socket("127.0.0.1", 4004);
}catch (IOException e) {
System.out.println("未连接到服务器");
}


try {
fis = new FileInputStream(sendfile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}


try {
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("111/#" + sendfile.getName() + "/#" + fis.available());
ps.flush();
} catch (IOException e) {
System.out.println("服务器连接中断");
}


try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}


try {


os = s.getOutputStream();


int size = 0;


while((size = fis.read(buffer)) != -1){
System.out.println("客户端发送数据包,大小为" + size);

os.write(buffer, 0, size);

os.flush();
}
} catch (FileNotFoundException e) {
System.out.println("客户端读取文件出错");
} catch (IOException e) {
System.out.println("客户端输出文件出错");
}finally{


try {
if(fis != null)
fis.close();
} catch (IOException e) {
System.out.println("客户端文件关闭出错");
}//catch (IOException e)
}//finally

}//public static void main(String[] args)
}//public class ClientSend
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
跪求精华学校宋晓磊老师的高中物理视频中的全
下列图象中,以方程y-2x-2=0的解为坐标的点组
上海荣乙化工有限公司尼泓塑料分公司怎么样?
手机自动生成dump文件,50多G内存一会儿就被
精工男表,光动能,为何大秒针不动啊,按左边
天津蓟l县到罗庄指镇乘几路车
天猫上买的rust国区的怎么联机不了
单选题我国公民参与国家管理的基础和标志是A.
香菇酱汁薄肠粉怎么做
1000以内的减法算式大全
南审校园卡遗失怎么办
山西的壹枚双旗币值钱么
490(x-50)(x-40)=8000
般若纹身头上接什么小图好看
阳春八甲三角排影响那些风水?
推荐资讯
请问伙表箱800*800*300怎么计算面积
噗在动物里代表是什么动物
梦幻西游飞升后的人物可以换多少个潜能果?
盗窃案件可办理取保吗?
all kids living on her street 为什么用livi
在杭州,汽车加油一般是去中石化还是中石油?
身上疤痕没有好可以吃羊肉吗
朱棣上位是我国古代最难的吗?
在五万人左右的小镇适合开什么店
煮羊肉怎样去除异味
准高三生常见的七大焦虑症,你占几条
我今年报了天津商业大学酒店管理(中美合作)
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?