JavaWeb--前端图片展示问题

本文介绍了一种在不同操作系统上加载图片的方法,特别是在Linux环境下如何处理服务器存储路径下的图片加载问题,并提供了一个具体的图片请求处理示例。

前台界面图片展示

  一般图片放置在系统中,通过http://ip:port/项目名/ImagePath就可显示该图片,如果该图片放置在服务器的其他存储路径下,这样的话Windows下直接使用file:///C:/image.jpg就可以了,但在Linux下,不行,最好写一个获取图片请求处理方法,

前台通过Img的src发送请求

var imgUrl="<c:url value='/manager/getImage.do?imagePath=tmp&imageName="+data.fileNamePath+"'/>";

$('#newForm').find("img").attr("src",imgUrl); 

 

请求代码如下:

	@RequestMapping(value = "/sceneManager/getImage.do")
	public ModelAndView getImage( @RequestParam(value ="imagePath")String imagePath,
			@RequestParam(value ="imageName")String imageName,
			HttpServletResponse response) {
		FileInputStream fis = null;
		response.setContentType("image/gif");
		try {
			OutputStream out = response.getOutputStream();
			String uploadDir = PropertyUtils.getProperty(SystemConstant.NAS_ROOT);
			String Path = PropertyUtils.getProperty(SystemConstant.SCENE_LOCAL_FILE_TMP_DIR);
			String documentPath = "";
			if(!StringUtils.equals("tmp", imagePath)){
				Path = PropertyUtils.getProperty(SystemConstant.SCENE_LOCAL_FILE_DIR);
				documentPath = uploadDir + Path;
			}
			 
			File file = new File(documentPath+FileUtils.fileSeparator+imageName);
			fis = new FileInputStream(file);
			byte[] b = new byte[fis.available()];
			fis.read(b);
			out.write(b);
			out.flush();
		} catch (Exception e) {
		     e.printStackTrace();
		} finally {
		    if (fis != null) {
		        try {
		           fis.close();
		        } catch (IOException e) {
				    e.printStackTrace();
				}   
		    }
		}
		return null;
	} 

 就可以了

 

JavaWeb里,要把图片返回给前端供用户下载,可借助Spring Boot框架达成。下面是实现步骤与示例代码: 1. **配置Spring Boot项目**:整合Spring Boot框架与数据库管理系统,构建高效、安全的图片管理系统,以便实现图片的上传、存储以及前端展示功能[^1]。 2. **从数据库获取图片数据**:从数据库取出图片数据。 3. **设置响应头**:在响应头里设定`Content-Disposition`,告知浏览器这是一个附件,需要下载。 4. **将图片数据写入响应输出流**:把图片数据写入`HttpServletResponse`的输出流。 示例代码如下: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; @Controller public class ImageDownloadController { @GetMapping("/downloadImage/{imageName}") public void downloadImage(@PathVariable String imageName, HttpServletResponse response) throws IOException { // 设置响应头 response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + imageName); // 从数据库或文件系统获取图片数据 // 这里假设图片存储在本地文件系统 String imagePath = "path/to/your/images/" + imageName; FileInputStream fis = new FileInputStream(imagePath); // 获取响应输出流 OutputStream os = response.getOutputStream(); // 将图片数据写入响应输出流 byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } // 关闭流 fis.close(); os.close(); } } ``` 在上述代码中: - 借助`@GetMapping`注解定义一个处理图片下载请求的接口`/downloadImage/{imageName}`。 - 设定响应头`Content-Disposition`,告知浏览器这是一个附件,需要下载。 - 从文件系统读取图片数据,再把数据写入`HttpServletResponse`的输出流。 若使用火狐浏览器下载有中文名的文件,可按如下方式设置响应头: ```java import java.io.UnsupportedEncodingException; import java.util.Base64; import javax.servlet.http.HttpServletResponse; // ... try { String fileName = "我就是要个中文名字.png"; String encodedFileName = "=?UTF-8?B?" + Base64.getEncoder().encodeToString(fileName.getBytes("UTF-8")) + "?="; response.setHeader("Content-Disposition", "attachment; filename=" + encodedFileName); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值