zoukankan      html  css  js  c++  java
  • servlet 读取文件

    读取pdf

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("application/pdf");
    		InputStream in = getServletContext().getResourceAsStream("/WEB-INF/java.pdf");
    		OutputStream out = response.getOutputStream();
    		writeBytes(in,out);
    	}
    
    	private void writeBytes(InputStream in, OutputStream out) throws IOException {
    		byte[] buffer =new byte[1024];
    		int length = -1;
    		while ((length=in.read(buffer))!=-1) {
    			out.write(buffer,0,length);
    		}
    		in.close();
    		out.close();
    		
    	}
    

    读取PNG

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("image/png");
    		File file = new File("F:\OneDrive\身份证\888.jpg");//获取文件流
    		System.out.println(file.getName());
    		InputStream in = new FileInputStream(file);//文件流转输入流
    		OutputStream out = response.getOutputStream();/获取输出流
    		writeBytes(in,out);//输出文件
    	}
    
    	private void writeBytes(InputStream in, OutputStream out) throws IOException {
    		byte[] buffer = new byte[1024];
    		int length=-1;
    		while((length=in.read(buffer))!=-1) {
    			out.write(buffer, 0, length);
    		}
    		in.close();
    		out.close();
    	}
    

      

  • 相关阅读:
    Web全栈探索,Vue基础系列,前后端交互(四)
    幸运者
    整数加法
    猜数游戏
    multi-select 左右选择插件
    Layui 踩坑记录
    jQuery 封装公共方法
    前后端文件内容 md5 计算
    Apscheduler 定时任务
    Git 开发流程
  • 原文地址:https://www.cnblogs.com/max-hou/p/11693291.html
Copyright © 2011-2022 走看看