TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
利用Servlet直接下载服务器上有密码保护的文件(这些文件应该放在WEB-INF目录下),在windows xp+tomcat 5.5+jdk1.5 上测试通过。原作来自:
http://blog.chinajavaworld.com/entry.jspa?id=303,站长修改整理(支持中文下载)。 一、servlet源码: package quanwen;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;
/**
* 下载用的servlet,把它放置到你的WEB APPLICATION目录下
* 在WEB.XML中声明一下即可
* author:qixiaorui
*/
public class DownLoadServlet extends HttpServlet {
//Initialize global variables public void init(ServletConfig config) throws ServletException {
super.init(config);
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String password=request.getParameter("password");
String filename=new String(request.getParameter("filename").getBytes("ISO8859_1"),"GB2312");
String path=getServletContext().getRealPath("/WEB-INF/file");
if((!checkPassword(password))||(password==null))
{
response.setContentType("text/HTML;charset=gb2312");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>下载信息</title></head>");
out.println("<body>");
out.println("<h1>你输入的注册码不正确</h1><hr>");
out.println("</body></html>");
out.close();
} else {
long totalSize=0;
File f=new File(path+"/"+filename);
long filelength=f.length();
byte[] b=new byte[1024];
FileInputStream fin=new FileInputStream(f);
DataInputStream in=new DataInputStream(fin);
response.setContentType("application/x-unknown;charset=GB2312");
response.setHeader("Content-Disposition", "attachment;filename=" +URLEncoder.encode(filename,"UTF8"));
String filesize=Long.toString(filelength);
response.setHeader("Content-Length",filesize);
ServletOutputStream servletOut=response.getOutputStream();
while(totalSize<filelength) {
totalSize+=1024;
if(totalSize>filelength) {
byte[] leftpart=new byte[1024-(int)(totalSize-filelength)];
in.readFully(leftpart);
servletOut.write(leftpart);
}else {
in.readFully(b);
servletOut.write(b);
}
}
servletOut.close();
}
}
//模拟验证注册信息
private boolean checkPassword(String s) {
String[] passwords={"aaa","bbb","ccc","ddd","eee","fff","ggg","hhh","lll","mmm"};
boolean flag=false;
for(int i=0;i<passwords.length;i++)
{
if (s.equals(passwords)){
flag=true;
break;
}
}
return flag;
}
//Get Servlet information
public String getServletInfo() {
return "DownLoadServlet.DownLoadServlet Information";
}
public void destroy() {
super.destroy();
}
} 二、相关首页:download.html的主要代码: <form method="POST" action="http://127.0.0.1:8080/Test/download">
<table border="0" cellpadding="10" cellspacing="10" style="border-collapse: collapse" bordercolor="#111111"
width="100%" id="AutoNumber3">
<tr>
<td width="100%" bgcolor="#00FFFF"> <p align="center"><font face="华文仿宋" color="#000080">
<b> --------------------------------- 下 载 文 件 ----------------------</b></font>
</td>
</tr>
<tr>
<td width="100%">
<table border="0" cellpadding="3" cellspacing="3" style="border-collapse: collapse" bordercolor="#111111"
width="100%" id="AutoNumber4">
<tr>
<td width="27%">请选择文件:</td>
<td width="73%">
<select name="filename">
<option>1.zip</option>
<option>2.zip</option>
<option>中文.zip</option>
</select>
</td>
</tr>
<tr>
<td width="27%">请输入操作密码:</td>
<td width="73%"> <input type="password" name="password" size="52"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="100%">
<input type="submit" value="提交" name="uploadfile">
<input type="reset" value="重置" name="B2"></td>
</tr>
</table>
</form>
三、部署描述文件中添加如下内容(web.xml):
<servlet>
<servlet-name>DownLoadServlet</servlet-name>
<servlet-class>quanwen.DownLoadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownLoadServlet</servlet-name>
<url-pattern>/download/*</url-pattern>
</servlet-mapping>
源码下载:http://file.javaxxz.com/2014/10/1/202803250.zip |
|