導航:首頁 > IDC知識 > java伺服器端

java伺服器端

發布時間:2020-10-04 08:31:58

1、java伺服器開發是做什麼?和web端的區別是什麼?

web開發,是開發服務端的,開發好的web程序,打包成war,然後放到web容器中運行,而web容器,是部署在伺服器中的。
web的客戶端就是瀏覽器,教你設計頁面,學CSS/HTML之類的。

標準的web伺服器只具有與客戶端瀏覽器通訊的功能,不能處理業務邏輯請求。
需要編寫程序來復制處理客戶端的請求。通過組件來處理客戶端的請求,這個組件就是實現特定規范的可以單獨部署的軟體模塊。組件必須通過容器來實現。容器是實現特定規范的程序,負責組件的運行環境和管理組件的生命周期。tomcat,weblogic都提供了容器。
web端可以理解為tomcat,並且tomcat中運行著你編寫的程序,這個程序稱為web應用。
java伺服器開發就是通過java語言來編寫程序,組合成web應用,將來部署到tomcat中,
編寫的這些程序就是組件,用來處理客戶端請求的。為了高效還會使用一些框架和技術來配合java程序,比如SpringMVC,struts2,Servlet。

2、Java用來編寫客戶端還是伺服器端?

兩者都可以...不過java的垃圾回收機制因為是自動的所以執行客戶端程序的時候效率特別慢...一般用於伺服器端或WEB開發...學java的重點就是以j2ee技術為核心的。想寫客戶端之類的用C語言是最佳的..不過內存處理機制全部自己編寫..難度不是一般的高。

3、java中如何實現從客戶端發送文件到伺服器端

伺服器端源碼:
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;

/**
*
* 文件名:ServerReceive.java
* 實現功能:作為伺服器接收客戶端發送的文件
*
* 具體實現過程:
* 1、建立SocketServer,等待客戶端的連接
* 2、當有客戶端連接的時候,按照雙方的約定,這時要讀取一行數據
* 其中保存客戶端要發送的文件名和文件大小信息
* 3、根據文件名在本地創建文件,並建立好流通信
* 4、循環接收數據包,將數據包寫入文件
* 5、當接收數據的長度等於提前文件發過來的文件長度,即表示文件接收完畢,關閉文件
* 6、文件接收工作結束

public class ServerReceive {

public static void main(String[] args) {

/**與伺服器建立連接的通信句柄*/
ServerSocket ss = null;
Socket s = null;

/**定義用於在接收後在本地創建的文件對象和文件輸出流對象*/
File file = null;
FileOutputStream fos = null;

/**定義輸入流,使用socket的inputStream對數據包進行輸入*/
InputStream is = null;

/**定義byte數組來作為數據包的存儲數據包*/
byte[] buffer = new byte[4096 * 5];

/**用來接收文件發送請求的字元串*/
String comm = null;

/**建立socekt通信,等待伺服器進行連接*/
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();
/**size為每次接收數據包的長度*/
int size = 0;
/**count用來記錄已接收到文件的長度*/
long count = 0;

/**使用while循環接收數據包*/
while(count < file_size){
/**從輸入流中讀取一個數據包*/
size = is.read(buffer);

/**將剛剛讀取的數據包寫到本地文件中去*/
fos.write(buffer, 0, size);
fos.flush();

/**將已接收到文件的長度+size*/
count += size;
System.out.println("伺服器端接收到數據包,大小為" + size);
}

} catch (FileNotFoundException e) {
System.out.println("伺服器寫文件失敗");
} catch (IOException e) {
System.out.println("伺服器:客戶端斷開連接");
}finally{
/**
* 將打開的文件關閉
* 如有需要,也可以在此關閉socket連接
* */
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;

/**
*
* 文件名:ClientSend.java
* 實現功能:作為客戶端向伺服器發送一個文件
*
* 具體實現過程:
* 1、建立與伺服器端的連接,IP:127.0.0.1, port:4004
* 2、將文件的名字和大小通過自定義的文件傳輸協議,發送到伺服器
* 3、循環讀取本地文件,將文件打包發送到數據輸出流中
* 4、關閉文件,結束傳輸
*
* */

public class ClientSend {

public static void main(String[] args) {

/**與伺服器建立連接的通信句柄*/
Socket s = null;

/**定義文件對象,即為要發送的文件
* 如果使用絕對路徑,不要忘記使用'/'和'\'的區別
* 具體區別,請讀者自行查詢
* */
File sendfile = new File("API.CHM");
/**定義文件輸入流,用來打開、讀取即將要發送的文件*/
FileInputStream fis = null;
/**定義byte數組來作為數據包的存儲數據包*/
byte[] buffer = new byte[4096 * 5];

/**定義輸出流,使用socket的outputStream對數據包進行輸出*/
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("未連接到伺服器");
}

/**用文件對象初始化fis對象
* 以便於可以提取出文件的大小
* */
try {
fis = new FileInputStream(sendfile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

/**首先先向伺服器發送關於文件的信息,以便於伺服器進行接收的相關准備工作
* 具體的准備工作,請查看伺服器代碼。
*
* 發送的內容包括:發送文件協議碼(此處為111)/#文件名(帶後綴名)/#文件大小
* */
try {
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("111/#" + sendfile.getName() + "/#" + fis.available());
ps.flush();
} catch (IOException e) {
System.out.println("伺服器連接中斷");
}

/**
* 此處睡眠2s,等待伺服器把相關的工作準備好
* 也是為了保證網路的延遲
* 讀者可自行選擇添加此代碼
* */
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}

/**之前的准備工作結束之後
* 下面就是文件傳輸的關鍵代碼
* */
try {

/**獲取socket的OutputStream,以便向其中寫入數據包*/
os = s.getOutputStream();

/** size 用來記錄每次讀取文件的大小*/
int size = 0;

/**使用while循環讀取文件,直到文件讀取結束*/
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{

/**
* 將打開的文件關閉
* 如有需要,也可以在此關閉socket連接
* */
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

4、java伺服器接收客戶端請求怎樣實現的

伺服器端代碼:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
* Socket通訊伺服器端
* @author 米強
*
*/
public class ServerMain {

public ServerMain() {
try {
// 構造伺服器ServerSocket對象,參數為伺服器端開放的埠號
ServerSocket ss = new ServerSocket(30102);
System.out.println("伺服器准備就緒!");
// 死循環可以使伺服器持續處於接收客戶端狀態
while(true){
// 該方法使程序阻塞,等待客戶端的鏈接,當監聽到客戶端的鏈接,創建一個Socket對象與客戶端單獨會話
Socket s = ss.accept();
// 為了不影響伺服器監聽其它客戶端,這里開啟了一個線程,由線程處理與這個客戶端的會話
new ServerThread(s).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new ServerMain();
}

}

/**
* 伺服器端與客戶端會話的線程
*/
class ServerThread extends Thread {
private Socket s = null;
private BufferedReader read = null;
private PrintStream print = null;

public ServerThread(Socket s) {
this.s = s;
try {
// 從Socket中獲取輸入流和輸出流,由於我們只做一個簡單的字元串通訊,所以採用BufferedRead和PrintStream來封裝輸入、輸出流
read = new BufferedReader(new InputStreamReader(s.getInputStream()));
print = new PrintStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 線程的運行run方法
*/
public void run() {
try {
String message = null;
// 這里循環可以使伺服器持續的接收客戶端信息。read.readLine()通過輸入流讀取一段字元串,賦值給message變數,如果message字元串不為「exit」則循環,否則結束循環
while (!(message = read.readLine()).equals("exit")){
// 將字元串前面添加「返回:」,再發回客戶端
print.println("返回:" + message);
}
} catch (IOException e) {
} finally {
// 在 finally 代碼塊中無論如何都會執行下面代碼:
try {
// 如果沒有關閉Socket
if(!s.isClosed()){
// 關閉Socket鏈接
s.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}

客戶端代碼:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
* Socket通訊客戶端
* @author 米強
*
*/
public class ClientMain {

public ClientMain() {
try {
// 構造與伺服器通訊的Socket對象,參數為伺服器IP地址(String)和埠號(int),埠號需要和伺服器端開放的埠號對應
Socket s = new Socket("192.168.1.100", 30102);
// 啟動一個線程與伺服器通訊,並把鏈接伺服器的Socket對象傳遞過去
new LinkThread(s).start();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
new ClientMain();
}

}

/**
* 與伺服器通訊的線程
*/
class LinkThread extends Thread {
private Socket s = null;
// 輸出流
private PrintStream out = null;
// 緩沖輸入流
private BufferedReader in = null;
// 錄入文字的Scanner對象
private Scanner scanner = null;

public LinkThread(Socket s) {
// 將Socket對象實例保存在全局變數中,因為run方法中我們還要用它斷開鏈接
this.s = s;
try {
// 從Socket中獲取輸入流和輸出流,由於我們只做一個簡單的字元串通訊,所以採用BufferedRead和PrintStream來封裝輸入、輸出流
out = new PrintStream(s.getOutputStream());
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 線程的運行run方法
*/
public void run() {
// 構造Scanner對象
scanner = new Scanner(System.in);
System.out.println("提示:如果要結束本次會話,請輸入「exit」指令!");
try {
// 死循環可以使客戶端不斷的向伺服器發送信息,不用擔心循環無法結束,後面的return語句可以結束整個線程。
while(true){
// 提示用戶輸入文字
System.out.print("請輸入:");
// 將用戶輸入的字元串保存在message變數中
String message = scanner.nextLine();
// 通過輸出流發送字元串
out.println(message);
// 清空緩沖,強制輸出
out.flush();
// 獲取伺服器返回的字元串
String str = in.readLine();
// 如果返回的字元串存在
if(str != null){
// 顯示在控制台
System.out.println(str);
}else{
// 提示會話結束,並結束線程
System.out.println("本次會話結束!");
return;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 在 finally 代碼塊中無論如何都會執行下面代碼:
try {
// 如果沒有關閉Socket
if(!s.isClosed()){
// 關閉Socket鏈接
s.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}

5、java 如何在使用java類 從客戶端下載伺服器上的文件

js 做不到 copy 到客戶端指定位置


如果說的是java的話, 可以做到

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/*
 * 文 件 名:  Test.java
 * 版    權:  XX Technologies Co., Ltd. Copyright YYYY-YYYY,  All rights reserved
 * 描    述:  <描述>
 * 修改時間:  2015-7-10
 * 跟蹤單號:  <跟蹤單號>
 * 修改單號:  <修改單號>
 * 修改內容:  <修改內容>
 */

/**
 * 
 * @version [版本號, 2015-7-10]
 * @see [相關類/方法]
 * @since [產品/模塊版本]
 */
public class Test
{
    public static void main(String[] args)
    {
        
        try
        {
            URLConnection openConnection = new URL("伺服器文件的訪問地址").openConnection();
            
            InputStream is = openConnection.getInputStream();
            
            byte[] buff = new byte[1024];
            int len;
            
            FileOutputStream fos = new FileOutputStream("c:你的文件名.擴展名");
            
            if (null != is)
            {
                
                while ((len = is.read(buff)) != -1)
                {
                    fos.write(buff, 0, len);
                }
            }
            fos.close();
            is.close();
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

6、java伺服器端「/「文件路徑如何書寫?

在java中路徑都這么寫:\\ , 這個就是路徑分隔符,以後就不用「/」了吧
不管是實在windows還是在linux中,你都用「\\」作為路徑分隔符就對了
File.separator()這個方法是沒什麼問題,但是,你如果傳入字元串後,
使用File.separator來split,可能會出錯的哦,你在所有的地方,都用「\\」,
肯定是不會錯的啦。

至於你說的json中也是,在java中就是用\\,這與json無關,而是轉義導致的

7、用java編寫app的伺服器端,需要用到什麼技術和框架

我也做服務端開發的,伺服器和客戶端傳輸數據使用到了servlet,為了提高效率使用了httpclient, 傳輸數據類型採用json,如果要跨語言開發那還要使用About thrift ,因為我們是做社交這塊的,所以還要用到java socket技術,推送消息用的是極光推送,框架的話使用輕量級spring ICO DI ,然後資料庫的話使用了三種 mongodb(主要使用) 、mysql(輔助)和redisdb(緩存)。大概就這么多了。還有app裡面不全是原生開發,還可以使用html5進行輔助開發。

與java伺服器端相關的知識