为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用Spring显示本地图片(保存在C盘)?

如何使用Spring显示本地图片(保存在C盘)?

隔江千里 2023-03-31 15:11:23
因此,我目前正在开发一个 Spring Web 应用程序,它允许您从表单上传图像(有效扩展名是:.png、.jpeg、.jpg 和 .gif)。然后将图像保存在磁盘上,并将其相对路径存储在数据库中。我已使用此代码成功将图像保存到磁盘:@Overridepublic void saveThumbnail(MultipartFile thumbnailFile) throws Exception {    String folder = "/bundles/";    byte[] bytes = thumbnailFile.getBytes();    Path path = Paths.get(folder + thumbnailFile.getOriginalFilename());    Files.write(path, bytes);}表格看起来像这样这是显示此表单的代码                <form:form method="POST" modelAttribute="eventForm" class="form-signin" enctype="multipart/form-data">                <spring:bind path="name">                    <span>Name</span><br>                    <div class="form-group ${status.error ? 'has-error' : ''}">                        <form:input type="text" path="name" class="form-control" placeholder="Name"                                    autofocus="true"></form:input>                        <form:errors path="name"></form:errors>                    </div>                </spring:bind>                <spring:bind path="price">                    <span>Price</span><br>                    <div class="form-group ${status.error ? 'has-error' : ''}">                        <form:input type="number" path="price" class="form-control"></form:input>                        <form:errors path="price"></form:errors>                    </div>                </spring:bind>我已经尝试过这样显示它:<img src="file:C:${eventForm.thumbnailUrl}">但是,我的浏览器似乎阻止了此操作以防止出现任何安全问题。我做了一些研究,发现您可以让 Apache 从它可以访问的目录中提供文件。由于我是 Web 开发的新手,所以即使我查阅了一些文章和教程,我也不知道如何实现它。
查看完整描述

3 回答

?
MMMHUHU

TA贡献1834条经验 获得超8个赞

您的 src 不应是服务器中文件的位置,源应该是将为您的资源提供服务的 http 链接。

您可以将 apache 配置为将 URL 映射到特定目录中的资源,然后在 src 属性中提及映射的 URL + 文件名

或者您可以创建一个控制器,它从特定位置获取资源并将其作为字节流返回,并在 src 属性中设置指向您的控制器的链接+文件名


查看完整回答
反对 回复 2023-03-31
?
紫衣仙女

TA贡献1839条经验 获得超15个赞

我终于设法显示图像。如果有其他人试图做同样的事情,我想列出我采取的步骤。仅供参考,我使用的是 Tomcat 和 MySQL 数据库。

  • 确保您的图像目录存在于您的系统上并向其中添加图像。

  • 创建一个名为的新类FileServlet并将以下代码应用到其中。

@WebServlet("/images/*")

public class FileServlet extends HttpServlet {

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws IOException

    {

        String filename = URLDecoder.decode(request.getPathInfo().substring(1), "UTF-8");

        File file = new File("C:\\your\\local\\image\\directory", filename);

        response.setHeader("Content-Type", getServletContext().getMimeType(filename));

        response.setHeader("Content-Length", String.valueOf(file.length()));

        response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

        Files.copy(file.toPath(), response.getOutputStream());

    }

}

现在转到您的主类并应用一个名为@ServletComponentScan. 你的主类现在应该是这样的:

@SpringBootApplication

@ServletComponentScan

public class WebApplication extends SpringBootServletInitializer {


    @Override

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

        return application.sources(WebApplication.class);

    }


    public static void main(String[] args) throws Exception {

        SpringApplication.run(WebApplication.class, args);

    }


}

在您的 .jsp 文件中,将此行添加到您想要显示图像的任何位置:

<img src="http://localhost:8080/images${YOUR_PASSED_OBJECT.RELATIVE_IMG_PATH_VARIABLE}">

重建您的 Web 应用程序并转到localhost:PORT/images/YOUR_FILE_NAME.EXTENSION

如果您有任何问题,请随时对此答案发表评论,我会根据需要进行更新。


查看完整回答
反对 回复 2023-03-31
?
慕标琳琳

TA贡献1830条经验 获得超9个赞

假设您有 Apache tomcat 服务器。您将必须更新 server.xml 文件。添加

<Context  docBase="C:\your\physical\path" path="/path/for/http/url" />

里面的<Host></Host>标签。这样做,您可以访问保存在其中的任何文件

“C:\你的\物理\路径”

在 URL 的帮助下从您的 Web 应用程序:

“ http://yourdomain/ path/for/http/url /somefile.someextension”


查看完整回答
反对 回复 2023-03-31
  • 3 回答
  • 0 关注
  • 88 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信