1

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

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

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

4

3 回答 3

1

在您的 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())
    {

在html页面中:

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


}}

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

于 2013-04-23T11:09:01.803 回答
1

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

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);  

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

<c:forEach var="img" items="${imageUrlList}">  
  <img >  
</c:forEach>
于 2013-04-23T13:02:16.453 回答
1
File f = new File("/uploadFolder/");

File[] list = f.listFiles();

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

你只需要像这样循环遍历它 -

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

有关文件的更多信息

于 2013-04-23T10:58:47.330 回答