javaweb--Download File list

Source: Internet
Author: User

Listfileservlet.java

Package cn.itcast.web.servlet;

Import Java.io.File;
Import java.io.IOException;
Import Java.util.HashMap;
Import Java.util.Map;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

List all download files for the site
public class Listfileservlet extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

String filepath = This.getservletcontext (). Getrealpath ("/web-inf/upload");
Map map = new HashMap ();
ListFile (New File (filepath), map);

Request.setattribute ("map", map);
Request.getrequestdispatcher ("/listfile.jsp"). Forward (request, response);
}

public void ListFile (File file,map Map) {

if (!file.isfile ()) {
File files[] = File.listfiles ();
for (File f:files) {
ListFile (F,MAP);
}
}else{
String realname = File.getname (). substring (File.getname (). IndexOf ("_") +1); 9349249849-88343-8344_ _. avi
Map.put (File.getname (), realname);
}

}//recursive method, using list to get recursive results

public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

Doget (request, response);
}

}

listfile.jsp

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<% @taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>my JSP ' listfile.jsp ' starting page</title>

<body>

<c:foreach var= "Me" items= "${map}" >
<c:url value= "/servlet/downloadservlet" var= "Downurl" >
<c:param name= "filename" value= "${me.key}" ></c:param>
</c:url>
${me.value} <a href= "${downurl}" > Downloads </a> <br/>
</c:forEach>

</body>

Downloadservlet.java

Package cn.itcast.web.servlet;

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.OutputStream;
Import Java.net.URLEncoder;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.commons.fileupload.FileItem;

public class Downloadservlet extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {



String filename = request.getparameter ("filename"); 23239283-92489-Avatar. avi
filename = new String (filename.getbytes ("iso8859-1"), "UTF-8");
String path = Makepath (Filename,this.getservletcontext (). Getrealpath ("/web-inf/upload"));

File File = new file (path + "\ \" + filename);
if (!file.exists ()) {
Request.setattribute ("message", "The resource you want to download has been deleted!! ");
Request.getrequestdispatcher ("/message.jsp"). Forward (request, response);
Return
}

String realname = filename.substring (Filename.indexof ("_") +1);
Response.setheader ("Content-disposition", "attachment;filename=" + urlencoder.encode (realname, "UTF-8"));

FileInputStream in = new FileInputStream (path + "\ \" + filename);
OutputStream out = Response.getoutputstream ();
byte buffer[] = new byte[1024];
int len = 0;
while ((Len=in.read (buffer)) >0) {
Out.write (buffer, 0, Len);
}
In.close ();
Out.close ();
}


public string Makepath (string filename,string savepath) {

int hashcode = Filename.hashcode ();
int dir1 = hashcode&0xf; 0--15
int dir2 = (hashcode&0xf0) >>4; 0-15

String dir = savepath + "\ \" + dir1 + "\ \" + Dir2; Upload\2\3 upload\3\5
File File = new file (dir);
if (!file.exists ()) {
File.mkdirs ();
}
return dir;
}


public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

Doget (request, response);
}

}

javaweb--Download File list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.