Unity3d搭建HTTP弱聯網的伺服器搭建及客戶端編寫(一)之java伺服器
HTTP弱聯網java伺服器
通過tomcat搭建java伺服器(linux
1、java環境搭建
java環境較容易搭建,在網上很容易找到相關的教程。
2、tomcat環境搭建
開始配置tomcat:
複製解壓後的檔案到 /opt 目錄
sudo cp -r apache-tomcat-8.0.12 /opt
進入 /opt/apache-tomcat-8.0.12 目錄
cd /opt/apache-tomcat-8.0.12
開啟啟動的指令碼檔案
sudo vi ./bin/startup.sh(不熟悉vi的,可以用gedit開啟:sudo gedit./bin/startup.sh)
拓展:vi編輯器在linux的版本中幾乎都有,但是gedit不一定有,如果需要,就得自己安裝:
sudo apt-get install gedit
開啟startup.sh檔案後,新增jdk和jre:
JAVA_HOME=/usr/java/jdk1.8.0_20
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME
CLASSPATH=.:$JRE_HOME/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
TOMCAT_HOME=/opt/apache-tomcat-8.0.12
JAVA_HOME和JRE_HOME 是已經配置好 JDK和JRE 目錄
TOMCAT_HOME 為當前 apache-tomcat-8.0.12 目錄
ps:注意,在安裝tomcat之前先要安裝jdk和jre,我安裝的是jdk1.8.0_20的java版本。
啟動tomcat: sudo ./bin/startup.sh
驗證tomcat配置和安裝是否成功:在瀏覽器中輸入:http://localhost:8080/
關閉tomcat:sudo ./bin/shutdown.sh
3、伺服器搭建
首先伺服器一般有post和get兩個方法。分別是伺服器處理客戶端發來的資訊和客戶端申請伺服器的相關資訊。
這裡我通過javaWeb的servlet來完成http的伺服器
@WebServlet(“/myServlet”)
servlet可以直接通過@WebServlet標籤配置URL對映。
doGet方法和doPost方法分別即是伺服器的post和get方法
當客戶端通過WWW方法傳送資料過來後,即呼叫doPost方法進行處理完成相關伺服器的邏輯
request.setCharacterEncoding(“utf-8”)
response.setCharacterEncoding(“utf-8”)
設定request和response的字型格式為utf-8
request.getParameter(“model”)
request.getParameter(“文字”),這裡文字即是Unity3d中WWW中getForm.AddField(“文字”, “資料”);的文字,通過該方法即可得到對應文字中的資料資訊
PrintWriter out = response.getWriter();
out.print(“文字”);
通過response.getWriter();方法可以得到伺服器返回給客戶端的輸出流,out.print(“文字”);中的文字即是你將會返回給客戶端的相關資訊,之後你可以在客戶端再對得到的資訊進行相關的處理。
以下的程式碼是我這次試驗中用到的,主要是返回在該專案中的一個txt檔案中的相關資訊。XML、json同理。java伺服器篇就到此為止了,下一篇會是php的伺服器教程。
package com;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class myServlet
*/
@WebServlet("/myServlet")
public class myServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public myServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("doget");
getInfo(request.getRealPath("/"), response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
System.out.println(request.getParameter("model"));
if (request.getParameter("model").equals("Get")) {
doGet(request, response);
} else {
System.out.println("dopost");
PrintWriter out = response.getWriter();
saveInfo(request.getRealPath("/"), request);
if (myBean.isAchievement()) {
out.print("true");
} else {
out.print("false");
}
}
}
private void getInfo(String filePath, HttpServletResponse response) {
filePath += "haha.txt";
System.out.println(filePath);
try {
String encoding = "utf-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判斷檔案是否存在
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考慮到編碼格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
PrintWriter out = response.getWriter();
while ((lineTxt = bufferedReader.readLine()) != null) {
out.println(lineTxt);
}
bufferedReader.close();
read.close();
} else {
System.out.println(filePath);
System.out.println("找不到指定的檔案");
}
} catch (Exception e) {
System.out.println("讀取檔案內容出錯");
e.printStackTrace();
}
}
private void saveInfo(String filePath, HttpServletRequest request) {
filePath += "haha.txt";
try {
String encoding = "utf-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判斷檔案是否存在
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, true), encoding);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
System.out.println("append");
bufferedWriter.append(request.getParameter("text"));
bufferedWriter.close();
writer.close();
myBean.setAchievement(true);
} else {
System.out.println(filePath);
System.out.println("找不到指定的檔案");
}
} catch (Exception e) {
System.out.println("讀取檔案內容出錯");
e.printStackTrace();
}
}
}