[java]
package comhbsiservlet;
import javaioFile;
import javaioFileInputStream;
import javaioIOException;
import javaioOutputStream;
import URLEncoder;
import javaxservletServletException;
import javaxservlethttpHttpServlet;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {
String filename = requestgetParameter(filename);
filename = new String(filenamegetBytes(iso)UTF);
Systemoutprintln(filename);
//找到该文件所在的文件夹位置
//String savepath = thisgetFileSavePath(filename); //现在的文件名是UUID
String savepath = thisgetFileSavePath(thisgetRealName(filename));
File f = new File(savepath+\\+filename);
if(!fexists()){
requestsetAttribute(message下载的资源不存在);
requestgetRequestDispatcher(/messagejsp)forward(request response);
}
responsesetHeader(contentdispositionattachment;filename=+URLEncoderencode(thisgetRealName(filename)UTF));
FileInputStream in = new FileInputStream(f);
byte[] buf = new byte[];
int len=;
OutputStream out = responsegetOutputStream();
while((len=inread(buf))>){
outwrite(buflen);
}
inclose();
}
public String getFileSavePath(String filename){
int dir = filenamehashCode()&xf;
int dir = (filenamehashCode()>>)&xf;
String savepath = thisgetServletContext()getRealPath(/WEBINF/upload)+\\+dir+\\+dir;
return savepath;
}
public String getRealName(String filename){
String realName = filenamesubstring(filenameindexOf(_)+);
return realName;
}
public void doPost(HttpServletRequest request HttpServletResponse response)
throws ServletException IOException {
doGet(request response);
}
}