📄 downloadservlet.java
字号:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
*
* @author Liyan
*/
public class DownloadServlet extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request,response);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
//用于下载的头:
response.setContentType ("application/octet-stream;");
Person p=new Person();
p.setName (CommonMethods.GetUserName(request));
String pass = CommonMethods.GetUserPass (request);
if( p.getName ()==null || (!p.checkPassword(pass)) )
{
return;
}
String sDownload =request.getParameter("download");
sDownload = CommonMethods.DealWithGBCodeOfRequest(sDownload);
String sPath=CommonMethods.GetUserFolderdir()+p.getName();
//输出流:
ServletOutputStream sos= response.getOutputStream ();
//文件流:
FileInputStream fis = new FileInputStream(sPath+sDownload);
int nLength=1024;
byte[] a = new byte[nLength];
int nSize=fis.read(a,0,nLength);
while(nSize!=-1){
for(int i=0;i<nSize;i++)
sos.write (a[i]);
nSize=fis.read(a,0,nLength);
}
fis.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -