Spring Boot Thymeleaf显示来自mysql的图像;

时间:2020-07-14 03:02:19

标签: java mysql spring spring-boot thymeleaf

我要疯了,因为我花了一整天,但仍然无法解决这个问题。在我看来,这是正确的路径,我也使用了绝对路径来生成类似file://之类的图像,但是它也不起作用。我想要的是在数组列表对象中显示图像。 在第一张图片中,即使它是控制台中的正确路径,也没有显示图片,第二张图片是th:src =“ $ {list.storedFilePath}”,我将其用作图片标签。我不熟悉thymeleaf,我在google上搜索并查找了所有资源,但仍然无法解决。(ps我尝试了th:src =“'/'+ $ {list.storedFilePath}”,但它也不起作用。 )

这是我的查看页面

 <div class="row cat-pd" onclick="location.href='productDetail.do'">
                     <div class="col-md-6" style="width:280px;" th:each="list:${list}">
                        <div class="small-box-c">
                           <div class="small-img-b">
                              <img th:src="'http://localhost:8090/'+${list.storedFilePath}" class="img-responsive"  alt="Image"/>
                           </div>
                           <div class="dit-t clearfix">
                              <div class="left-ti">
                                 <h4 th:text="${list.pName}"></h4>
                                 <p>By <span>Beko</span> under <span>Lights</span></p>
                              </div>
                              <a th:text="${list.price}+'원(하루)'"> <br>
                                     </a>
                           </div>
                        </div>
                     </div>
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer{

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setDefaultEncoding("UTF-8");
        commonsMultipartResolver.setMaxUploadSizePerFile(5 * 1024 * 1024);
        return commonsMultipartResolver;
    }
@Component
public class FileUtils {
    
    public List<FileDto> parseFileInfo(int pNo, MultipartHttpServletRequest multipartHttpServletRequest) throws Exception{
        
        if(ObjectUtils.isEmpty(multipartHttpServletRequest)) {
            return null;
        }
        List<FileDto> fileList = new ArrayList<>();
        
        DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyyMMdd"); 
        ZonedDateTime current = ZonedDateTime.now();
        String path = "images/"+current.format(format);
        File file1 = new File(path);
        if(file1.exists() == false){
            file1.mkdirs();
        }
        
        Iterator<String> iterator = multipartHttpServletRequest.getFileNames();
        
        String newFileName, originalFileExtension, contentType;
        
        while(iterator.hasNext()){
            List<MultipartFile> list = multipartHttpServletRequest.getFiles(iterator.next());
            for (MultipartFile multipartFile : list){
                if(multipartFile.isEmpty() == false){
                    contentType = multipartFile.getContentType();
                    if(ObjectUtils.isEmpty(contentType)){
                        break;
                    }
                    else{
                        if(contentType.contains("image/jpeg")) {
                            originalFileExtension = ".jpg";
                        }
                        else if(contentType.contains("image/png")) {
                            originalFileExtension = ".png";
                        }
                        else if(contentType.contains("image/gif")) {
                            originalFileExtension = ".gif";
                        }
                        else{
                            break;
                        }
                    }
                    
                    newFileName = Long.toString(System.nanoTime()) + originalFileExtension;
                    FileDto file = new FileDto();
                    file.setpNo(pNo);
                    file.setFileSize(multipartFile.getSize());
                    file.setOrigin(multipartFile.getOriginalFilename());
                    file.setStoredFilePath(path + "/" + newFileName);
                    fileList.add(file);
                    
                    file1 = new File(path + "/" + newFileName);
                    multipartFile.transferTo(file1);
                }
            }
        }
        return fileList;
    
    }
}

控制器

@RequestMapping("/viewProductList.pr")
public ModelAndView selectProductList(@RequestParam int cNo) throws Exception{
    
    ModelAndView mv = new ModelAndView("/productCategory");
    
    
    List<ProductDto> list = pService.selectProductList(cNo);
    mv.addObject("list", list);
    
    return mv;
}
```[![enter image description here][1]][1]


  [1]: https://i.stack.imgur.com/8lSXk.png

0 个答案:

没有答案