近期在学习一个C/S项目,作为一个之前只会写套皮B/S项目的摸鱼选手,只能说书到用时方恨少。虽然web前端+springboot项目非常常见,但基础的知识不能不学。遂在网络搜寻,幸得一点学习心得
要求:原本是一个完整的对接项目,但是由于服务端一直报乱码,因此便自己写一个服务端接收客户端传输的字节流,看看究竟是不是乱码。服务端仍旧采取springboot建立项目,写一个controller接收数据;客户端则简单采用main方法模拟
使用工具:intellij idea、postman(测试controller,可不用)
服务端springboot项目搭建:
新建项目,选择spring initializr(这里近做简单演示,笔者用的古早年间的现成项目,可以看一下笔者之前写的建立springboot项目的笔记
java版本不宜过高,容易不兼容sdk
由于仅仅想写一个接口,不需要数据库,因此依赖只选择spring web
项目建立好后,在application.properties写配置文件(笔者喜欢重构为yml文件,properties就写server.port=8081,8081是项目运行的端口号,随意指定不冲突的即可
server:
port: 8081
然后新建controller类
代码如下:
package com.example.demo_back.controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
@RestController
@CrossOrigin
public class HttpTestController
{
@RequestMapping("/httpByteTest")
public String httpByteTest(HttpServletRequest request) throws IOException
{
String result = "";
ServletInputStream inputStream= request.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
try {
StringBuilder buffer = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
buffer.append(line);
}
result=buffer.toString();
System.out.println("server returned what you said: "+result);
} catch (Exception e) {
e.printStackTrace();
} finally {
inputStream.close();
inputStreamReader.close();
bufferedReader.close();
}
return result;
}
}
运行application类,可以使用postman测试一下这个接口,不报错即可(这里返回是空的
这样服务端就建立好了
客户端就随便写个main函数,可以直接在这个controller类里面写
public static void main(String[]args) throws Exception
{
StringBuffer sbRet = new StringBuffer();
String strCBCURL = "http://localhost:8081/httpByteTest";
int connectTimeOut = Integer.valueOf(30000);
int readTimeOut = Integer.valueOf(100) * 1000;
HttpURLConnection httpUrlConnection = null;
URL url = null;
OutputStream outStrm = null;
InputStream inStrm = null;
try {
url = new URL(strCBCURL);
URLConnection rulConnection = url.openConnection();
rulConnection.setConnectTimeout(connectTimeOut);
rulConnection.setReadTimeout(readTimeOut);
httpUrlConnection = (HttpURLConnection) rulConnection;
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
httpUrlConnection.setRequestMethod("POST".toUpperCase());
outStrm = httpUrlConnection.getOutputStream();
outStrm.write("我踏马莱纳".getBytes("utf-8")); // 不设置编码则为项目默认编码
outStrm.flush();
inStrm = httpUrlConnection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inStrm));
String s;
while ((s = in.readLine()) != null && s.length() != 0) {
sbRet.append(s);
}
System.out.println("client got response from server:"+sbRet);
} catch (Exception e) {
throw new Exception("Servlet connect Exception:" + e.getMessage()); // �쳣
} finally {
if (outStrm != null) {
outStrm.close();
}
if (inStrm != null) {
inStrm.close();
}
}
}
再运行这个main方法(要和springboot项目同时运行
服务端控制台的输出:
客户端(main方法)控制台的输出:
总结:传输字节流其实和传输字符串请求差不多,用HttpServletRequest类型接收即可,且无需指定@RequestParam属性
传输和接收最好都注意一下编码问题,比如发送时使用getBytes(charset)来进行指定。