📄 articledownloadservlet.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.*;
public class articleDownloadServlet 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
{
InputStream in = null;
OutputStream out = response.getOutputStream();
MailBean mb = null;
try {
mb=new MailBean();
DealString ds=new DealString();
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("数据库无法响应,请 <a href='javascript:window.history.back(-1);'>返回</a> 重试"));
out_writer.println("</html>");
return;
}
//取得要下载的文件名
String name=ds.toString(request.getParameter("newname"));
String filename=ds.toGBK(request.getParameter("newfile"));
String filenewname=name;
String cominfo=ds.toString(request.getParameter("cominfo"));//公司新闻下载
//取得路径
String temp = oa.main.ParentBean.FilesPath+"news\\appendix\\";
if(cominfo.equals("cominfo"))
temp = oa.main.ParentBean.FilesPath+"cominfo\\";
if(cominfo.equals("meet"))
temp = oa.main.ParentBean.FilesPath+"meet\\";
if(cominfo.equals("office"))
temp = oa.main.ParentBean.FilesPath+"office\\";
String saveDirectory = temp;
//设定头信息
response.setContentType("application/x-download");
response.setContentType("text/html; charset=ISO2022CN");
response.setHeader("Content-disposition","attachment; filename="+mb.toUtf8String(filename));
String filepath=saveDirectory+filenewname;
//向客户端写入字节流
in = new BufferedInputStream(new FileInputStream(filepath));
byte[] buf = new byte[2048];
int bytesRead;
while ( (bytesRead = in.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}
}catch(Exception e){
System.out.print(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 + -