spring使用流读取指定路径图片


//流读取图片

package cn.fenghuait.controller;

import java.io.FileInputStream;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.fenghuait.service.SysParameterService;

@Controller
@RequestMapping("/picture")
public class ShowPicController extends BaseController{
  private static Log logger = LogFactory.getLog(ShowPicController.class);
  @Autowired
  private SysParameterService parameterService;
 
  @RequestMapping(value="/showPic")
  public @ResponseBody Map<String,Object> showPic(HttpServletRequest request,HttpServletResponse response) throws Exception{
    try{
      String basePath = parameterService.getProblemPic();
      String str = request.getParameter("picName");
        String realName = basePath+"/"+str;
        FileInputStream fs = new FileInputStream(realName);
        int b=fs.available(); //得到文件大小   
        byte data[]=new byte[b];   
        fs.read(data);  //读数据   
        response.setContentType("image/*"); //设置返回的文件类型   
        ServletOutputStream toClient=response.getOutputStream(); //得到向客户端输出二进制数据的对象   
        toClient.write(data);  //输出数据   
        toClient.flush();
        toClient.close();
        fs.close();
    }catch(Exception e){
      e.printStackTrace();
      logger.error(e);
    }
    return null;
    
  }
}



//查询图片名称

info=problemInfoService.getProblemInfoById(problemInfo.getProblemId());
           List<String> list = new ArrayList<String>();
       if(info.getPicturePath()!=null&&!"".equals(info.getPicturePath().trim())){
       String str = info.getPicturePath();
       String[] strs = str.split(",");
       String pic = "";
       for(int i = 0;i<strs.length;i++){
          pic = strs[i];
         list.add(pic);
       }
       }
          mav.addObject("problem", info);
          mav.addObject("realName", list);


//jsp显示图片

    <c:forEach items="${realName }" var="item">

                <li><a  target="_blank"><img src="${basePath}/picture/showPic?picName=${item}" alt="民情图片" /></a></li>
            </c:forEach>
Spring Boot中,你可以使用`Resource`和`InputStream`来读取指定路径的文件,并将文件内容返回为文件。以下是一个示例代码: ```java import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @RestController public class FileController { @GetMapping("/file/{filename}") public ResponseEntity<Resource> getFile(@PathVariable String filename) throws IOException { // 指定文件路径 String filePath = "/path/to/your/file/" + filename; // 创建文件资源对象 Resource resource = new FileSystemResource(filePath); // 检查文件是否存在 if (!resource.exists()) { return ResponseEntity.notFound().build(); } // 获取文件输入 InputStream inputStream = resource.getInputStream(); // 返回文件 return ResponseEntity.ok() .contentLength(Files.size(Paths.get(filePath))) .header("Content-Disposition", "attachment; filename=\"" + filename + "\"") .body(new InputStreamResource(inputStream)); } } ``` 在上述代码中,你需要将`/path/to/your/file/`替换为实际存储文件的路径。当访问`/file/{filename}`时,该方法会检查文件是否存在,然后将文件内容返回为文件。 请注意,上述示例假设文件路径是绝对路径。如果你需要处理相对路径或基于类路径的文件,请使用适当的方法获取资源。 希望这对你有所帮助!如果你有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值