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

因此,我目前正在开发一个 Spring Web 应用程序,它允许您从表单上传图像(有效扩展名是:.png、.jpeg、.jpg 和 .gif)。然后将图像保存在磁盘上,并将其相对路径存储在数据库中。我已使用此代码成功将图像保存到磁盘:


@Override

public void saveThumbnail(MultipartFile thumbnailFile) throws Exception {

    String folder = "/bundles/";

    byte[] bytes = thumbnailFile.getBytes();

    Path path = Paths.get(folder + thumbnailFile.getOriginalFilename());

    Files.write(path, bytes);

}

表格看起来像这样

http://img2.mukewang.com/642687bb0001756f06570244.jpg

这是显示此表单的代码


                <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 开发的新手,所以即使我查阅了一些文章和教程,我也不知道如何实现它。


隔江千里
浏览 93回答 3
3回答

MMMHUHU

您的 src 不应是服务器中文件的位置,源应该是将为您的资源提供服务的 http 链接。您可以将 apache 配置为将 URL 映射到特定目录中的资源,然后在 src 属性中提及映射的 URL + 文件名或者您可以创建一个控制器,它从特定位置获取资源并将其作为字节流返回,并在 src 属性中设置指向您的控制器的链接+文件名

紫衣仙女

我终于设法显示图像。如果有其他人试图做同样的事情,我想列出我采取的步骤。仅供参考,我使用的是 Tomcat 和 MySQL 数据库。确保您的图像目录存在于您的系统上并向其中添加图像。创建一个名为的新类FileServlet并将以下代码应用到其中。@WebServlet("/images/*")public class FileServlet extends HttpServlet {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void doGet(HttpServletRequest request, HttpServletResponse response)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws IOException&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; String filename = URLDecoder.decode(request.getPathInfo().substring(1), "UTF-8");&nbsp; &nbsp; &nbsp; &nbsp; File file = new File("C:\\your\\local\\image\\directory", filename);&nbsp; &nbsp; &nbsp; &nbsp; response.setHeader("Content-Type", getServletContext().getMimeType(filename));&nbsp; &nbsp; &nbsp; &nbsp; response.setHeader("Content-Length", String.valueOf(file.length()));&nbsp; &nbsp; &nbsp; &nbsp; response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");&nbsp; &nbsp; &nbsp; &nbsp; Files.copy(file.toPath(), response.getOutputStream());&nbsp; &nbsp; }}现在转到您的主类并应用一个名为@ServletComponentScan. 你的主类现在应该是这样的:@SpringBootApplication@ServletComponentScanpublic class WebApplication extends SpringBootServletInitializer {&nbsp; &nbsp; @Override&nbsp; &nbsp; protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {&nbsp; &nbsp; &nbsp; &nbsp; return application.sources(WebApplication.class);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; SpringApplication.run(WebApplication.class, args);&nbsp; &nbsp; }}在您的 .jsp 文件中,将此行添加到您想要显示图像的任何位置:<img src="http://localhost:8080/images${YOUR_PASSED_OBJECT.RELATIVE_IMG_PATH_VARIABLE}">重建您的 Web 应用程序并转到localhost:PORT/images/YOUR_FILE_NAME.EXTENSION如果您有任何问题,请随时对此答案发表评论,我会根据需要进行更新。

慕标琳琳

假设您有 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”
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java