[英]Show local disk images in the JSP (images out of web context)
我做了servlet,以使圖像路徑可在.JSP頁面中訪問
public void nova() {
System.out.println("Nova Certidão");
certidoes = new File("C:\\imagens\\").listFiles();
List<String> paths= new ArrayList<String>();
for (File arquivo : certidoes) {
paths.add(arquivo.getPath());
}
result.include("files", caminhos);
}
現在在頁面中,我可以訪問arraylist文件中磁盤中的文件路徑
在jsp中,我有這個來顯示圖像:
<c:forEach items="${files}" var="files">
${file} -
<img src="/getImage/${file}" height="42" width="42">
<br>
</c:forEach>
而我的getImage是這樣的:
@Get("/getImage/{path}")
public void getImage(String path) {
//here will come all the way to send the stream of the image if i receive the path
System.out.println(path);
}
但它不起作用,因為它創建了一個奇怪的請求圖像路徑:
它創建:
/getImage/C:/imagens/image1.jpg
所以它不起作用,如果我手動鍵入:/ getImage / image1,我正確地顯示了系統,我認為它不適用於文件路徑,因為/(我認為它很明顯),但是我不知道如何解決這個問題
您需要將圖像添加到Web應用程序根目錄的子目錄中。
我假設您的應用程序根目錄為{web_apps} / yourapp。 在此目錄下,您將擁有一些子目錄,例如WEB-INF,樣式,圖像。
將所有圖像放入/ yourapp / images /(此文件夾可從公共位置訪問)。
在JSP中
<img src="${pageContext.request.contextPath}/images/${file}" height="42" width="42" />
更新
由於圖像位於Web應用程序根目錄下的固定文件夾中,因此我們無法使用上述解決方案。
這是正確的解決方案:
使用DES / AES加密將圖像的路徑(絕對路徑/相對路徑)轉換為VALID URL變量-輸出可以是Base16,Base64編碼。
將public16,Base64編碼路徑轉換回public void getImage函數中的圖像路徑
@Get("/getImage/{path}") public void getImage(String path) { // path is in based16/Base64 encoding String pathOfImage = functionToConvertEncodedPathToRealPath ( path) }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.