java读取本地图片

阅读更多
IE当前7以上版本不支持直接在src上写本地硬盘地址来显示图片。因为我们只有通过后台在response中读到二进制流的方式来在前台显示图片。具体代码如下:
public void showPicture(){
		String id = ServletActionContext.getRequest().getParameter("id");//前台传来的存图片路径实体类的主键id
		HttpServletResponse response = ServletActionContext.getResponse();//struts2获取response
		if(id != null && !"".equals(id)){
			this.classicCases = this.classicCasesManager.findClassicCasesById(id);
			String pic_path = this.classicCases.getImagesLocalPath();//图片路径
			FileInputStream is;
			try {
				is = new FileInputStream(pic_path);
				int i = is.available(); // 得到文件大小
				byte data[] = new byte[i];
				is.read(data); // 读数据
				is.close();
				response.setContentType("image/*"); // 设置返回的文件类型
				OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
				toClient.write(data); // 输出数据
				toClient.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


jsp页面很简单,路径格式为,http://localhost:8080/projectName/*.action:prama=XXX

你可能感兴趣的:(java,jsp,ie)