多平台统一管理软件接口,如何实现多平台统一管理软件接口
348
2022-08-30
使用HttpServletResponse对象获取请求行信息
目录HttpServletResponse对象获取请求行信息方法列表实例HttpServletResponse和HttpServletRequest解析HttpServletResponse和HttpServletRequestHttpServletResponse
HttpServletResponse对象获取请求行信息
方法列表
String reqMethod = request.getMethod()
String reqURI=request.getRequestURI();
String reqURI=request.getRequestURI();
StringBuffer reqURL=request.getRequestURL();
String reqPath=request.getContextPath();
String queryString=request.getQueryString();
String reqClient=request.getRemoteAddr();
实例
HTML
姓名:
年龄:
java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LineServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置编码方式
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
// 获得PrintWriter输出对象
PrintWriter writer = response.getWriter();
// 1、获取请求方式
String reqMethod = request.getMethod();
writer.write("请求方式method: " + reqMethod);
writer.print("
");
//2、获得请求的资源相关的内容
String reqURI=request.getRequestURI();
StringBuffer reqURL=request.getRequestURL();
writer.write("请求内容URI: " + reqURI);
writer.print("
");
writer.write("请求内容URL: " + reqURL);
writer.print("
");
//获得web应用的名称
String reqPath=request.getContextPath();
writer.write("web应用contextPath: " + reqPath);
writer.print("
");
//地址后的参数的字符串
String queryString=request.getQueryString();
writer.write("参数的字符串queryString: " + queryString);
writer.print("
");
//3、获得客户机的信息---获得访问者IP地址
String reqClient=request.getRemoteAddr();
writer.write("客户机的信息RemoteAddr: " + reqClient);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
HttpServletResponse和HttpServletRequest解析
最近有这么一个诉求,我在A服务器中的java调用执行一个python文件,并将其返回值返回给B服务器的客户端。当时在想A服务器暴露一个接口,然后使用runtime API调用即可,但是返回值这块我却有点苦恼。我的本jejyGOeD意是直接返回IO流,因为生成的数据可大可小,无规律,但是我发现这样是不可以的。
[org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class org.springframework.boot.loader.jar.JarURLConnection$2]
后来兜兜转转发现了HttpServletResponse 可以来解决这个问题。
HttpServletResponse和HttpServletRequest
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象。request和response对象即然代表请求和响应,那我们要获取客户机提交过来的数据,只需要找request对象就行了。要向客户机输出数据,只需要找response对象就行了。
本文主讲HttpServletResponse,HttpServletRequest碰到合适的场景我会将他补全。
HttpServletResponse
HttpServletResponse对象代表服务器的响应。这个对象中封装了向客户端发送数据、发送响应头,发送响应状态码的方法。
他的作用如下
1、保存流对象
使用HttpServletResponse 我们可以将IO流读出来在写入HttpServletResponse中的OutputStream中。然后别的客户端访问的时候方便获取
相关代码
@RequestMapping(value = "/testHdf")
@ResponseBody
public void testHdfs(String csv, String sql, HttpServletResponse response) {
BufferedInputStream bis = null;
try {
Process process = Runtime.getRuntime().exec("python /blazingsql/testData/zgh/aaa.py");
process.waitFor();
bis = new BufferedInputStream(process.getInputStream());
OutputStream os = null;
byte[] buff = new byte[1024];
os = response.getOutputStream();
int i = bis.read(buff);
while (i != -1) {
os.write(buff, 0, buff.length);
os.flush();
i = bis.read(buff);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、下载文件
使用HttpServletResponse可以下载文件,很方便
下载文件是,设置这些参数很重要
response.setHeader(“Content-Disposition”, “attachment;filename=”+ URLEncoder.encode(“demo.csv”, “UTF-8”));
response.setHeader(“Connection”, “close”);
response.setHeader(“Content-Type”, “application/octet-stream”);
相关代码
@RequestMapping(value = "/download")
@ResponseBody
public void download(String path,HttpServletResponse response) throws UnsupportedEncodingException {
response.reset();
response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("demo.csv", "UTF-8"));
response.setHeader("Connection", "close");
response.setHeader("Content-Type", "application/octet-stream");
OutputStream ops = null;
FileInputStream fis =null;
byte[] buffer = new byte[8192];
int bytesRead = 0;
try {
ops = response.getOutputStream();
fis = new FileInputStream(path);
while((bytesRead = fis.read(buffer, 0, 8192)) != -1){
ops.write(buffer, 0, bytesRead);
}
ops.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fis != null){
fis.close();
}
if(ops != null){
ops.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~