使用java實現(xiàn)云端資源共享小程序的代碼
云端共享小程序:
首先介紹一些程序功能:多用戶共享資源,創(chuàng)建一個共享服務(wù)器,服務(wù)器存儲器可以存放資源,用戶可以向服務(wù)器上傳文件,也可以從服務(wù)器下載文件,實現(xiàn)了多用戶分享資源的功能。
技術(shù)棧
1.集合框架(Map集合)
2.IO流(對象序列化,文件傳輸?shù)龋?br />
3.多線程
4.網(wǎng)絡(luò)編程(TCP/IP協(xié)議)
5.簡單的GUI界面
來看下界面效果(本人喜歡粉色,用戶可以自定義顏色…):

點擊下載后:

具體不再詳述,看程序:服務(wù)端:
package com.softeem.clound.server;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import com.softeem.clound.ToolsAndUI.Tools;
/***
* 云端共享服務(wù)器:
* 實現(xiàn)多用戶云端共享資源
* @author gq
*
*/
public class CloudServer extends Thread {
/** 套接字 */
private Socket s;
/** 文件Map集合 */
Map<String, File> map = new HashMap<String, File>();
/** 服務(wù)器暫存文件的文件夾(這里我用電腦作為服務(wù)器) */
File file = new File("C:\\Users\\14252\\Desktop\\keepFiles");
/**
* 定義構(gòu)造器初始化套接字
*
* @param s
*/
public CloudServer(Socket s) {
this.s = s;
}
@Override
public void run() {
File files[] = file.listFiles();
// 先將服務(wù)器文件加入到結(jié)合中去
for (File file : files) {
map.put(file.getName(), file);
}
// 先詢問用戶下載還是上傳
try {
/*
* // 將選擇發(fā)送給用戶 String msg = "歡迎進入云端共享(先選擇您的操作)";
* Tools.sendMsg(s.getOutputStream(), msg);
*/
// 接收到用戶的回復
String s1 = Tools.getMsg(s.getInputStream());
// 得到用戶請求進行操作
if (s1.equals("1")) {
Tools.tips("用戶選擇了下載操作!");
downLoad();
} else if ("2".equals(s1)) {
Tools.tips("用戶選擇了上傳操作!");
upLoad();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 下載文件
*
* @throws IOException
*/
private void downLoad() throws IOException {
/**
* 將文件集合序列化傳給用戶
*/
Tools.transObject(map, s.getOutputStream());
// 得到用戶下載文件的名字(用戶發(fā)來的名字可能有空格,去除空格)
String name = Tools.getMsg(s.getInputStream()).trim();
// 通過對面?zhèn)鬟^來的文件名找到文件
File file = map.get(name);
// 將文件傳輸給用戶
Tools.transFile(s.getOutputStream(), file);
// 將傳輸成功的信息打印到控制臺
Tools.tips(file.getName() + ":傳輸完成");
// 通過半關(guān)閉將輸出流關(guān)閉,解決服務(wù)端阻塞問題
s.shutdownOutput();
}
/**
* 用戶上傳文件
*
* @throws IOException
* @throws ClassNotFoundException
*/
private void upLoad() throws ClassNotFoundException, IOException {
// 通過對象序列化得到文件對象
Object obj = Tools.getObject(s.getInputStream());
File f = (File) obj;
// 設(shè)置好文件路徑
file = new File(file, f.getName());
// 傳輸信息解決阻塞問題
Tools.sendMsg(s.getOutputStream(), "");
// 獲取文件
Tools.getFile(s.getInputStream(), file);
// 服務(wù)器控制臺顯示用戶下載成功
Tools.tips(file.getName() + " 文件上傳成功");
}
public static void main(String[] args) throws IOException {
// 設(shè)置一個端口為5555的服務(wù)器
ServerSocket server = new ServerSocket(5555);
// 不斷循環(huán)接收多個用戶請求
while (true) {
// 監(jiān)聽用戶請求
Socket s = server.accept();
// 當監(jiān)聽到用戶請求時,創(chuàng)建線程執(zhí)行任務(wù)
new CloudServer(s).start();
// 將每個客戶進入到服務(wù)器的信息打印到控制臺
Tools.tips("客戶端訪問了服務(wù)器:" + s.getInetAddress().getHostAddress());
}
}
}
工具類:
package com.softeem.clound.ToolsAndUI;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
/**
* 工具類
* @author 14252
*/
public class Tools {
/**
* * 將套接字輸出流包裝成打印流并且打印信息
* @param os
* @param msg
* @throws IOException
*/
public static void Println(OutputStream os, String msg) throws IOException {
}
public static String getMsg(InputStream in) throws IOException {
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
String s1 = null;
s1 = br.readLine();
return s1;
}
/**
* 簡化輸出語句
*
* @param s
*/
public static void tips(String s) {
System.out.println(s);
}
/**
* 傳輸(發(fā)送)文件
* @param os
* @param file
* @throws IOException
*/
public static void transFile(OutputStream os, File file) throws IOException {
BufferedOutputStream fos = new BufferedOutputStream(os);
byte b[] = new byte[1024];
FileInputStream fis = new FileInputStream(file);
int len = 0;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.flush();
}
/**
* 發(fā)送消息
* @param os
* @param msg
*/
public static void sendMsg(OutputStream os, String msg) {
PrintWriter pw = new PrintWriter(os);
pw.println(msg);
pw.flush();
}
/**
* 接收文件
* @param in
* @param file
* @throws IOException
*/
public static void getFile(InputStream in, File file) throws IOException {
BufferedInputStream bu = new BufferedInputStream(in);
int len = 0;
byte b[] = new byte[1024];
// System.out.println("下載完成!");
FileOutputStream fos = new FileOutputStream(file);
while ((len = bu.read(b)) != -1) {
fos.write(b, 0, len);
}
fos.flush();
}
/**
* 定義泛型方法傳輸序列化對象
* @param t
* @param os
* @throws IOException
*/
public static <T>void transObject(T t,OutputStream os) throws IOException{
ObjectOutputStream oos=new ObjectOutputStream(os);
oos.writeObject(t);
}
/**
* 定義泛型方法反序列化序列化對象
* @param in
* @return
* @throws ClassNotFoundException
* @throws IOException
*/
public static Object getObject(InputStream in) throws ClassNotFoundException, IOException{
ObjectInputStream ois = new ObjectInputStream(in);
Object o= ois.readObject();
return o;
}
}
界面
package com.softeem.clound.ToolsAndUI;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* JFrame界面
*
* @author 14252
*
*/
public class MyJFrame extends JFrame {
private static final long serialVersionUID = -82252562835060372L;
// 確認按鈕
public static JButton jb1 = new JButton("確認");
public static JButton jb2 = new JButton("確認");
public static JButton jbDown = new JButton("下載");
public static JButton jbUp = new JButton("上傳");
// 文本框
public static JTextArea jt2 = new JTextArea();
public static JTextArea jt = new JTextArea();
public static JTextArea jt1 = new JTextArea();
public static JScrollPane js = new JScrollPane();
// 標簽
public static JLabel j1 = new JLabel("輸入您要下載的文件名");
public static JLabel j2 = new JLabel("輸入上傳文件路徑");
/**
* 顯示窗口
*/
public static void showJFrame() {
JFrame jf = new JFrame();
/** 設(shè)置窗口 */
jf.setTitle("云端文件共享mini版");
jf.setSize(520, 700);
jf.setLayout(null);
jf.setVisible(true);
jf.setResizable(false);
// 設(shè)置容器
Container c = jf.getContentPane();
/** 設(shè)置滾動條以及文本框 */
js.setBounds(50, 50, 400, 200);
c.add(js);
jt.setFont(new Font("", 20, 16));
jt.setBounds(50, 50, 400, 200);
js.setViewportView(jt);
c.add(jt);
/** 設(shè)置上傳下載按鈕 */
jbDown.setBounds(50, 280, 100, 40);
jbUp.setBounds(350, 280, 100, 40);
c.add(jbUp);
c.add(jbDown);
/** 設(shè)置按鈕1以及文本框1以及標簽1 */
jb1.setBounds(350, 350, 100, 40);
c.add(jb1);
j1.setBounds(50, 360, 300, 100);
j1.setFont(new Font("", 20, 20));
j1.setForeground(Color.RED);
c.add(j1);
jt1.setBounds(50, 350, 240, 40);
jt1.setFont(new Font("", 18, 18));
js.setViewportView(jt);
c.add(jt1);
/** 設(shè)置按鈕2以及文本框2以及標簽2 */
jb2.setBounds(350, 500, 100, 40);
c.add(jb2);
jt2.setBounds(50, 500, 240, 40);
c.add(jt2);
j2.setBounds(50, 510, 300, 100);
j2.setFont(new Font("", 20, 20));
j2.setForeground(Color.RED);
c.add(j2);
jt2.setFont(new Font("", 17, 17));
/** 設(shè)置背景顏色 */
c.setBackground(Color.PINK);
}
}
服務(wù)端:
package com.softeem.clound.cilent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import com.softeem.clound.ToolsAndUI.MyJFrame;
import com.softeem.clound.ToolsAndUI.Tools;
/**
* 客戶端
*
* @author gq
*/
public class CilentSharing {
/** 套接字 */
private Socket s;
/** 下載到的路徑 */
File file1;
/** 定義一個狀態(tài)(1 下載 2 上傳) */
String ss;
/** 服務(wù)器ip地址 */
private String ip;
/** 服務(wù)器端口號 */
private int port;
public CilentSharing(File file1, String ip, int port) {
this.file1 = file1;
this.ip = ip;
this.port = port;
}
public CilentSharing() {
}
/**
* 選擇進行的操作
*
* @throws IOException
* @throws ClassNotFoundException
*/
private void choose() throws IOException, ClassNotFoundException {
// 下載
downLoad();
// 上傳
upLoad();
}
/**
* 下載功能(點擊下載按鈕啟動)
*/
private void downLoad() {
// 點擊下載按鈕開始下載
MyJFrame.jbDown.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
s = new Socket(ip, port);
} catch (IOException e2) {
e2.printStackTrace();
}
// 將你的選擇發(fā)送給服務(wù)端(1,2)
try {
ss = "1";
Tools.sendMsg(s.getOutputStream(), ss);
// 啟動下載線程
new DownlownServer(s, file1).start();
return;
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
/**
* 上傳文件功能(點擊上傳按鈕啟動)
*/
private void upLoad() {
MyJFrame.jbUp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
MyJFrame.jt.setText("請輸入您要上傳文件所在路徑!");
s = new Socket(ip, port);
// 將選擇發(fā)給服務(wù)端
ss = "2";
Tools.sendMsg(s.getOutputStream(), ss);
// 開啟上傳線程
new UpServer(s).start();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
/**
* 開始任務(wù)
*
* @throws ClassNotFoundException
* @throws IOException
*/
public static void main(String[] args) throws ClassNotFoundException, IOException {
// 啟動界面
MyJFrame.showJFrame();
// 說明
MyJFrame.jt.setText("歡迎使用云端共享!??!" + "\n" + "注意:" + "\n" + "在上傳文件填寫路徑前或選擇下載文件時" + ",先選擇您的" + "\n" + "操作(上傳/下載)");
// 文件下載的路徑(這里寫自己的保存路徑)
File f = new File("C:\\Users\\14252\\Desktop\\新建文件夾 (4)");
// 端口號
int port = 5555;
// ip地址
String ip = "192.168.0.102";
// 調(diào)用選擇進行操作的方法
new CilentSharing(f, ip, port).choose();
}
}
客戶端下載服務(wù)線程:
package com.softeem.clound.cilent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import com.softeem.clound.ToolsAndUI.MyJFrame;
import com.softeem.clound.ToolsAndUI.Tools;
/**
* 創(chuàng)建線程回應下載點擊事件
*
* @author gq
*
*/
public class DownlownServer extends Thread {
/** socket套接字 */
Socket s;
/** 用戶的下載路徑 */
File file;
/** 接收傳過來的map集合 */
Map<String, File> map;
/** 接收文本框內(nèi)容 */
String msg;
/** 接受所有文件信息 */
String show="";
public DownlownServer(Socket s, File file) {
this.s = s;
this.file = file;
}
@Override
public void run() {
// 創(chuàng)建線程執(zhí)行下載任務(wù)
// 反對象序列化(得到map集合)
Object obj = null;
try {
obj = Tools.getObject(s.getInputStream());
} catch (ClassNotFoundException | IOException e2) {
// TODO 自動生成的 catch 塊
e2.printStackTrace();
}
// 得到Map集合
map = (HashMap<String, File>) obj;
// 將服務(wù)器文件信息顯示到這里
map.forEach((k, v) -> {
show = show + "文件名: " + k + "\n";
});
// 將可以下載的文件列出來
MyJFrame.jt.setText(show);
// 按鈕點擊事件下載文件
MyJFrame.jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 將選擇的文件名字傳給服務(wù)端
try {
msg = MyJFrame.jt1.getText().trim();
// 將下載文件名字傳給服務(wù)器
Tools.sendMsg(s.getOutputStream(), msg);
// 將服務(wù)端文件下載下來
File f = new File("");
// 接收原路徑
f = file;
file = new File(file, msg);
// 接收文件
Tools.getFile(s.getInputStream(), file);
MyJFrame.j1.setText("下載成功?。。?);
// 恢復原路徑可以多次下載
file = f;
//關(guān)閉套接字
s.close;
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
}
客戶端上傳線程:
package com.softeem.clound.cilent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.Map;
import com.softeem.clound.ToolsAndUI.MyJFrame;
import com.softeem.clound.ToolsAndUI.Tools;
/***
* 上傳線程
* @author 14252
*
*/
public class UpServer extends Thread{
/***/
Socket s;
File file;
Map<String, File> map;
String show;
public UpServer(Socket s) {
this.s = s;
}
@Override
public void run() {
MyJFrame.jb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//得到輸入的文件路徑
String content=MyJFrame.jt2.getText();
file=new File(content);
try {
//將文件信息通過對象序列化傳過去
Tools.transObject(file, s.getOutputStream());
//得到服務(wù)端響應避免阻塞
Tools.getMsg(s.getInputStream());
//開始傳輸文件
Tools.transFile(s.getOutputStream(), file);
//在界面顯示顯示上傳成功
MyJFrame.j2.setText("上傳成功");
MyJFrame.jt.setText(file.getName()+"上傳成功!");
// 通過半關(guān)閉將輸出流關(guān)閉,解決服務(wù)端阻塞問題
s.shutdownOutput();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
}
總結(jié):
這個小程序綜合性比較強,所以代碼量較多,可以一直下載或者上傳,多線程之間互不影響,每次點擊下載或者上傳都會創(chuàng)建一個線程進行下載或上傳,各個任務(wù)之間互不影響,上面程序還可以繼續(xù)擴展和優(yōu)化,比如加入進度顯示,大廳通知所有人某個用戶上傳了什么,下載了什么,這里不再敘述。
到此這篇關(guān)于用java寫一個云端資源共享小程序的文章就介紹到這了,更多相關(guān)java 云端資源共享小程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot接口請求入?yún)⒑统鰠⒃鰪姷奈宸N方法
這篇文章主要介紹了SpringBoot接口請求入?yún)⒑统鰠⒃鰪姷奈宸N方法,使用`@JsonSerialize`和`@JsonDeserialize`注解,全局配置Jackson的`ObjectMapper`,使用`@ControllerAdvice`配合`@InitBinder`,自定義HttpMessageConverter和使用AOP進行切面編程,需要的朋友可以參考下2024-07-07
Java?Web中ServletContext對象詳解與應用
ServletContext是一個容器,可以用來存放變量,供一個web項目中多個Servlet共享,下面這篇文章主要給大家介紹了關(guān)于Java?Web中ServletContext對象詳解與應用的相關(guān)資料,需要的朋友可以參考下2023-04-04
jcl與jul?log4j1?log4j2?logback日志系統(tǒng)機制及集成原理
這篇文章主要介紹了jcl與jul?log4j1?log4j2?logback的集成原理,Apache?Commons-logging?通用日志框架與日志系統(tǒng)的機制,有需要的朋友可以借鑒參考下2022-03-03
java使用TimerTask定時器獲取指定網(wǎng)絡(luò)數(shù)據(jù)
java.util.Timer定時器,實際上是個線程,定時調(diào)度所擁有的TimerTasks。一個TimerTask實際上就是一個擁有run方法的類,需要定時執(zhí)行的代碼放到run方法體內(nèi),TimerTask一般是以匿名類的方式創(chuàng)建,下面的就用示例來學習他的使用方法2014-01-01

