通过response向服务器用Io流写入图片

摘要:
1.响应头设置字节。
 
 1.响应头设置字节。
    使用response获得字节输出流  
     ServletOutputStream out = response.getOutputStream();
    获取文件的绝对路径
        String realPath = this.getServletContext().getRealPath("a.jpg");
    获得服务器上的图片
        FileInputStream in=new FileInputStream(realPath);
通过response向服务器用Io流写入图片第1张
package com.hdh.content;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ByteServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 用response获得字节输出流
        ServletOutputStream sos = response.getOutputStream();
        // 获取文件的绝对路径
        String realPath = this.getServletContext().getRealPath("a.jpg");
        // 获得服务器上的图片
        FileInputStream fis = new FileInputStream(realPath);

        int len = 0;
        byte[] buffer = new byte[1024];
        while ((len = fis.read(buffer)) > 0) {
            sos.write(buffer, 0, len);

        }
        fis.close();
        sos.close();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

免责声明:文章转载自《通过response向服务器用Io流写入图片》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇转 Android 4.0后,自定义Title报错 You cannot combine custom titles with other title featureHive 使用Tez引擎的配置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

.Net操作Excel —— NPOI

近期的两个项目都有关于NPOI的功能,经过了一点学习,自己也摸索了一会,感觉还有点意思。现在将部分代码分享一下。一部分是C#代码,一部分是VB.Net的,懒得修改了,基本上都是从项目文件中copy出来的。如果错漏,请指教。 概述: 1、整个Excel表格:WorkBook(工作薄),包含的叫页(工作表):Sheet;行:Row;单元格Cell。 2、NPO...

NODE_ENV判断node服务器环境的设置

build->webpack.sit.conf.js 拷贝webpack.prod.conf.js,新建webpack.sit.conf.js、webpack.uait.conf.js,根据环境修改env的引入文件 config->sit.env.js build->bulid.js src -> config文件夹 -&...

http协议的POST传数据

PostRequest使用StreamWriter对象写入请求流,不需要使用HttpUtility.UrlEncode显示转码,而下面的需要显示转码,还需要将参数转为字节码 蛋疼…………。 public static string PostRequest(string url, string postData) { HttpWebRequest httpW...

Servlet第三篇【request和response简介、response的常见应用】

response、request对象 Tomcat收到客户端的http请求,会针对每一次请求,分别创建一个代表请求的request对象、和代表响应的response对象 既然request对象代表http请求,那么我们获取浏览器提交过来的数据,找request对象即可。response对象代表http响应,那么我们向浏览器输出数据,找response对象...

.NET ActionFilterAttribute等

public override void OnException(HttpActionExecutedContext actionExecutedContext){//加LOG actionExecutedContext.Exception //2.返回调用方具体的异常信息if (actionExecutedContext.Exception is Not...

C#微信公众平台开发者模式开启代码

using System;using System.IO;using System.Text;using System.Web.Security; namespace HPZJ.Web.sys.excel{    public partial class hpd_api_weixin : System.Web.UI.Page    {        con...