【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“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();
//取得要下载的文件名
String name=ds.toString(request.getParameter("newname"));
String filename=ds.toGBK(request.getParameter("newfile"));
String filenewname=name;
//取得路径
String temp = oa.main.ParentBean.FilesPath+"news\\appendix\\";
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() {}
}