JAva webダウンロードファイルの文字化けし処理

983 ワード

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