java Web读取图片显示

这篇博客介绍了如何在Java Web环境中读取图片文件流,并将其正确显示。通过使用特定的工具类,开发者可以有效地处理图片资源,实现网页上的图片展示功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


工具类:


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();
	    }

		
	 
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值