まず、そのパスに存在するすべてのファイル名を取得します
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 を取得します。フォルダから直接画像を表示することはできないため、サーブレットを作成する必要があります。
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]);
}
Web ページに画像を表示する
<%
for(int i=0; i< k.length ;i++){
out.println(" <img id='copyimg"+i+"' alt='img' style='width:304px;height:228px;' src="+url[i]+">");
}
%>
サーブレット コード
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;
}
}