亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用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 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號