首页 > 其他分享 > inputStream和outputStream互相转换

inputStream和outputStream互相转换

时间:2023-03-01 17:56:28浏览次数:41  
标签:outputStream 转换 ByteArrayOutputStream inputStream byteArrayOutputStream new impo

inputStream转换成outputStream

package org.example.base.controller;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author l
 * @date Created in 2021/5/25 14:33
 */
@RestController
@AllArgsConstructor
@RequestMapping("/heic")
public class HeicController {


    @GetMapping("/flower")
    public void flower(HttpServletResponse response) throws IOException {
        response.setContentType("image/heic");
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("static/images/Flower.heic");
        if (inputStream == null) {
            return;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int len;
        byte[] bytes = new byte[1024];
        while ((len = inputStream.read(bytes)) != -1) {
            byteArrayOutputStream.write(bytes, 0, len);
        }
        inputStream.close();
        byteArrayOutputStream.close();
        byteArrayOutputStream.writeTo(response.getOutputStream());
    }


}

 
outputStream 转换成inputStream

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());


标签:outputStream,转换,ByteArrayOutputStream,inputStream,byteArrayOutputStream,new,impo
From: https://www.cnblogs.com/tiancai/p/17169176.html

相关文章

  • 算法刷题-计算日期到天数转换-JAVA
    0x00引言为获取一个良好的算法思维,以及不再成为一个脚本小子,争取每天一道算法题,培养自己的逻辑思维,温顾各类型语言语法知识。题解只写自己理解的解法,其他解法不再增加。......
  • 【InputStream】Java中InputStream和String之间的转换方法
    【转载】https://blog.csdn.net/lmy86263/article/details/60479350在Java中InputStream和String之间的转化十分普遍,本文主要是总结一下转换的各种方法,包括JDK原生提供的,......
  • 文件编码转换utf-8更改为ascii
    #!/usr/bin/python#-*-coding:UTF-8-*-"""为啥搞这个?idea设置了自动转码,settings->editor->fileEncodings->Transparentnative-to-asciiconversionpropert......
  • 计算机中进位计数制及其转换
     计算机中常用的几种进位计数制的表示进位制基数基本符号权形式表示二进制20,12iB八进制80,1,2,3,4,5,6,78iO十进制100,1,2,3,4,5,6,7,8,910iD十六进制1......
  • C语言自动类型转换
    当运算符的两边出现不一致的类型时,会自动转换较大的类型下面是自动转换规则:char---->short---->int---->long---->longlongint---->float----->double注意:对于print......
  • C++string大小写转换
    #include<iostream>#include<string>#include<algorithm>usingnamespacestd;intmain(){stringstr="ancdANDG";cout<<"转换前的字符串:"<<str......
  • 进制转换
     1.8进制转16进制stringocts,hexs,t;voidOtoH(){for(inti=0;i<octs.length();i+=1){strings=octs.substr(i,1);if(s=="0")t+="000";......
  • 微信json对象 转换为json字符串传参给后端,后端解析
    普通js通常这样转换//这是一个json对象varjsonobj={"sztno":"330890811","transTime":"2018-5-708:00:36","amt":"50"};//json对象转成json字符串varjsonstr=JSON.s......
  • 前端页面查看pdf,后端数据返回blob;blob与json格式转换
    <a-drawertitle="查看报告"width="520":closable="false":visible="visible"@update:visible="updateVisible"@close="close"......
  • C# 货币金额中文(英文)大写转换方法-工具类
    1publicstaticclassMoney{2privatestaticreadonlyStringcnNumber="零壹贰叁肆伍陆柒捌玖";3privatestaticreadonlyStringcnUni......