熊咪

javaweb--下载文件列出

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

//列出网站所有下载文件
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);
}

}//递归方法,使用list获得递归结果

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">
<html>
<head>
<title>My JSP 'listfile.jsp' starting page</title>
</head>

<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}">下载</a> <br/>
</c:forEach>

</body>
</html>

 

 

//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-阿凡达.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", "您要下载的资源已被删除!!");
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);
}

}

posted on 2015-05-22 15:20  熊咪  阅读(232)  评论(0编辑  收藏  举报