downloadservlet.java

来自「JAVA邮件系统」· Java 代码 · 共 56 行

JAVA
56
字号
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 + =
减小字号Ctrl + -
显示快捷键?