domino servlet 文件下载

request.getParameter("")所获的参数,在页面提交URL中文参数的时候,手动使用URLEncoder(UTF-8)进行了

 

编码。

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownLoadServlet extends HttpServlet {
 /**
  * 下载文档附件
  */
 private static final long serialVersionUID = 3375470032511144463L;

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  this.doPost(request, response);
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String filePath = null;
  if (request.getParameter("filePath") != null) {
   //System.out.println("URLDecode filePath="+URLDecoder.decode(request.getParameter("filePath"), "UTF-8"));
   filePath = new String(request.getParameter("filePath").getBytes("ISO-8859-1"), "utf-8");
   System.out.println("filePath="+filePath);
   
  }else{
   response.setContentType("text/html;charset=GBK");
   response.getWriter().print("<script>alert('请求参数错误!');javascript:history.go(-1);</script>");
   return;
  }
  String mainPath = "/xkjt/notesdata/domino/html/";
  //String mainPath="/local/notesdata/domino/";
  System.out.println("mainPath="+mainPath);
  String path=mainPath+filePath;
  File file =new File(path);
  System.out.println(file.getAbsolutePath());
  if(!file.exists()){
   System.out.println("文件不存在!");
   response.setContentType("text/html;charset=GBK");
   response.getWriter().print("<script>alert('文件不存在!');</script>");
   //response.getWriter().print("<script>window.opener.close();</script>");
   response.getWriter().print("<script>javascript:history.go(-1);</script>");
   return;
  }else{
   downloadFile(path, getFileName(path), response);
  }
 }


 private static void downloadFile(String filePath, String fileName,
   HttpServletResponse response) throws IOException {
  //设置响应头和保存文件名 
  response.setContentType("APPLICATION/OCTET-STREAM;charset=gbk");
//  response.setContentType("application/x-msdownload");
  response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
 
  BufferedInputStream bis=null;
  BufferedOutputStream bos=null;
  try{
  ServletOutputStream sos=response.getOutputStream();
  bis=new BufferedInputStream(new FileInputStream(filePath));
  bos=new BufferedOutputStream(sos);
  
  byte[] buffer=new byte[4096];
  int byteread;
  while(-1!=(byteread=bis.read(buffer,0,buffer.length))){
   bos.write(buffer, 0, byteread);
  }
  }catch(IOException e){
   e.printStackTrace();
  }finally{
   if(bis!=null){
    bis.close();
   }
   if(bos!=null){
    bos.close();
   }
   System.out.println("关闭流!");
  }
  System.out.println("文件下载完毕.");  
 }
 //截取文件名称
 private String getFileName(String filePath){
  int  index1=filePath.lastIndexOf("/");
  int  index2=filePath.lastIndexOf("\\");
  int index=(index1>index2)?index1:index2;
  return filePath.substring(index+1);
 }
}
--------------------- 
作者:woshiwxw765 
来源:CSDN 
原文:https://blog.csdn.net/woshiwxw765/article/details/6653476 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值