81,122
社区成员




response.setHeader("Content-type","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
// 设置下载头信息
try {
TransmitFile.downLoad(response, filepath);
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
out.flush();
out.close();
[code=java]
/**
* 下载文件
* @param config
* @param response
* @param downLoadFileName
*/
public static void downLoad(HttpServletResponse response,String filePath){
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
String filename=filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition","filename="+new String(filename.getBytes("gb2312"),"iso8859-1"));
bis =new BufferedInputStream(new FileInputStream(filePath));
bos=new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesread;
while(-1 != (bytesread = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesread);
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (bis != null)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (bos != null)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
[/code]response.addHeader("Content-Disposition","inline;filename="+filename+".doc");%>
<%@ page contentType="application/vnd.ms-word; charset=UTF-8"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%String filename= new String(("xxxx").getBytes("GBK"),"ISO-8859-1");
response.addHeader("Content-Disposition","inline;filename="+filename+".xls");%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
即可