java直接在response中添加写入返回信息

工具代码


一般用于网关返回提示信息,会将信息直接写入response,。

/**
	 *	网络请求直接在response中返回信息
	 * @param map 返回response中的信息
	 * @param request
	 * @param response
	 * @param code
	 * @param msg
	 * @throws JsonProcessingException
	 * @throws IOException
	 */
	public static void outPrint(Map<String,Object> map, HttpServletRequest request, HttpServletResponse response, Integer code, String msg) throws JsonProcessingException, IOException {
		HttpServletResponse httpresponse = (HttpServletResponse) response;
		Map<String, Object> m=new HashMap<>();
		if(!map.isEmpty()){
			for(Map.Entry<String,Object> me:map.entrySet()){
				String key=me.getKey();
				Object value=me.getValue();
				m.put(key,value);
			}
		}
		if(code == null) {
			m.put("code", 300);
		}else {
			m.put("code", code);
		}
		m.put("msg", msg);
		httpresponse.reset();
		httpresponse.setHeader("Access-Control-Allow-Credentials", "true");
		httpresponse.setHeader("Access-Control-Allow-Origin", "*");
		httpresponse.setHeader("Access-Control-Allow-Headers", request.getHeader("Access-Control-Request-Headers"));
		httpresponse.setContentType("application/json;charset=UTF-8");
		PrintWriter writer = null;
		try {
	        writer = httpresponse.getWriter();
	        writer.write(JSONObject.toJSONString(m));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null)
            	writer.close();
        }
	}
Java ,将 InputStream 的文件内容添加到 ZIP 包并作为响应流返回前台,可以按照以下步骤操作: 1. **创建 ZIP 文件流**: 首先,你需要使用 `java.util.zip` 包下的 `ZipOutputStream` 类来创建一个 ZIP 输出流。 ```java import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; // 创建一个临时的 ByteArrayOutputStream 来保存 ZIP 内容 ByteArrayOutputStream zipOutput = new ByteArrayOutputStream(); ZipOutputStream zipStream = new ZipOutputStream(zipOutput); ``` 2. **添加文件到 ZIP**: 然后遍历 InputStream 对象的每个文件,并将其添加到 ZIP ,创建对应的 `ZipEntry` 对象,设置合适的名称。 ```java List<InputStream> inputStreamList = ...; // 你的 InputStream 列表 for (InputStream is : inputStreamList) { String entryName = ...; // 每个文件在 ZIP 的名称 byte[] buffer = new byte[4096]; int read; try { ZipEntry zipEntry = new ZipEntry(entryName); zipStream.putNextEntry(zipEntry); while ((read = is.read(buffer)) != -1) { zipStream.write(buffer, 0, read); } zipStream.closeEntry(); // 关闭当前 ZIP Entry } catch (IOException e) { // 处理异常 } } ``` 3. **完成 ZIP 文件并关闭流**: 最后,确保关闭 ZIP 输出流以及所有 InputStream。 ```java try { zipStream.finish(); // 完成 ZIP 文件 } catch (IOException e) { // 处理异常 } zipStream.close(); // 关闭 ZIP 流 ``` 4. **响应发送**: 使用 `ServletResponse` 或 `HttpServletResponse` 的 `setContentType()` 和 `setContentLength()` 方法设置响应头,然后将 ZIP 内容转换为字节数组并发送。 ```java String contentType = "application/zip"; long contentLength = zipOutput.size(); // 设置响应头 response.setContentType(contentType); response.setContentLength((int) contentLength); // 获取 response 的输出流并写入 ZIP 内容 response.getOutputStream().write(zipOutput.toByteArray()); response.getOutputStream().flush(); response.getOutputStream().close(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值