
i need a jsp code to download images from a folder in eclipse

1)viewFiles.jsp:
<%@ page import="java.io.*"%>
<html>
<table>
<tr><th>File Name</th><th>View File</th><th>Delete File</th>
<%
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><%=name%></td><td><a href="download.jsp?f=<%=path%>">View</a></td></tr>
<%
}
%>
</table>
</html>
2)download.jsp:
<%@page import="java.io.*,java.net.*"%>
<%!
public static String getMimeType(String fileUrl)
throws java.io.IOException, MalformedURLException
{
String type = null;
URL u = new URL(fileUrl);
URLConnection uc = null;
uc = u.openConnection();
type = uc.getContentType();
return type;
}
%>
<%
String file=request.getParameter("f");
File f = new File (file);
String filename=f.getName();
String type=getMimeType("file:"+file);
response.setContentType(type);
response.setHeader("Content-Disposition", "attachment; filename=\""+filename+"\"");
String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();
int bit = 256;
int i = 0;
try {
while((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
%>
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.