(读取某一个文件夹下的所有的文件,存到集合里面List,再存到request作用域范围中)ListFileServelt—(将所有的文件列表显示)Listfiles.jsp-----DownloaServlet.java
private String id;
private String savename; //上传文件的名称,文件的uuid名
private String realName; //上传文件的真实名称
private String savepath; //记住文件的位置
private Date uptime; //文件的上传时间
private String description; //文件的描述
private String username; //上传人
ListFileServlet
package com.hbsi.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
public class ListFileServlet extendsHttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
Stringsavepath = this.getServletContext().getRealPath(
"/WEB-INF/upload");
Mapmap = new HashMap();
listFiles(newFile(savepath), map);
request.setAttribute("map",map);
request.getRequestDispatcher("/listfile.jsp")
.forward(request,response);
}
privatevoid listFiles(File file, Map map) {
if(file.isFile()) {
Stringuuidname = file.getName(); // uuid_a_1_3_3.txt
Stringrealname = uuidname.substring(uuidname.indexOf("_") + 1);
map.put(uuidname,realname);
}else {
File[]files = file.listFiles();
for(File f : files) {
listFiles(f,map);
}
}
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
doGet(request,response);
}
}
DownloadServlet
package com.hbsi.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
public class DownloadServlet extendsHttpServlet {
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
Stringfilename = request.getParameter("filename");
filename= new String(filename.getBytes("iso8859-1"), "utf-8");
System.out.println(filename);
Stringsavepath = this.getFileSavePath(this.getRealName(filename));
Filef = new File(savepath + "\\" + filename);
if(!f.exists()) {
request.setAttribute("message","下载的资源不存在");
request.getRequestDispatcher("/message.jsp").forward(request,response);
}
response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(this.getRealName(filename),"UTF-8"));
FileInputStreamin = new FileInputStream(f);
byte[]buf = new byte[1024];
intlen = 0;
OutputStreamout = response.getOutputStream();
while((len = in.read(buf)) > 0) {
out.write(buf,0, len);
}
in.close();
}
publicString getFileSavePath(String filename) {
intdir1 = filename.hashCode() & 0xf;
intdir2 = (filename.hashCode() >> 4) & 0xf;
Stringsavepath = this.getServletContext().getRealPath("/WEB-INF/upload")+"\\" + dir1 + "\\" + dir2;
returnsavepath;
}
publicString getRealName(String filename) {
StringrealName = filename.substring(filename.indexOf("_") + 1);
returnrealName;
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
doGet(request,response);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有