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

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

服务器端源码:\x0d\import javaioBufferedReader;\x0d\import javaioFile;\x0d\import javaioFileNotFoundException;\x0d\import javaioFileOutputStream;\x0d\import javaioIOException;\x0d\import javaioInputStream;\x0d\import javaioInputStreamReader;\x0d\import javanetServerSocket;\x0d\import javanetSocket;\x0d\\x0d\/\x0d\ \x0d\ 文件名:ServerReceivejava\x0d\ 实现功能:作为服务器接收客户端发送的文件\x0d\ \x0d\ 具体实现过程:\x0d\ 1、建立SocketServer,等待客户端的连接\x0d\ 2、当有客户端连接的时候,按照双方的约定,这时要读取一行数据\x0d\ 其中保存客户端要发送的文件名和文件大小信息\x0d\ 3、根据文件名在本地创建文件,并建立好流通信\x0d\ 4、循环接收数据包,将数据包写入文件\x0d\ 5、当接收数据的长度等于提前文件发过来的文件长度,即表示文件接收完毕,关闭文件\x0d\ 6、文件接收工作结束\x0d\\x0d\public class ServerReceive {\x0d\ \x0d\ public static void main(String[] args) {\x0d\ \x0d\ /与服务器建立连接的通信句柄/\x0d\ ServerSocket ss = null;\x0d\ Socket s = null;\x0d\ \x0d\ /定义用于在接收后在本地创建的文件对象和文件输出流对象/\x0d\ File file = null;\x0d\ FileOutputStream fos = null;\x0d\ \x0d\ /定义输入流,使用socket的inputStream对数据包进行输入/\x0d\ InputStream is = null;\x0d\ \x0d\ /定义byte数组来作为数据包的存储数据包/\x0d\ byte[] buffer = new byte[4096 5];\x0d\ \x0d\ /用来接收文件发送请求的字符串/\x0d\ String comm = null;\x0d\\x0d\/建立socekt通信,等待服务器进行连接/\x0d\ try {\x0d\ ss = new ServerSocket(4004);\x0d\ s = ssaccept();\x0d\ } catch (IOException e) {\x0d\ eprintStackTrace();\x0d\ }\x0d\\x0d\/读取一行客户端发送过来的约定信息/\x0d\ try {\x0d\ InputStreamReader isr = new InputStreamReader(sgetInputStream());\x0d\ BufferedReader br = new BufferedReader(isr);\x0d\ comm = brreadLine();\x0d\ } catch (IOException e) {\x0d\ Systemoutprintln("服务器与客户端断开连接");\x0d\ }\x0d\ \x0d\ /开始解析客户端发送过来的请求命令/\x0d\ int index = commindexOf("/#");\x0d\ \x0d\ /判断协议是否为发送文件的协议/\x0d\ String xieyi = commsubstring(0, index);\x0d\ if(!xieyiequals("111")){\x0d\ Systemoutprintln("服务器收到的协议码不正确");\x0d\ return;\x0d\ }\x0d\ \x0d\ /解析出文件的名字和大小/\x0d\ comm = commsubstring(index + 2);\x0d\ index = commindexOf("/#");\x0d\ String filename = commsubstring(0, index)trim();\x0d\ String filesize = commsubstring(index + 2)trim();\x0d\\x0d\/创建空文件,用来进行接收文件/\x0d\ file = new File(filename);\x0d\ if(!fileexists()){\x0d\ try {\x0d\ filecreateNewFile();\x0d\ } catch (IOException e) {\x0d\ Systemoutprintln("服务器端创建文件失败");\x0d\ }\x0d\ }else{\x0d\ /在此也可以询问是否覆盖/\x0d\ Systemoutprintln("本路径已存在相同文件,进行覆盖");\x0d\ }\x0d\ \x0d\ /以上就是客户端代码中写到的服务器的准备部分/\x0d\\x0d\/\x0d\ 服务器接收文件的关键代码/\x0d\ try {\x0d\ /将文件包装到文件输出流对象中/\x0d\ fos = new FileOutputStream(file);\x0d\ long file_size = LongparseLong(filesize);\x0d\ is = sgetInputStream();\x0d\ /size为每次接收数据包的长度/\x0d\ int size = 0;\x0d\ /count用来记录已接收到文件的长度/\x0d\ long count = 0;\x0d\ \x0d\ /使用while循环接收数据包/\x0d\ while(count 回答于 2022-12-11

通过socket可以用如下方式进行。
1启动服务端代码。
2启动客户端自动连接服务端。
3服务端上传文件,保存文件和路径。
4将路径发送给连接服务端的客户端。

程序分Server和Client
服务器端打开侦听的端口,一有客户端连接就创建两个新的线程来负责这个连接
一个负责客户端发送的信息(ClientMsgCollectThread 类),
另一个负责通过该Socket发送数据(ServerMsgSendThread )
Serverjava代码如下:
/
创建日期 2009-3-7

TODO 要更改此生成的文件的模板,请转至
窗口 - 首选项 - Java - 代码样式 - 代码模板
/
package faueMutiUser;
import javaioBufferedReader;
import javaioIOException;
import javaioInputStreamReader;
import javaioPrintWriter;
import javanetServerSocket;
import javanetSocket;
/
服务器端

@author Faue
/
public class Server extends ServerSocket {
private static final int SERVER_PORT = 10000;
/
构造方法,用于实现连接的监听

@throws IOException
/
public Server() throws IOException {
super(SERVER_PORT);
try {
while (true) {
Socket socket = superaccept();
new Thread(new ClientMsgCollectThread(socket), "getAndShow"
+ socketgetPort())start();
new Thread(new ServerMsgSendThread(socket), "send"
+ socketgetPort())start();
}
} catch (IOException e) {
eprintStackTrace();
}
}
public static void main(String[] args) throws IOException {
new Server();
}
/
该类用于创建接收客户端发来的信息并显示的线程

@author Faue
@version 100
/
class ClientMsgCollectThread implements Runnable {
private Socket client;
private BufferedReader in;
private StringBuffer inputStringBuffer = new StringBuffer("Hello");
/
得到Socket的输入流

@param s
@throws IOException
/
public ClientMsgCollectThread(Socket s) throws IOException {
client = s;
in = new BufferedReader(new InputStreamReader(client
getInputStream(), "GBK"));
}
public void run() {
try {
while (!clientisClosed()) {
inputStringBufferdelete(0, inputStringBufferlength());
inputStringBufferappend(inreadLine());
Systemoutprintln(getMsg(inputStringBuffertoString()));
}
} catch (IOException e) {
//eprintStackTrace();
Systemoutprintln(clienttoString() + " is closed!");
}
}
/
构造显示的字符串

@param line
@return
/
private String getMsg(String line) {
return clienttoString() + " says:" + line;
}
}
/
该类用于创建发送数据的线程

@author Faue
@version 100
/
class ServerMsgSendThread implements Runnable {
private Socket client;
private PrintWriter out;
private BufferedReader keyboardInput;
private StringBuffer outputStringBuffer = new StringBuffer("Hello");
/
得到键盘的输入流

@param s
@throws IOException
/
public ServerMsgSendThread(Socket s) throws IOException {
client = s;
out = new PrintWriter(clientgetOutputStream(), true);
keyboardInput = new BufferedReader(new InputStreamReader(Systemin));
}
public void run() {
try {
while (!clientisClosed()) {
outputStringBufferdelete(0, outputStringBufferlength());
outputStringBufferappend(keyboardInputreadLine());
outprintln(outputStringBuffertoString());
}
} catch (IOException e) {
//eprintStackTrace();
Systemoutprintln(clienttoString() + " is closed!");
}
}
}
}
客户端:
实现基于IP地址的连接,连接后也创建两个线程来实现信息的发送和接收
/
创建日期 2009-3-7

/
package faueMutiUser;
import javaioBufferedReader;
import javaioIOException;
import javaioInputStreamReader;
import javaioPrintWriter;
import javanetSocket;
/
客户端

@author Faue
/
public class Client {
private Socket mySocket;
/
创建线程的构造方法

@param IP
@throws IOException
/
public Client(String IP) throws IOException {
try {
mySocket = new Socket(IP, 10000);
new Thread(new ServerMsgCollectThread(mySocket), "getAndShow"
+ mySocketgetPort())start();
new Thread(new ClientMsgSendThread(mySocket), "send"
+ mySocketgetPort())start();
} catch (IOException e) {
//eprintStackTrace();
Systemoutprintln("ServerIP:" + IP
+ " port:10000 can not be Connected");
}
}
public static void main(String[] args) throws IOException {
try {
new Client(args[0]);
} catch (Exception e) {
Systemoutprintln("输入的IP地址错误");
}
}
/
该类用于创建接收服务端发来的信息并显示的线程

@author Faue
@version 100
/
class ServerMsgCollectThread implements Runnable {
private Socket client;
private BufferedReader in;
private StringBuffer inputStringBuffer = new StringBuffer("Hello");
/
得到Socket的输入流

@param s
@throws IOException
/
public ServerMsgCollectThread(Socket s) throws IOException {
client = s;
in = new BufferedReader(new InputStreamReader(client
getInputStream(), "GBK"));
}
public void run() {
try {
while (!clientisClosed()) {
inputStringBufferdelete(0, inputStringBufferlength());
inputStringBufferappend(inreadLine());
Systemoutprintln(getMsg(inputStringBuffertoString()));
}
} catch (IOException e) {
//eprintStackTrace();
Systemoutprintln(clienttoString() + " is closed!");
Systemexit(0);
}
}
/
构造输入字符串

@param line
@return
/
private String getMsg(String line) {
return clienttoString() + " says:" + line;
}
}
/
该类用于创建发送数据的线程

@author Faue
@version 100
/
class ClientMsgSendThread implements Runnable {
private Socket client;
private PrintWriter out;
private BufferedReader keyboardInput;
private StringBuffer outputStringBuffer = new StringBuffer("Hello");
/
得到键盘的输入流

@param s
@throws IOException
/
public ClientMsgSendThread(Socket s) throws IOException {
client = s;
out = new PrintWriter(clientgetOutputStream(), true);
keyboardInput = new BufferedReader(new InputStreamReader(Systemin));
}
public void run() {
try {
while (!clientisClosed()) {
outputStringBufferdelete(0, outputStringBufferlength());
outputStringBufferappend(keyboardInputreadLine());
outprintln(outputStringBuffertoString());
}
outprintln("--- See you, bye! ---");
} catch (IOException e) {
//eprintStackTrace();
Systemoutprintln(clienttoString() + " is closed!");
Systemexit(0);
}
}
}
}
如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
vaela

当然不行了,你这个是在本地做的文件 *** 作,而不是真正意义上的上传。
可以借助某些插件实现文件上传,比如smartupload、common-fileupload等。
要明白什么是上传!!!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/13413814.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-31
下一篇 2023-07-31

发表评论

登录后才能评论

评论列表(0条)

保存