-
Java调用Http/Https接口(1)--编写服务端
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设置方向代理来实现;由于这不是本系列文章的重点,这里就不详细介绍了,感兴趣的可以搜索研究。
来源:https://www.cnblogs.com/wuyongyin/p/11913419.html
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
JavaScript判断两个数组相等的四类方法
js如何操作video标签
React实战--利用甘特图和看板,强化Paas平
【记录】正则替换的偏方
前端下载 Blob 类型整理
抽象语法树AST必知必会
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程