OutPutStream输出流

110 篇文章 0 订阅
18 篇文章 0 订阅
package com.yanshu.controller;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;


import javax.servlet.http.HttpServletResponse;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;


import ch.qos.logback.classic.pattern.FileOfCallerConverter;


/*
 * 需要的jar包
 * <dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>1.7</version>
</dependency>*/
/**
 * IntputStream  读取
 * int read( ); //读取一个字节,返回值为所读的字节 
int read( byte b[ ] ); //读取多个字节,放置到字节数组b中,通常读取的字节数量为b的长度,返回值为实际读取的字节的数量 
int read( byte b[ ], int off, int len ); //读取len个字节,放置到以下标off开始字节数组b中,返回值为实际读取的字节的数量 
int available( );   //返回值为流中尚未读取的字节的数量 
long skip( long n ); //读指针跳过n个字节不读,返回值为实际跳过的字节数量 


 * outputStream  输出
 * ◇ 输出数据: 
void write( int b );   //往流中写一个字节b 
void write( byte b[ ] ); //往流中写一个字节数组b 
void write( byte b[ ], int off, int len ); //把字节数组b中从下标off开始,长度为len的字节写入流中 


◇ flush( )  //刷空输出流,并输出所有被缓存的字节,由于某些流支持缓存功能,该方法将把缓存中所有内容强制输出到流中。  
 * @author 刘瑞光
 *
 */
@Controller
public class OutPutStreamController {

@RequestMapping(value="printWriter",method={RequestMethod.GET,RequestMethod.POST})
public void getPrintWriter(HttpServletResponse response) throws IOException
{
response.setContentType("text/html;charset=utf-8");
PrintWriter printWriter=response.getWriter();
printWriter.write("<p>200</p>");
printWriter.write("<p>PrintWriter</p>");
printWriter.flush();
printWriter.close();
}

@RequestMapping(value="opstram",method={RequestMethod.GET,RequestMethod.POST})
public void getStream(HttpServletResponse response) throws IOException
{
String data="天空中飘着云";
OutputStream outputStream=response.getOutputStream();
//html :<meta> 标签也能模拟响应头
outputStream.write("<meta  http-equiv='content-type' content='text/html;charset=utf-8/>".getBytes());
outputStream.write(data.getBytes("utf-8"));
outputStream.flush();
outputStream.close();
//return outputStream;

}


//http://localhost:8080/health/test
@RequestMapping(value="/test",method={RequestMethod.GET,RequestMethod.POST})
public OutputStream getStr(HttpServletResponse response) throws IOException
{
//响应到页面上
OutputStream outputStream=response.getOutputStream();
generateQR("22222", 500, 500,outputStream); 
outputStream.flush();
outputStream.close();
return outputStream;
}

public static void generateQR(String contents, int width, int height, OutputStream os) {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
//hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
System.out.println("1----->>>>>>>");
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
System.out.println("2----->>>>>>>");
MatrixToImageWriter.writeToStream(bitMatrix, "png", os);
System.out.println("3----->>>>>>>");

//bitMatrix.

//writeToFile(bitMatrix, "png", new File(imgPath));
} catch (Exception e) {
e.printStackTrace();
}
}
//当个Stream
public static  void FileAndStream() throws Exception{
File file= new File("d:" + File.separator + "test.txt") ;    // 声明File对象
System.err.println(file);
File f=new File("f://a.txt");//使用File类找到一个文件 文件不存在会自动生成
OutputStream outputStream=null;//实例化父类对象
outputStream=new FileOutputStream(f);
String stringoutPutStream="Hello World!!!";
byte b[]=stringoutPutStream.getBytes();
outputStream.write(b);//往流中写入一个字节数组[]
outputStream.flush();
outputStream.close();//关闭输出流
}
//多个stream
public static void moreFileAndStream() throws Exception
{
File file=new File("f://b.txt");//声明一个File
OutputStream outputStream=null;
outputStream=new FileOutputStream(file);//通过对象多态,进行实例化
String stringOut="Hello World!!! FileOutputStream";//准备一个字符串
byte b[]=stringOut.getBytes();//只能输出byte数组,所以将字符串变为byte
for(int i=0;i<b.length;i++)//采用循环的方式写入
{
outputStream.write(b[i]);//每次只写入一个内容
}
//关闭输出流
outputStream.close();

}

public static void addMoreFileAndStream() throws Exception
{
File file=new File("f:"+File.separator+"test.txt");//声明File对象
OutputStream outputStream=null;//准备好一个输出的对象
outputStream=new FileOutputStream(file,true);//此出表示文件末尾可以扩展
String stringOutPutStream="Hellow  sdnskanfmalvavavbag";
byte b[]=stringOutPutStream.getBytes();
for(int i=0;i<b.length;i++)
{
outputStream.write(b[i]);//每次只写入一个内容
}
outputStream.close();

}
public static void main(String[] args) throws Exception  {
FileAndStream();
// addMoreFileAndStream();
}




}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值