IT序号网

java Web读取图片显示知识解答

flyfish 2021年06月13日 编程语言 134 0


工具类:


public static void saveFile(MultipartFile file,HttpServletRequest request,String name) throws IOException{ 
		 
		String path=upload_img_path; 
		 
		System.out.println("path:"+path); 
		 
		File folder = new File(path); 
        if(!folder.exists()){ 
        	folder.mkdirs(); 
        } 
        	 
		byte[] bytes = file.getBytes(); 
		BufferedOutputStream stream =new BufferedOutputStream(new FileOutputStream(new File(path+"/"+name))); 
		stream.write(bytes); 
		stream.close(); 
 
	}


读取流显示图片 


        @RequestMapping("/image") 
	@ResponseBody 
	public void getImage(HttpServletRequest request, HttpServletResponse response) throws Exception{ 
	    String JPG="image/jpeg;charset=GB2312"; 
	    String iname=request.getParameter("iname"); 
	    if(!StringUtil.isEmpty(iname)){ 
		    // 本地文件路径 
	        String filePath = UploadUtil.upload_img_path+"\\"+iname; 
	        File file = new File(filePath); 
	        // 获取输出流 
	        OutputStream outputStream = response.getOutputStream(); 
	        FileInputStream fileInputStream = new FileInputStream(file); 
	        // 读数据 
	        byte[] data = new byte[fileInputStream.available()]; 
	        fileInputStream.read(data); 
	        fileInputStream.close(); 
	        // 回写 
	        response.setContentType(JPG); 
	        outputStream.write(data); 
	        outputStream.flush(); 
	        outputStream.close(); 
	    } 
 
		 
	  
	}



发布评论
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Spring +MyBatis 配置文件概要说明知识解答
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。