Http接口输入的数据一般是键值对或json数据,返回的一般是json数据。本系列文章主要介绍Java调用Http接口的各种方法,本文主要介绍服务端的编写,方便后续文章里的客户端的调用。文中所使用到的软件版本:Java 1.8.0_191、SpringBoot 2.2.1.RELEASE。

1、服务端Controller

package com.inspur.demo.http.server;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import com.inspur.demo.common.entity.CallResult;
import com.inspur.demo.common.util.FileUtil;
import com.inspur.demo.http.entity.User; @Controller
@RequestMapping(value="/httptest/", method = {RequestMethod.GET, RequestMethod.POST})
public class HttpTestController {
private static Logger logger = LoggerFactory.getLogger(HttpTestController.class); @RequestMapping(value = "getUser", produces = "application/json;charset=UTF-8")
@ResponseBody
public CallResult<User> getUser(String userId, String userName) {
logger.info("userId={},userName={}", userId, userName);
User user = new User();
user.setUserId(userId);
user.setUserName(userName);
CallResult<User> result = new CallResult<User>(0, "OK", user);
return result;
} /**
* 传入json
* @param user
* @return
*/
@RequestMapping("addUser")
@ResponseBody
public CallResult<User> addUser(@RequestBody User user) {
logger.info(user.toString());
CallResult<User> result = new CallResult<User>(0, "OK", user);
return result;
} /**
* 上传文件
* 这种方式不适合页面form表单上传文件,适合客户端调用
* @param request
* @return
*/
@RequestMapping(value = "upload", produces = "application/json;charset=UTF-8")
@ResponseBody
public CallResult<String> upload(HttpServletRequest request) {
InputStream in = null;
OutputStream out = null;
CallResult<String> result = new CallResult<String>(0, "OK", "上传成功");
try {
in = new BufferedInputStream(request.getInputStream(), 16 * 1024);
//假设上传的就是jpg文件
String fileName = "d:/temp/upload_" + System.currentTimeMillis() + ".jpg";
out = new BufferedOutputStream(new FileOutputStream(fileName), 16 * 1024);
byte[] buffer = new byte[16 * 1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
result = new CallResult<String>(-1, "发生异常", "");
e.printStackTrace();
} finally {
FileUtil.close(in);
FileUtil.close(out);
}
logger.info("upload返回结果:{}", result);
return result;
} /**
* 上传文件及发送键值对数据
* @param file
* @param param1
* @param param2
* @return
*/
@RequestMapping("multi")
@ResponseBody
public CallResult<String> multi(@RequestParam("file") MultipartFile file, String param1, String param2) {
logger.info("file={},param1={},param2={}", file.getOriginalFilename(), param1, param2);
InputStream in = null;
OutputStream out = null;
CallResult<String> result = new CallResult<String>(0, "OK", "上传成功");
try {
in = new BufferedInputStream(file.getInputStream(), 16 * 1024);
String originalFilename = file.getOriginalFilename();
//ie上传文件该值是全路径,处理下
if (originalFilename.indexOf("\\") > -1) {
originalFilename = originalFilename.substring(originalFilename.indexOf("\\") + 1);
}
String fileName = "d:/temp/multi_" + System.currentTimeMillis() + "_" + originalFilename;
out = new BufferedOutputStream(new FileOutputStream(fileName), 16 * 1024);
byte[] buffer = new byte[16 * 1024];
int len = 0;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
result = new CallResult<String>(-1, "发生异常", "");
e.printStackTrace();
} finally {
FileUtil.close(in);
FileUtil.close(out);
}
logger.info("multi返回结果:{}", result);
return result;
} /**
* 下载文件
* @param request
* @param response
*/
@RequestMapping("download")
public void download(HttpServletRequest request, HttpServletResponse response) {
int BUFFER_SIZE = 16 * 1024;
BufferedInputStream bis = null;
OutputStream out = null;
try {
String fileName = "a.jpg";
String urlFileName = "";
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
urlFileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
} else {
urlFileName = java.net.URLEncoder.encode(fileName, "UTF-8");
} response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + urlFileName + "\"");
response.setHeader("Connection", "close"); bis = new BufferedInputStream(new FileInputStream("d:/" + fileName), BUFFER_SIZE);
out = new BufferedOutputStream(response.getOutputStream(), BUFFER_SIZE);
byte buf[] = new byte[BUFFER_SIZE];
int len;
while ((len = bis.read(buf)) != -1) {
out.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
FileUtil.close(bis);
FileUtil.close(out);
}
}
}

2、其他辅助类

2.1、CallResult类

package com.inspur.demo.common.entity;

import java.io.Serializable;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; /**
* @param <T>
*/
public class CallResult<T> implements Serializable {
private static final long serialVersionUID = 1L; /**
* 返回码
* 0 正常,其他异常
*/
private int returnCode = 0; /**
* 描述
*/
private String description = "OK"; /**
* 结果数据
*/
private T result; public CallResult(){} public CallResult(int returnCode, String description) {
this.returnCode = returnCode;
this.description = description;
} public CallResult(int returnCode, String description, T result) {
this.returnCode = returnCode;
this.description = description;
this.result = result;
} public int getReturnCode() {
return returnCode;
}
public void setReturnCode(int returnCode) {
this.returnCode = returnCode;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
} @Override
public String toString() {
//return JSON.toJSONString(this, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty);
try {
return new ObjectMapper().writeValueAsString(this);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "";
}
}

2.2、User类

package com.inspur.demo.http.entity;

public class User {
private String userId;
private String userName; public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
} @Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + "]";
}
}

2.2、FileUtil类

package com.inspur.demo.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; /**
* 文件操作工具
*/
public class FileUtil {
private FileUtil() {} public static void close(InputStream in) {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} public static void close(OutputStream out) {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} }

3、接口地址

在本地部署后,访问地址为:

http://localhost:8080/demo/httptest/getUser

http://localhost:8080/demo/httptest/addUser

http://localhost:8080/demo/httptest/upload

http://localhost:8080/demo/httptest/multi

http://localhost:8080/demo/httptest/download

4、Https接口

Https接口可以通过openssl生成证书、nginx设置方向代理来实现;由于这不是本系列文章的重点,这里就不详细介绍了,感兴趣的可以搜索研究。

5、客户端

本文主要是服务端的实现,客户端参见:

Java调用Http/Https接口(2)--HttpURLConnection/HttpsURLConnection调用Http/Https接口

Java调用Http/Https接口(3)--Commons-HttpClient调用Http/Https接口

Java调用Http/Https接口(4)--HttpClient调用Http/Https接口

Java调用Http/Https接口(5)--HttpAsyncClient调用Http/Https接口

Java调用Http/Https接口(6)--RestTemplate调用Http/Https接口

Java调用Http/Https接口(7,end)--WebClient调用Http/Https接口

Java调用Http/Https接口(1)--编写服务端的更多相关文章

  1. Java调用Http/Https接口(7,end)--WebClient调用Http/Https接口

    WebClient是Spring提供的非阻塞.响应式的Http客户端,提供同步及异步的API,将会代替RestTemplate及AsyncRestTemplate.文中所使用到的软件版本:Java 1 ...

  2. Java调用Http/Https接口(6)--RestTemplate调用Http/Https接口

    RestTemplate是Spring提供的用于访问Http接口的客户端,提供同步的API:在将来的Spring版本中可能会过时,将逐渐被WebClient替代.文中所使用到的软件版本:Java 1. ...

  3. Java调用Http/Https接口(5)--HttpAsyncClient调用Http/Https接口

    HttpAsyncClient是HttpClient的异步版本,提供异步调用的api.文中所使用到的软件版本:Java 1.8.0_191.HttpClient 4.1.4. 1.服务端 参见Java ...

  4. Java调用Http/Https接口(4)--HttpClient调用Http/Https接口

    HttpClient是Apache HttpComponents项目下的一个组件,是Commons-HttpClient的升级版,两者api调用写法也很类似.文中所使用到的软件版本:Java 1.8. ...

  5. Java调用Http/Https接口(3)--Commons-HttpClient调用Http/Https接口

    Commons-HttpClient原来是Apache Commons项目下的一个组件,现已被HttpComponents项目下的HttpClient组件所取代:作为调用Http接口的一种选择,本文介 ...

  6. Java调用Http/Https接口(2)--HttpURLConnection/HttpsURLConnection调用Http/Https接口

    HttpURLConnection是JDK自身提供的网络类,不需要引入额外的jar包.文中所使用到的软件版本:Java 1.8.0_191. 1.服务端 参见Java调用Http接口(1)--编写服务 ...

  7. Java调用第三方http接口的方式

    1. 概述 在实际开发过程中,我们经常需要调用对方提供的接口或测试自己写的接口是否合适.很多项目都会封装规定好本身项目的接口规范,所以大多数需要去调用对方提供的接口或第三方接口(短信.天气等). 在J ...

  8. Java实现UDP之Echo客户端和服务端

    Java实现UDP之Echo客户端和服务端 代码内容 采用UDP协议编写服务器端代码(端口任意) 编写客户机的代码访问该端口 客户机按行输入 服务器将收到的字符流和接收到的时间输出在服务器consol ...

  9. Java实现TCP之Echo客户端和服务端

    Java实现TCP之Echo客户端和服务端 代码内容 采用TCP协议编写服务器端代码(端口任意) 编写客户机的代码访问该端口 客户机按行输入 服务器将收到的字符流和接收到的时间输出在服务器consol ...

随机推荐

  1. GO语言GIN框架入门

    Gin框架介绍 Gin是一个用Go语言编写的web框架.它是一个类似于martini但拥有更好性能的API框架, 由于使用了httprouter,速度提高了近40倍. 中文文档 Gin框架安装与使用 ...

  2. hdoj - 1181 变形课

    Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个 ...

  3. Django入门——《Python编程从入门到实践》

    Django是一个Web框架--一套用于帮助开发交互式网站的工具.Django能够响应网页请求,还能让你更轻松地读写数据库.管理用户等. 1.建立项目 开始编写一个名为"学习笔记" ...

  4. C#中将xml文件反序列化为实例时采用基类还是派生类的问题

    基类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  5. 【转载】 LSTM构建步骤以及static_rnn与dynamic_rnn之间的区别

    原文地址: https://blog.csdn.net/qq_23981335/article/details/89097757 --------------------- 作者:周卫林 来源:CSD ...

  6. HTTP协议复习

    HTTP请求/响应的步骤: 客户端连接到WEB服务器:浏览器与web服务器的HTTP端口建立一个TCP套接字连接,例如:http://www.baidu.com 发送HTTP请求:通过TCP套接字,客 ...

  7. 【翻译】Flink Table Api & SQL — 内置函数

    本文翻译自官网:Built-In Functions  https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/fu ...

  8. tensorflow2.0手写数字识别

    import tensorflow as tf import matplotlib.pyplot as plt import numpy as np datapath = r'D:\data\ml\m ...

  9. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  10. [LeetCode] 463. Island Perimeter 岛的周长

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...