2012-07-23 95 views
0

您好我正在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> 
+0

哪來的文件名?你只是給圖像路徑,即使你沒有啓動一個錨標記之前標籤 – 2012-07-23 05:23:17

+0

我認爲你必須輸入完整路徑... – 2012-07-23 05:26:56

+0

你能告訴我怎麼做? @約翰 – 2012-07-23 05:29:52

回答

0

您可以使用路徑,如:

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

相反書面方式

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

您只使用文件路徑的需要的文件夾的路徑而不是圖像。

+0

如果我想使用多個圖像怎麼辦... – 2012-07-23 05:30:33

+0

做一個for循環並相應地輸出 html標籤? – Jasonw 2012-07-23 07:22:01

0

嘗試這樣的事情

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

想法是給在img標籤的文件名與路徑一起。請原諒我,如果事情是錯誤的,因爲我不知道JSP

+0

=路徑變量包含路徑以及文件名。 – 2012-07-23 05:36:08

+0

nopes你的代碼不能正常工作。 – 2012-07-23 05:39:29

+0

「...因爲我不知道JSP」 - 如何不回答JSP問題>。<? – Christoph 2012-07-23 07:25:10

2

首先獲得所有出現在這條道路

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