url 汉字 乱码

1.url中有汉字数据需要传到后台存入database,在js中

 

encodeURIComponent(URIstring)

 

传到后台是乱码。

 

解决方法:

在tomcat 的server.xml 中

在下配置中添加红色部分即可。

 

<Connector acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" enableLookups="false" maxHttpHeaderSize="8192" maxSpareThreads="75" maxThreads="150" minSpareThreads="25" port="8089" redirectPort="8443" URIEncoding="UTF-8"/>

 

2. 下载文件有乱码

从页面通过js拼字符串,然后通过request 把数据 push 到servlet 端,在servlet 端转化成文件流再 push 到客户端以txt 的形式。

 

	public void pushData(HttpServletResponse response, String content) throws Exception{ 
		ServletOutputStream out = null; 
		InputStream is = null; 
		try{ 
			String downloadDisplayName =DOWNLOAD_DISPLAY_NAME; 
			is = new ByteArrayInputStream(content.getBytes("UTF-8")); 
			response.setContentType("application/octet-stream");
			response.setHeader("Content-disposition","attachment; filename=" + downloadDisplayName);
			response.setCharacterEncoding("UTF-8");
			// Get the output stream of the response
			out = response.getOutputStream(); 
			int c; 
			while ((c = is.read()) != -1){ 
			out.write(c); 
			} 
			out.flush(); 
		}catch(IOException ex){ 
			logger.error(ex.getMessage(), ex); 
			ex.printStackTrace(); 
		}finally{ 
			if (is != null){ 
				is.close(); 
			} 
			if (out != null){ 
				out.flush(); 
				out.close(); 
			} 
		} 
	} 

 如果是content.getBytes())就会有乱码

你可能感兴趣的:(tomcat,C++,c,servlet,C#)