---
title: "flex交互编码，解决乱码问题"
author: "softctwo"
date: "2011-08-24T09:54:04Z"
tags: []
categories: ["Web前端"]
slug: "flex"
draft: false
lastmod: "2011-08-24T09:54:04Z"
id: 1156545
url: "https://softctwo.iteye.com/blog/1156545"
---

1.Flex端传到Java端  
   
Flex端:encodeURIComponent(comment.text)  
使用encodeURIComponent把参数转换为 application/x-www-form-urlencoded 格式  
   
Java端:URLDecoder.decode(comment, "utf-8")  
对 x-www-form-urlencoded 字符串解码  
   
2.Java端传到Flex端  
   
HttpServletResponse response = ServletActionContext.getResponse();  
PrintWriter out = response.getWriter();  
response.setContentType("text/html;charset=utf-8");

out.println(doc.asXML());  
out.flush();  
out.close();

→

HttpServletResponse response = ServletActionContext.getResponse();  
ServletOutputStream out = response.getOutputStream();  
response.setContentType("text/html;charset=utf-8");

out.write(doc.asXML().getBytes("UTF-8"));  
out.flush();  
out.close();