亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

使用java實(shí)現(xiàn)云端資源共享小程序的代碼

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

云端共享小程序:

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

使用java實(shí)現(xiàn)云端資源共享小程序的代碼

點(diǎn)擊下載后:

使用java實(shí)現(xiàn)云端資源共享小程序的代碼

具體不再詳述,看程序:服務(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ù)器: * 實(shí)現(xiàn)多用戶(hù)云端共享資源 * @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:Users14252DesktopkeepFiles');/** * 定義構(gòu)造器初始化套接字 * * @param s */public CloudServer(Socket s) {this.s = s;}@Overridepublic void run() {File files[] = file.listFiles();// 先將服務(wù)器文件加入到結(jié)合中去for (File file : files) {map.put(file.getName(), file);}// 先詢(xún)問(wèn)用戶(hù)下載還是上傳try {/* * // 將選擇發(fā)送給用戶(hù) String msg = '歡迎進(jìn)入云端共享(先選擇您的操作)'; * Tools.sendMsg(s.getOutputStream(), msg); */// 接收到用戶(hù)的回復(fù)String s1 = Tools.getMsg(s.getInputStream());// 得到用戶(hù)請(qǐng)求進(jìn)行操作if (s1.equals('1')) {Tools.tips('用戶(hù)選擇了下載操作!');downLoad();} else if ('2'.equals(s1)) {Tools.tips('用戶(hù)選擇了上傳操作!');upLoad();}} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}}/** * 下載文件 * * @throws IOException */private void downLoad() throws IOException {/** * 將文件集合序列化傳給用戶(hù) */Tools.transObject(map, s.getOutputStream());// 得到用戶(hù)下載文件的名字(用戶(hù)發(fā)來(lái)的名字可能有空格,去除空格)String name = Tools.getMsg(s.getInputStream()).trim();// 通過(guò)對(duì)面?zhèn)鬟^(guò)來(lái)的文件名找到文件File file = map.get(name);// 將文件傳輸給用戶(hù)Tools.transFile(s.getOutputStream(), file);// 將傳輸成功的信息打印到控制臺(tái)Tools.tips(file.getName() + ':傳輸完成');// 通過(guò)半關(guān)閉將輸出流關(guān)閉,解決服務(wù)端阻塞問(wèn)題s.shutdownOutput();}/** * 用戶(hù)上傳文件 * * @throws IOException * @throws ClassNotFoundException */private void upLoad() throws ClassNotFoundException, IOException {// 通過(guò)對(duì)象序列化得到文件對(duì)象Object obj = Tools.getObject(s.getInputStream());File f = (File) obj;// 設(shè)置好文件路徑file = new File(file, f.getName());// 傳輸信息解決阻塞問(wèn)題Tools.sendMsg(s.getOutputStream(), '');// 獲取文件Tools.getFile(s.getInputStream(), file);// 服務(wù)器控制臺(tái)顯示用戶(hù)下載成功Tools.tips(file.getName() + ' 文件上傳成功');}public static void main(String[] args) throws IOException {// 設(shè)置一個(gè)端口為5555的服務(wù)器ServerSocket server = new ServerSocket(5555);// 不斷循環(huán)接收多個(gè)用戶(hù)請(qǐng)求while (true) {// 監(jiān)聽(tīng)用戶(hù)請(qǐng)求Socket s = server.accept();// 當(dāng)監(jiān)聽(tīng)到用戶(hù)請(qǐng)求時(shí),創(chuàng)建線程執(zhí)行任務(wù)new CloudServer(s).start();// 將每個(gè)客戶(hù)進(jìn)入到服務(wù)器的信息打印到控制臺(tái)Tools.tips('客戶(hù)端訪問(wèn)了服務(wù)器:' + s.getInetAddress().getHostAddress());}}}

工具類(lèi):

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;/** * 工具類(lèi) * @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;}/** * 簡(jiǎn)化輸出語(yǔ)句 * * @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();}/** * 定義泛型方法傳輸序列化對(duì)象 * @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);}/** * 定義泛型方法反序列化序列化對(duì)象 * @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;// 確認(rèn)按鈕public static JButton jb1 = new JButton('確認(rèn)');public static JButton jb2 = new JButton('確認(rèn)');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();// 標(biāo)簽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è)置滾動(dòng)條以及文本框 */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以及標(biāo)簽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以及標(biāo)簽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;/** * 客戶(hù)端 * * @author gq */public class CilentSharing {/** 套接字 */private Socket s;/** 下載到的路徑 */File file1;/** 定義一個(gè)狀態(tài)(1 下載 2 上傳) */String ss;/** 服務(wù)器ip地址 */private String ip;/** 服務(wù)器端口號(hào) */private int port;public CilentSharing(File file1, String ip, int port) {this.file1 = file1;this.ip = ip;this.port = port;}public CilentSharing() {}/** * 選擇進(jìn)行的操作 * * @throws IOException * @throws ClassNotFoundException */private void choose() throws IOException, ClassNotFoundException {// 下載downLoad();// 上傳upLoad();}/** * 下載功能(點(diǎn)擊下載按鈕啟動(dòng)) */private void downLoad() {// 點(diǎn)擊下載按鈕開(kāi)始下載MyJFrame.jbDown.addActionListener(new ActionListener() {@Overridepublic 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);// 啟動(dòng)下載線程new DownlownServer(s, file1).start();return;} catch (IOException e1) {e1.printStackTrace();}}});}/** * 上傳文件功能(點(diǎn)擊上傳按鈕啟動(dòng)) */private void upLoad() {MyJFrame.jbUp.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {try {MyJFrame.jt.setText('請(qǐng)輸入您要上傳文件所在路徑!');s = new Socket(ip, port);// 將選擇發(fā)給服務(wù)端ss = '2';Tools.sendMsg(s.getOutputStream(), ss);// 開(kāi)啟上傳線程new UpServer(s).start();} catch (IOException e1) {e1.printStackTrace();}}});}/** * 開(kāi)始任務(wù) * * @throws ClassNotFoundException * @throws IOException */public static void main(String[] args) throws ClassNotFoundException, IOException {// 啟動(dòng)界面MyJFrame.showJFrame();// 說(shuō)明MyJFrame.jt.setText('歡迎使用云端共享!!!' + 'n' + '注意:' + 'n' + '在上傳文件填寫(xiě)路徑前或選擇下載文件時(shí)' + ',先選擇您的' + 'n' + '操作(上傳/下載)');// 文件下載的路徑(這里寫(xiě)自己的保存路徑)File f = new File('C:Users14252Desktop新建文件夾 (4)');// 端口號(hào)int port = 5555;// ip地址String ip = '192.168.0.102';// 調(diào)用選擇進(jìn)行操作的方法new CilentSharing(f, ip, port).choose();}}

客戶(hù)端下載服務(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)建線程回應(yīng)下載點(diǎn)擊事件 * * @author gq * */public class DownlownServer extends Thread {/** socket套接字 */Socket s;/** 用戶(hù)的下載路徑 */File file;/** 接收傳過(guò)來(lái)的map集合 */Map<String, File> map;/** 接收文本框內(nèi)容 */String msg;/** 接受所有文件信息 */String show='';public DownlownServer(Socket s, File file) {this.s = s;this.file = file;}@Overridepublic void run() {// 創(chuàng)建線程執(zhí)行下載任務(wù)// 反對(duì)象序列化(得到map集合)Object obj = null;try {obj = Tools.getObject(s.getInputStream());} catch (ClassNotFoundException | IOException e2) {// TODO 自動(dòng)生成的 catch 塊e2.printStackTrace();}// 得到Map集合map = (HashMap<String, File>) obj;// 將服務(wù)器文件信息顯示到這里map.forEach((k, v) -> {show = show + '文件名: ' + k + 'n';});// 將可以下載的文件列出來(lái)MyJFrame.jt.setText(show);// 按鈕點(diǎn)擊事件下載文件MyJFrame.jb1.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {// 將選擇的文件名字傳給服務(wù)端try {msg = MyJFrame.jt1.getText().trim();// 將下載文件名字傳給服務(wù)器Tools.sendMsg(s.getOutputStream(), msg);// 將服務(wù)端文件下載下來(lái)File f = new File('');// 接收原路徑f = file;file = new File(file, msg);// 接收文件Tools.getFile(s.getInputStream(), file);MyJFrame.j1.setText('下載成功!!!');// 恢復(fù)原路徑可以多次下載file = f;//關(guān)閉套接字s.close;} catch (IOException e1) {e1.printStackTrace();}}});}}

客戶(hù)端上傳線程:

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 {//將文件信息通過(guò)對(duì)象序列化傳過(guò)去Tools.transObject(file, s.getOutputStream());//得到服務(wù)端響應(yīng)避免阻塞Tools.getMsg(s.getInputStream());//開(kāi)始傳輸文件Tools.transFile(s.getOutputStream(), file);//在界面顯示顯示上傳成功MyJFrame.j2.setText('上傳成功');MyJFrame.jt.setText(file.getName()+'上傳成功!');// 通過(guò)半關(guān)閉將輸出流關(guān)閉,解決服務(wù)端阻塞問(wèn)題s.shutdownOutput();} catch (IOException e1) {e1.printStackTrace();} }});}}

總結(jié):

這個(gè)小程序綜合性比較強(qiáng),所以代碼量較多,可以一直下載或者上傳,多線程之間互不影響,每次點(diǎn)擊下載或者上傳都會(huì)創(chuàng)建一個(gè)線程進(jìn)行下載或上傳,各個(gè)任務(wù)之間互不影響,上面程序還可以繼續(xù)擴(kuò)展和優(yōu)化,比如加入進(jìn)度顯示,大廳通知所有人某個(gè)用戶(hù)上傳了什么,下載了什么,這里不再敘述。

到此這篇關(guān)于用java寫(xiě)一個(gè)云端資源共享小程序的文章就介紹到這了,更多相關(guān)java 云端資源共享小程序內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 高清免费a级在线观看国产 高清免费毛片 | 国产视频 一区二区 | 日本一区毛片免费观看 | 欧美一级级a在线观看 | 欧美在线第一二三四区 | 91sao国产在线观看 | 日本一级特黄完整大片 | 国产综合成色在线视频 | 男女很黄很色床视频网站免 | 日韩一级片播放 | 国产成人涩涩涩视频在线观看免费 | 一级黄色免费网站 | 麻豆影视视频高清在线观看 | 国产不卡在线观看 | a级黄毛片 | 亚洲高清在线 | wwwxxxx美国| 国产特级毛片aaaaaaa高清 | 免费一级 一片一毛片 | 中文字幕欧美日韩一 | 久久草在线看 | 精品一区二区久久久久久久网精 | 国产不卡的一区二区三区四区 | 国产 日韩 欧美 在线 | 免费观看一级成人毛片软件 | 最新精品视频在线观看 | 色香影视 | 国产成人综合网 | 免费久久精品视频 | 国产精品久久久久国产精品三级 | 久草在线国产视频 | 国产剧情视频在线观看 | 国产偷国产偷亚洲高清在线 | 国产一区二区三区在线观看精品 | 免费的黄视频 | 国产亚洲小视频 | 亚洲第一免费 | 国产日韩免费视频 | 国产毛片毛片精品天天看 | 国产精品亚洲片在线观看麻豆 | 精品一区二区久久久久久久网站 |