在jsp中显示图像

时间:2012-07-23 05:21:39

标签: html jsp

您好我在jsp中创建一个网页,我必须从一个路径显示图像到我存储图像的文件夹。但是当我运行文件时,我只看到路径名但不显示图像。以下代码可以帮助我。

代码:

<%@page import="java.io.*"%>
<html>
<body>

<table>
<%
File f = new File("C:/UploadedFiles");
File[] files = f.listFiles();
for(int i=0;i<files.length;i++){
String name=files[i].getName();
String path=files[i].getPath();

%>
<tr>
<td><img src="<%=path=name%>"/></a></td>
</tr>
<%
}
%>
</table>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

首先获取该路径中存在的所有文件名

public class ManipulateImage {    
public static String[]  display(){

             File folder = new File("C:/Folder Name");
             File[] listOfFiles = folder.listFiles();
             String [] k = new String[listOfFiles.length];
             Arrays.fill(k,"none");

                 for (int i = 0; i < listOfFiles.length; i++) {
                   if (listOfFiles[i].isFile()) {
                     System.out.println("File " + listOfFiles[i].getName());
                     k[i]="File " + listOfFiles[i].getName();
                   } else if (listOfFiles[i].isDirectory()) {
                     System.out.println("Directory " + listOfFiles[i].getName());
                   }

                 }
                 return k;
         }
}

然后在JSP中获取存在的文件数

<%
    String[] k=ManipulateImage.display();
    int l=k.length;
%>

获取显示图像的URL,您无法直接从文件夹中显示图像,因此您需要创建Servlet。

 for(int i=0;i<k.length;i++){
              j[i]=k[i].substring(5);
              link[i]="http://localhost:8080/ImageUploadWebApp/DisplayImageServlet/images/"+j[i];
        }
 URL[] url= new URL[k.length]; 
    for(int i=0;i<k.length;i++){
    url[i]= new URL(link[i]);
    }

在网页中显示图像

<%
    for(int i=0; i< k.length ;i++){
        out.println(" <img id='copyimg"+i+"' alt='img' style='width:304px;height:228px;' src="+url[i]+">");

     } 
%>

Servlet代码

public class DisplayImageServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
String image_name = "";
ResourceBundle props = null;
String filePath = "";
private static final int BUFSIZE = 100;
private ServletContext servletContext;
public DisplayImageServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("FROM SERVLET");
sendImage(getServletContext(), request, response);
}
public void sendImage(ServletContext servletContext,
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.servletContext = servletContext;
String reqUrl = request.getRequestURL().toString();
StringTokenizer tokens = new StringTokenizer(reqUrl, "/");
int noOfTokens = tokens.countTokens();
String tokensString[] = new String[noOfTokens];
int count = 0;
while (tokens.hasMoreElements()) {
tokensString[count++] = (String) tokens.nextToken();
}
String folderName = tokensString[noOfTokens - 2];
image_name = tokensString[noOfTokens - 1];
filePath = "/" + folderName + "/" + image_name;
String fullFilePath = "C:/Folder Name" + filePath;
System.out.println("FULL PATH :: "+fullFilePath);
doDownload(fullFilePath, request, response);
}
private void doShowImageOnPage(String fullFilePath,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
response.reset();
response.setHeader("Content-Disposition", "inline");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Expires", "0");
response.setContentType("image/tiff");
byte[] image = getImage(fullFilePath);
OutputStream outputStream = response.getOutputStream();
outputStream.write(image);
outputStream.close();
}
private void doDownload(String filePath, HttpServletRequest request,
HttpServletResponse response) throws IOException {
File fileName = new File(filePath);
int length = 0;
ServletOutputStream outputStream = response.getOutputStream();
ServletContext context = servletContext;
String mimetype = context.getMimeType(filePath);
response.setContentType((mimetype != null) ? mimetype
: "application/octet-stream");
response.setContentLength((int) fileName.length());
response.setHeader("Content-Disposition", "attachment; filename=\""
+ image_name + "\"");
byte[] bbuf = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(fileName));
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
outputStream.write(bbuf, 0, length);
}
in.close();
outputStream.flush();
outputStream.close();
}
private byte[] getImage(String filename) {
byte[] result = null;
String fileLocation = filename;
File f = new File(fileLocation);
result = new byte[(int)f.length()];
try {
FileInputStream in = new FileInputStream(fileLocation);
in.read(result);
}
catch(Exception ex) {
System.out.println("GET IMAGE PROBLEM :: "+ex);
ex.printStackTrace();
}
return result;
}
}

答案 1 :(得分:0)

您可以使用路径:

File f = new File("C:/UploadedFiles/abc.jpg");

而不是写

File f = new File("C:/UploadedFiles");

您正在使用的文件路径仅采用文件夹的路径而非图像的路径。

答案 2 :(得分:0)

尝试这样的事情

<td><img src="<%=path%>"/"<%=name%>"></td>

想法是在img标签中提供文件名和路径。如果出现问题,请原谅我,因为我不了解JSP