首先獲得所有出現在這條道路
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;
}
}
哪來的文件名?你只是給圖像路徑,即使你沒有啓動一個錨標記之前標籤 – 2012-07-23 05:23:17
我認爲你必須輸入完整路徑... – 2012-07-23 05:26:56
你能告訴我怎麼做? @約翰 – 2012-07-23 05:29:52