java 如何显示特定文件夹中jsp页面中的所有图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16167429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:05:33  来源:igfitidea点击:

how to show all image in jsp page from particular folder?

javajspliferay

提问by Kapil

I have one folder it contain more then one images, i want to show all image in my jsp page. I try following code in my jsp page.

我有一个文件夹,它包含多个图像,我想在我的 jsp 页面中显示所有图像。我尝试在我的 jsp 页面中使用以下代码。

<img src="<%=request.getContextPath() %>/uploadFolder/poll1.jpg"
      width="114" height="110" style="float: left;">
<h1>
   Images
</h1>

I have more then 10 images in uploadFolder i want to display all images in jsp page kindly help me how to do this?

我在uploadFolder中有超过10张图片我想在jsp页面中显示所有图片请帮助我如何做到这一点?

采纳答案by Mohammad Adil

File f = new File("/uploadFolder/");

File[] list = f.listFiles();

You will get list of all files in your upload folder -

您将获得上传文件夹中所有文件的列表 -

You just need to loop through it like this -

你只需要像这样循环它 -

for(int i = 0 ; i < list.length ; i++){
  File jpg = list[i]; 
  // use this file object to create img tag's in your jsp
}

More info about File

有关文件的更多信息

回答by Mohammad Adil

use following code on your jsp page :

在您的 jsp 页面上使用以下代码:

File folder = new File("d:\Reports"); //your path
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++)
{
    if (listOfFiles[i].isFile())
    {

in html page:

在 html 页面中:

<a href="servlet&filename=<%=listOfFiles[i].getName()%>">Download</a> 


}}

i have shown using <a>tag you can show it in table or in <img>tag.

我已经使用<a>标签显示,您可以在表格或<img>标签中显示它。

回答by Vignesh Vino

Build the list of filenames to display.

构建要显示的文件名列表。

List imageUrlList = new ArrayList();  
File imageDir = new File("/myapp/images");  
for(File imageFile : imageDir.listFiles()){  
  String imageFileName = imageFile.getName();  

  // add this images name to the list we are building up  
  imageUrlList.add(imageFileName);  

}  
request.setAttribute("imageUrlList", imageUrlList);  

then on the jsp display an <img>tag for each file.

然后在jsp上<img>为每个文件显示一个标签。

<c:forEach var="img" items="${imageUrlList}">  
  <img >  
</c:forEach>