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);
}
}