【问题标题】:Store and Display Image in Spring Boot在 Spring Boot 中存储和显示图像
【发布时间】:2021-05-10 15:11:58
【问题描述】:

我正在尝试保存徽标(在项目中而不是数据库中)并获取它并将其显示在 JSP 页面上为此我将图像的 URL 保存在数据库中但是当我获取它时我没有得到这里的图片是我的代码和项目结构的屏幕截图,请在这里帮助我,如果这是正确的文件夹,请告诉我,或者我应该将图片保存在其他地方谢谢。

文件保存方法

public static String storeLogoPath(MultipartFile file) {
        logger.info("File AAYA HAI " + file);
        String path = "";
        try {
            //String paths = "G:/MainWorkSpace/Picture_testing/WebContent/resources/images/"+file.getOriginalFilename();
            path = "G:/MainWorkSpace/TestProjects/ecomProject/src/main/resources/static/img/storeLogo/"+file.getOriginalFilename();
            File newFile = new File(path);
            newFile.createNewFile();
            FileOutputStream myfile = new FileOutputStream(newFile);
            myfile.write(file.getBytes());
            myfile.close();
            logger.info("File should be saved now :: ");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return path;
    }

控制器方法

@RequestMapping(value = "/storeFormshow" ,method = RequestMethod.GET)
    public String addStoreForm(Model theModel) {
        Stores storeObj = new Stores();
        theModel.addAttribute("storeForm",storeObj);
        return "Store/storeForm";
    }

JSP 页面列表

<%@include file="/WEB-INF/views/Store/StoreTemplet/Header.jsp"%>


<div class="content-wrapper">
                <table class="table" id="table_id">
                    <thead>
                        <tr>
                            <th>Logo</th>
                            <th>ID</th>
                            <th>Store Name</th>
                            <th>Country</th>
                            <th class="text-center">Store Status</th>

                        </tr>
                    </thead>
                    <tbody>

                        <c:forEach var="store" items="${storeList}">
                            <tr>
                            <td> <img src="${store.logoURL}" width="10" height="10"> </td>
                                <td>${store.id}</td>
                                <td><a>${store.storeName}</a></td>
                                <td>${store.country}</td>
                                <td>${store.storeStatus}</td>
                            </tr>
                        </c:forEach>
                    </tbody>
                </table>
</div>

<%@include file="/WEB-INF/views/Store/StoreTemplet/Footer.jsp"%>

项目结构

为图片返回的 URL G:/MainWorkSpace/TestProjects/ecomProject/src/main/resources/static/img/storeLogo/Screenshot_2018-07-11-21-46-48.jpg"

应用程序属性

spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp


spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB

【问题讨论】:

    标签: spring image spring-boot jsp project-structure


    【解决方案1】:

    将您的图片添加到资源/静态文件夹,然后在 img 标签中访问它们,如下所示

    <img src="/images/solr.png" height="50" width="50">
    

    【讨论】:

    • 我使用文件夹 storelogo 的原因是我必须存储很多图像实际上我想要的功能是用户可以添加徽标和个人资料图片都应该存储在不同的文件夹中,所以名称不应重复并造成混淆当我使用 Spring MVC 结构时,我曾经将图像保存在 Web 应用程序下,并且图像很容易访问,但是在 Spring Boot 中,图像不容易访问,并且路径由数据库返回所以我不能在这里简单地传递 Image 的具体路径。
    【解决方案2】:
    public static String storeLogoPath(MultipartFile file) {
        logger.info("File AAYA HAI " + file);
        String path = "";
        try {
            path = "G:/MainWorkSpace/TestProjects/ecomProject/src/main/resources/static/img/storeLogo/"+file.getOriginalFilename();
            File newFile = new File(path);
            newFile.createNewFile();
            FileOutputStream myfile = new FileOutputStream(newFile);
            myfile.write(file.getBytes());
            path ="/img/storeLogo/"+file.getOriginalFilename();
            myfile.close();
            logger.info("File should be saved now :: ");
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("Path Returned is ::: "  + path);
        return path;
    }
    

    我已经从静态文件夹中修剪了路径并将其保存在数据库中

    这不是正确的做法,但它对我有用

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 2019-01-02
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      相关资源
      最近更新 更多