java web 下载文件乱码处理

java web 下载文件乱码处理

String name  = new StringBuffer().append("班次信息").append(begindate).append("至").append(enddate).append(".xlsx").toString();
        String userAgent = req.getHeader("User-Agent");  
        // name.getBytes("UTF-8")处理safari的乱码问题  
        byte[] bytes = userAgent.contains("MSIE") ? name.getBytes() : name.getBytes("UTF-8"); 
        // 各浏览器基本都支持ISO编码  
        name = new String(bytes, "ISO-8859-1"); 
        
		resp.setCharacterEncoding("UTF-8");
		resp.addHeader("Content-type"," application/octet-stream");
		resp.addHeader("Content-Disposition",new StringBuffer().append("attachment;filename=").append(name).toString());
		ServletOutputStream out = resp.getOutputStream();
		
		try {
			book.write(out);
		} finally {
			out.close();
		}

 

你可能感兴趣的:(java,java,Web)