⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 downloadservlet.java

📁 “JSP数据库项目案例导航”一书从第一章到第十一章各章实例的源程序文件以及数据库文件。 注意: 1. 本书中的案例提供的数据库环境不同
💻 JAVA
字号:
	package oa.servlet;

	import javax.servlet.*;
	import javax.servlet.http.*;
	import java.io.*;
	import java.util.*;
	import oa.bean.*;
	import oa.main.*;
	import java.lang.*;
	import java.sql.*;


	public class DownloadServlet extends HttpServlet {
	  static final private String CONTENT_TYPE = "text/html; charset=GB2312";
	  //Initialize global variables

	  //Process the HTTP Get request
	  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 doPost(request,response);
	}
	  //Process the HTTP Post request
	  public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 //PrintWriter out = response.getWriter();
		InputStream in = null;
		OutputStream out = response.getOutputStream();
		iMsgServer2000 imsg=new iMsgServer2000();
		MailBean mb=null; 
		try {
			DealString ds=new DealString();
			mb=new MailBean(); 

			if( mb.getConn() == null ){
				PrintWriter out_writer = response.getWriter();
				response.setContentType("text/html; charset=GB2312");
				out_writer.println("<html>");
				out_writer.println(ds.toASCII("<title>数据库无响应,请后退并重试</title>"));
				out_writer.println(ds.toASCII("数据库无法响应,请&nbsp;<a href='javascript:window.history.back(-1);'>返回</a>&nbsp;重试"));
				out_writer.println("</html>");
				out_writer.close();
				return;
			}

			String yjfjid=ds.toString(request.getParameter("id"));
			String type=ds.toString(request.getParameter("type"));
			String filename= ds.toGBK(ds.toString(request.getParameter("filename")));
			String filepath = "";
			if(!yjfjid.equals(""))//email下载邮件附件************************************************************************(1)
			{				
				Vector vec=new Vector();
				MailElementBean meb;
				vec=mb.receiveFujian(yjfjid,"yjfjid");
				if (vec!=null)
				{
					meb=new MailElementBean();
					meb=(MailElementBean)vec.get(0);
					//get filename and pathname
					filename =meb.getFJMC();
					//String filename =new String(meb.getFJMC().getBytes("ISO8859_1"),"gb2312");

					String temp = meb.getFJWZ();  //virtual path(path in the database)
					//temp = oa.main.ParentBean.FilesPath+"email/";
					
					String filenewname=meb.getFJXMC() ;//new filename
					String day = filenewname.substring(6,8);
					filepath=temp+day+"\\"+filenewname;

					response.setContentType("application/x-download");
					response.setContentType("application/x-download; charset=ISO2022CN");
					response.setHeader("Content-disposition","attachment; filename="+mb.toUtf8String(filename));
				
					in = new BufferedInputStream(new FileInputStream(filepath));
					byte[] buf = new byte[2048];
					int bytesRead;
					while ( (bytesRead = in.read(buf)) != -1) {
						out.write(buf, 0, bytesRead);
					}
				}
			}
			else if(type.equals("1"))//temp_sure下载新闻中的附件**********************************************************-(2)
			{
				String temp = oa.main.ParentBean.FilesPath;
				filepath=temp+filename;
				String name = filename.substring(filename.lastIndexOf('/')+1,filename.length());

				response.setContentType("application/x-download");
				response.setContentType("application/x-download; charset=ISO2022CN");
				response.setHeader("Content-disposition","attachment; filename="+mb.toUtf8String(name));

				in = new BufferedInputStream(new FileInputStream(filepath));
				byte[] buf = new byte[2048];
				int bytesRead;
				while ( (bytesRead = in.read(buf)) != -1) {
					out.write(buf, 0, bytesRead);
				}
			}
			else if(type.equals("2"))//cabinet/departfiles下载部门文件柜****************************************************(3)
			{
				String txt_fileID=ds.toString(request.getParameter("txt_fileID"));
				String txt_departID=ds.toString(request.getParameter("txt_departID"));
				Vector vect = mb.getOnePage("select WJMC,WJLJ from ZZ_BMWJB where BMBH= '"+txt_departID+"' and WJBH="+txt_fileID,1,1);
				String name = "";
				if(vect.size()>1)
				{
					Hashtable hash = (Hashtable)vect.get(1);
					filepath = (String)hash.get("WJLJ");
					name = (String)hash.get("WJMC");
				}

				String temp = oa.main.ParentBean.FilesPath;
				filepath = temp + "cabinet/departfiles/" + filepath;

				response.setContentType("application/x-download");
				response.setContentType("application/x-download; charset=ISO2022CN");
				response.setHeader("Content-disposition","attachment; filename="+mb.toUtf8String(name));
				
				in = new BufferedInputStream(new FileInputStream(filepath));
				byte[] buf = new byte[2048];
				int bytesRead;
				while ( (bytesRead = in.read(buf)) != -1) {
					out.write(buf, 0, bytesRead);
				}
			//	String go = "oa/sharedfiles/departFileDownload.jsp?err=0";
			//	response.sendRedirect(go);
			}
			else if(type.equals("3"))//cabinet/personfiles下载个人文件柜**********************************************************(4)
			{
				String txt_fileID=ds.toString(request.getParameter("txt_fileID"));
				String txt_personno=ds.toString(request.getParameter("txt_personno"));
				Vector vect = mb.getOnePage("select WJMC,WJLJ from ZZ_GRWJB where ZGBH= '"+txt_personno+"' and WJBH="+txt_fileID,1,1);
				String name = "";
				if(vect.size()>1)
				{
					Hashtable hash = (Hashtable)vect.get(1);
					filepath = (String)hash.get("WJLJ");
					name = (String)hash.get("WJMC");
				}
				String temp = oa.main.ParentBean.FilesPath;
				filepath = temp + "cabinet/personfiles/" + filepath;

				response.setContentType("application/x-download");
				response.setContentType("application/x-download; charset=ISO2022CN");
				response.setHeader("Content-disposition","attachment; filename="+mb.toUtf8String(name));
				in = new BufferedInputStream(new FileInputStream(filepath));
				byte[] buf = new byte[2048];
				int bytesRead;
				while ( (bytesRead = in.read(buf)) != -1) {
					out.write(buf, 0, bytesRead);
				}
			//	String go = "oa/person/personFileDownload.jsp?err=0";
			//	response.sendRedirect(go);
			}
		}catch(Exception e){
			System.out.println(e);
		}finally {
			if(mb != null)
				mb.closeConn();
			if (in != null)
				in.close();
		}
	}
	//Clean up resources
	public void destroy() {
	}
}
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -