response.setContentType("text/xml;charset=GB2312");//设置Type为XML
//读文件
private void readXmlFile(HttpServletResponse response, String fPath) throws FileNotFoundException, IOException {
File rXmlFile = new File(fPath);
FileInputStream inFile=new FileInputStream(rXmlFile);
int i = inFile.available();
ServletOutputStream out=response.getOutputStream();
byte[] data = new byte[i];
inFile.read(data); // 读数据本地XML流
inFile.close();
inFile = null;
out.write(data); // 输出数据到客户端
out.flush();
out.close();
out =null;
}
//输出Error XML
private void printErrMsg(HttpServletResponse response, String errMsg) {
try {
PrintWriter out = response.getWriter();
out.println("<?xml version=\"1.0\" encoding=\"GB2312\" ?>");
out.println("<err_info>");
out.println(errMsg);
out.println("</err_info>");
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//读取目录下的所有文件
File directory = new File(filePath);//存放中心上传的配置文件目录,文件名如:0050101_200910_0001.xml
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
String fName=files[i].getName();
fName=fName.replaceAll(xml, "");
if(fName.startsWith(jgbmYYYYMM)){
fName=fName.replaceAll(jgbmYYYYMM, "");
int fNum=Integer.parseInt(fName); //文件列表自增码
if(iCkdh<fNum){
fSize++;
sbf.append("<item>"+files[i].getName().replaceAll(xml, "")+"</item>");
fPath=files[i].getAbsolutePath();
}
}
}
Servlet设定下载文件名
response.setHeader("Content-disposition","inline; filename=centerStaffList.xls");
//以上这行设定传送到前端浏览器时的档名为FwmxExcel.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档