永发信息网

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

答案:1  悬赏:40  手机版
解决时间 2021-03-17 09:01
  • 提问者网友:夢醒日落
  • 2021-03-16 19:40
java中如何实现从客户端发送文件到服务器端?
最佳答案
  • 五星知识达人网友:鸽屿
  • 2021-03-16 21:07
服务器端源码:
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
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯