您的位置:首頁技術文章
文章詳情頁

使用java實現云端資源共享小程序的代碼

瀏覽:92日期:2022-08-28 09:05:37

云端共享小程序:

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

使用java實現云端資源共享小程序的代碼

點擊下載后:

使用java實現云端資源共享小程序的代碼

具體不再詳述,看程序:服務端:

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;/*** * 云端共享服務器: * 實現多用戶云端共享資源 * @author gq * */public class CloudServer extends Thread {/** 套接字 */private Socket s;/** 文件Map集合 */Map<String, File> map = new HashMap<String, File>();/** 服務器暫存文件的文件夾(這里我用電腦作為服務器) */File file = new File('C:Users14252DesktopkeepFiles');/** * 定義構造器初始化套接字 * * @param s */public CloudServer(Socket s) {this.s = s;}@Overridepublic void run() {File files[] = file.listFiles();// 先將服務器文件加入到結合中去for (File file : files) {map.put(file.getName(), file);}// 先詢問用戶下載還是上傳try {/* * // 將選擇發送給用戶 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());// 得到用戶下載文件的名字(用戶發來的名字可能有空格,去除空格)String name = Tools.getMsg(s.getInputStream()).trim();// 通過對面傳過來的文件名找到文件File file = map.get(name);// 將文件傳輸給用戶Tools.transFile(s.getOutputStream(), file);// 將傳輸成功的信息打印到控制臺Tools.tips(file.getName() + ':傳輸完成');// 通過半關閉將輸出流關閉,解決服務端阻塞問題s.shutdownOutput();}/** * 用戶上傳文件 * * @throws IOException * @throws ClassNotFoundException */private void upLoad() throws ClassNotFoundException, IOException {// 通過對象序列化得到文件對象Object obj = Tools.getObject(s.getInputStream());File f = (File) obj;// 設置好文件路徑file = new File(file, f.getName());// 傳輸信息解決阻塞問題Tools.sendMsg(s.getOutputStream(), '');// 獲取文件Tools.getFile(s.getInputStream(), file);// 服務器控制臺顯示用戶下載成功Tools.tips(file.getName() + ' 文件上傳成功');}public static void main(String[] args) throws IOException {// 設置一個端口為5555的服務器ServerSocket server = new ServerSocket(5555);// 不斷循環接收多個用戶請求while (true) {// 監聽用戶請求Socket s = server.accept();// 當監聽到用戶請求時,創建線程執行任務new CloudServer(s).start();// 將每個客戶進入到服務器的信息打印到控制臺Tools.tips('客戶端訪問了服務器:' + 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);}/** * 傳輸(發送)文件 * @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();}/** * 發送消息 * @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();/** 設置窗口 */jf.setTitle('云端文件共享mini版');jf.setSize(520, 700);jf.setLayout(null);jf.setVisible(true);jf.setResizable(false);// 設置容器Container c = jf.getContentPane();/** 設置滾動條以及文本框 */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);/** 設置上傳下載按鈕 */jbDown.setBounds(50, 280, 100, 40);jbUp.setBounds(350, 280, 100, 40);c.add(jbUp);c.add(jbDown);/** 設置按鈕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);/** 設置按鈕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));/** 設置背景顏色 */c.setBackground(Color.PINK);}}

服務端:

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;/** 定義一個狀態(1 下載 2 上傳) */String ss;/** 服務器ip地址 */private String ip;/** 服務器端口號 */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() {@Overridepublic void actionPerformed(ActionEvent e) {try {s = new Socket(ip, port);} catch (IOException e2) {e2.printStackTrace();}// 將你的選擇發送給服務端(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() {@Overridepublic void actionPerformed(ActionEvent e) {try {MyJFrame.jt.setText('請輸入您要上傳文件所在路徑!');s = new Socket(ip, port);// 將選擇發給服務端ss = '2';Tools.sendMsg(s.getOutputStream(), ss);// 開啟上傳線程new UpServer(s).start();} catch (IOException e1) {e1.printStackTrace();}}});}/** * 開始任務 * * @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:Users14252Desktop新建文件夾 (4)');// 端口號int port = 5555;// ip地址String ip = '192.168.0.102';// 調用選擇進行操作的方法new CilentSharing(f, ip, port).choose();}}

客戶端下載服務線程:

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;/** * 創建線程回應下載點擊事件 * * @author gq * */public class DownlownServer extends Thread {/** socket套接字 */Socket s;/** 用戶的下載路徑 */File file;/** 接收傳過來的map集合 */Map<String, File> map;/** 接收文本框內容 */String msg;/** 接受所有文件信息 */String show='';public DownlownServer(Socket s, File file) {this.s = s;this.file = file;}@Overridepublic void run() {// 創建線程執行下載任務// 反對象序列化(得到map集合)Object obj = null;try {obj = Tools.getObject(s.getInputStream());} catch (ClassNotFoundException | IOException e2) {// TODO 自動生成的 catch 塊e2.printStackTrace();}// 得到Map集合map = (HashMap<String, File>) obj;// 將服務器文件信息顯示到這里map.forEach((k, v) -> {show = show + '文件名: ' + k + 'n';});// 將可以下載的文件列出來MyJFrame.jt.setText(show);// 按鈕點擊事件下載文件MyJFrame.jb1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 將選擇的文件名字傳給服務端try {msg = MyJFrame.jt1.getText().trim();// 將下載文件名字傳給服務器Tools.sendMsg(s.getOutputStream(), msg);// 將服務端文件下載下來File f = new File('');// 接收原路徑f = file;file = new File(file, msg);// 接收文件Tools.getFile(s.getInputStream(), file);MyJFrame.j1.setText('下載成功?。。?);// 恢復原路徑可以多次下載file = f;//關閉套接字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;}@Overridepublic void run() {MyJFrame.jb2.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//得到輸入的文件路徑String content=MyJFrame.jt2.getText();file=new File(content);try {//將文件信息通過對象序列化傳過去Tools.transObject(file, s.getOutputStream());//得到服務端響應避免阻塞Tools.getMsg(s.getInputStream());//開始傳輸文件Tools.transFile(s.getOutputStream(), file);//在界面顯示顯示上傳成功MyJFrame.j2.setText('上傳成功');MyJFrame.jt.setText(file.getName()+'上傳成功!');// 通過半關閉將輸出流關閉,解決服務端阻塞問題s.shutdownOutput();} catch (IOException e1) {e1.printStackTrace();} }});}}

總結:

這個小程序綜合性比較強,所以代碼量較多,可以一直下載或者上傳,多線程之間互不影響,每次點擊下載或者上傳都會創建一個線程進行下載或上傳,各個任務之間互不影響,上面程序還可以繼續擴展和優化,比如加入進度顯示,大廳通知所有人某個用戶上傳了什么,下載了什么,這里不再敘述。

到此這篇關于用java寫一個云端資源共享小程序的文章就介紹到這了,更多相關java 云端資源共享小程序內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
国产综合久久一区二区三区