JAVA中文件上传下载知识点整理

先来看看文件上传的部分,首先我先介绍下要实现文件上传,对于页面的要求,实际上也就是表单要多加一些属性。

  1. <h2>查看文件上传的请求正文</h2> 
  2. <form id="form1" method="POST" enctype="multipart/form-data" action="SeeRequestContentServlet"> 
  3.     <input type="text" name="myText" value="test" /><br/> 
  4.     <input type="file" name="myFile"/><br/> 
  5.     <input type="submit" value="提交" /><br/> 
  6. </form> 
<h2>查看文件上传的请求正文</h2>
<form id="form1" method="POST" enctype="multipart/form-data" action="SeeRequestContentServlet">
	<input type="text" name="myText" value="test" /><br/>
	<input type="file" name="myFile"/><br/>
	<input type="submit" value="提交" /><br/>
</form>

首先,因为是文件上传,那么表单的method必须指定为POST,然后必须告诉服务器,这是一次带有附件的请求,因此必须加上enctype="multipart/form-data"的表单属性以及对应的值。对于文件控件,相应的就是修改input标签的type属性为file就可以了。SeeRequestContentServlet这是一个用于处理我这次文件上传的Servlet文件,详细的东西大家可以查看附件。

想要知道待会要介绍的fileupload这个jar包,大家必须先知道一个带有附件的请求的请求正文到底是什么样子的,这样有利于大家的理解。

所以接下来演示,不使用jar包,来分析带附件的请求的请求正文。请看SeeRequestContentServlet的doPost方法。

Java代码
  1. public void doPost(HttpServletRequest request, HttpServletResponse response) 
  2.             throws ServletException, IOException { 
  3.  
  4.     System.out.println("本次请求正文长度为:" + request.getHeader("Content-Length")); 
  5.  
  6.     System.out.println("本次请求类型及表单域分隔符:" + request.getContentType()); 
  7.  
  8.     ServletInputStream sis = request.getInputStream(); 
  9.  
  10.     ServletOutputStream sos = response.getOutputStream(); 
  11.  
  12.     byte[] b = new byte[2048]; 
  13.  
  14.     @SuppressWarnings("unused"
  15.     int count = 0
  16.          
  17.     while ((count = sis.read(b)) != -1) { 
  18.         sos.write(b, 0, count); 
  19.     }    
  20.      
  21.     sos.flush(); 
  22.     sos.close(); 
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	System.out.println("本次请求正文长度为:" + request.getHeader("Content-Length"));

	System.out.println("本次请求类型及表单域分隔符:" + request.getContentType());

	ServletInputStream sis = request.getInputStream();

	ServletOutputStream sos = response.getOutputStream();

	byte[] b = new byte[2048];

	@SuppressWarnings("unused")
	int count = 0;
		
	while ((count = sis.read(b)) != -1) {
		sos.write(b, 0, count);
	}	
	
	sos.flush();
	sos.close();
}

代码实际上很简单,就是一些基础的流操作,我们来看一下运行结果,并做一个分析。

下面是我即将上传的文件:


我的表单数据:


请看上传后在网页上看到的结果:

Java代码
  1. -----------------------------80233217916595 
  2. Content-Disposition: form-data; name="myText" 
  3.  
  4. test 
  5. -----------------------------80233217916595 
  6. Content-Disposition: form-data; name="myFile"; filename="测试文件.txt" 
  7. Content-Type: text/plain 
  8.  
  9. �����ļ����� 
  10. -----------------------------80233217916595-- 
-----------------------------80233217916595
Content-Disposition: form-data; name="myText"

test
-----------------------------80233217916595
Content-Disposition: form-data; name="myFile"; filename="测试文件.txt"
Content-Type: text/plain

�����ļ�����
-----------------------------80233217916595--

以及我的控制台打印结果:

Java代码
  1. 本次请求正文长度为:310 
  2. 本次请求类型及表单域分隔符:multipart/form-data; boundary=---------------------------80233217916595 
本次请求正文长度为:310
本次请求类型及表单域分隔符:multipart/form-data; boundary=---------------------------80233217916595

待会在处理乱码问题,这个不是关键。

通过控制台打印结果,以及网页的输出结果,我们发现了,对于含有附件的请求正文来说,表单域之前会有一个---------------------------80233217916595作为分隔符,并且对于附件域来说,还会有额外的 filename="测试文件.txt"的文本来标示文件在上传时使用的文件名。

于是大家如果能够知道请求正文的格式组成之后,剩下来要做的不就是根据分隔符以及相应的一些固定的字符串如filename,name,通过字符串操作来获取请求正文中大家需要的数据了。感觉很繁琐吧,这也是为什么大家现在完成文件上传功能是都是使用Apache提供的fileupload的jar包来实现的原因了。不过个人感觉理解了原理,再去应用,对于学习是很有好处的,所以写了这个例子。

接下来,解释下出现乱码的原因,很简单,就是编码不统一引起的,由于我编程时习惯上都把编码设置为UTF-8,包括上传表单的页面编码,而text文档在中文系统中的默认编码是GBK(关于这个大家可以用Editplus求证),所以在打印响应内容的时候,由于编码的不兼容引起了乱码,解决方式很简单,用Editplus打开文件,重新编码即可。

原始编码:


修改后编码:


网页结果:

Java代码 复制代码 收藏代码
  1. -----------------------------20169232712042 
  2. Content-Disposition: form-data; name="myText" 
  3.  
  4. test 
  5. -----------------------------20169232712042 
  6. Content-Disposition: form-data; name="myFile"; filename="测试文件.txt" 
  7. Content-Type: text/plain 
  8.  
  9. 测试文件内容 
  10. -----------------------------20169232712042-- 
-----------------------------20169232712042
Content-Disposition: form-data; name="myText"

test
-----------------------------20169232712042
Content-Disposition: form-data; name="myFile"; filename="测试文件.txt"
Content-Type: text/plain

测试文件内容
-----------------------------20169232712042--

另外我们也发现了分隔符是随机生成的。以上例子并没有真正完成文件上传,只是想从请求正文的内容来分析文件上传原理,实际上大家自己有耐心地去做一些字符串截取就可以实现文件上传了,这里就省略了。进入fileupload的使用部分。

先看页面代码,跟之前的实际上没有太大区别,只是改了个action的地址。

Html代码 复制代码 收藏代码
  1. <h2>使用fileupload组件实现文件上传</h2> 
  2. <form id="form2" method="POST" enctype="multipart/form-data" action="FileUploadServlet"> 
  3.     <input type="text" name="myText" value="test" /><br/> 
  4.     <input type="file" name="myFile"/><br/> 
  5.     <input type="submit" value="提交" /><br/> 
  6. </form> 
<h2>使用fileupload组件实现文件上传</h2>
<form id="form2" method="POST" enctype="multipart/form-data" action="FileUploadServlet">
	<input type="text" name="myText" value="test" /><br/>
	<input type="file" name="myFile"/><br/>
	<input type="submit" value="提交" /><br/>
</form>

来看FileUploadServlet的doPost方法

Java代码 复制代码 收藏代码
  1. @SuppressWarnings("unchecked"
  2. public void doPost(HttpServletRequest request, HttpServletResponse response) 
  3.             throws ServletException, IOException { 
  4.  
  5.     try
  6.         FileItemFactory factory = new DiskFileItemFactory(); 
  7.         ServletFileUpload upload = new ServletFileUpload(factory); 
  8.         upload.setHeaderEncoding("utf8");//支持中文文件名 
  9.         List<FileItem> items = upload.parseRequest(request); 
  10.              
  11.         Iterator<FileItem> iter = items.iterator(); 
  12.         while (iter.hasNext()) { 
  13.             FileItem item = iter.next(); 
  14.             if (item.isFormField()) { 
  15.                 System.out.println("查找到一个普通文本数据"); 
  16.                  System.out.println("该文本数据的name为:"+item.getFieldName()); 
  17.                 System.out.println("该文本数据的value为:"+item.getString()); 
  18.                 System.out.println(); 
  19.              } else
  20.                  System.out.println("查找到一个二进制数据"); 
  21.                    System.out.println("该文件表单name为:"+item.getFieldName()); 
  22.                  System.out.println("该文件文件名为:"+item.getName()); 
  23.                  System.out.println("该文件文件类型为:"+item.getContentType()); 
  24.                  System.out.println("该文件文件大小为:"+item.getSize()); 
  25.                  System.out.println(); 
  26.                  File uploadedFile = new File(this.getServletContext().getRealPath("fileupload")+"\\"+item.getName()); 
  27.                  item.write(uploadedFile); 
  28.             } 
  29.         } 
  30.     } catch (FileUploadException e) { 
  31.         e.printStackTrace(); 
  32.     } catch (Exception e) { 
  33.         e.printStackTrace(); 
  34.     } 
  35.          
  36.     response.sendRedirect("success.jsp"); 
  37.  
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	try {
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding("utf8");//支持中文文件名
		List<FileItem> items = upload.parseRequest(request);
			
		Iterator<FileItem> iter = items.iterator();
		while (iter.hasNext()) {
			FileItem item = iter.next();
			if (item.isFormField()) {
			    System.out.println("查找到一个普通文本数据");
			     System.out.println("该文本数据的name为:"+item.getFieldName());
			    System.out.println("该文本数据的value为:"+item.getString());
			    System.out.println();
			 } else {
			     System.out.println("查找到一个二进制数据");
			       System.out.println("该文件表单name为:"+item.getFieldName());
			     System.out.println("该文件文件名为:"+item.getName());
			     System.out.println("该文件文件类型为:"+item.getContentType());
			     System.out.println("该文件文件大小为:"+item.getSize());
			     System.out.println();
			     File uploadedFile = new File(this.getServletContext().getRealPath("fileupload")+"\\"+item.getName());
			     item.write(uploadedFile);
			}
		}
	} catch (FileUploadException e) {
		e.printStackTrace();
	} catch (Exception e) {
		e.printStackTrace();
	}
		
	response.sendRedirect("success.jsp");

}

唯一要注意的就是那个可以支持中文文件名的代码,其他的跟Apache官方提供的代码就一致了。

有了上面的代码相信要实现多文件上传也是很容易的事情了。

最后来个带上传进度的文件上传。这里需要AJAX的知识,不懂的朋友可以先去了解下。

先看页面的代码

Html代码 复制代码 收藏代码
  1. <h2>文件上传显示进度</h2> 
  2. <iframe id='target_upload' name='target_upload' src='' style='display: none'></iframe> 
  3. <form id="form3" method="POST" enctype="multipart/form-data" action="AJAXFileUploadServlet" target="target_upload"> 
  4.     <input type="file" name="myFile"/><br/> 
  5.     <input type="button" value="提交" id="myButton"/><br/> 
  6. </form> 
  7. <div id="show"></div> 
<h2>文件上传显示进度</h2>
<iframe id='target_upload' name='target_upload' src='' style='display: none'></iframe>
<form id="form3" method="POST" enctype="multipart/form-data" action="AJAXFileUploadServlet" target="target_upload">
	<input type="file" name="myFile"/><br/>
	<input type="button" value="提交" id="myButton"/><br/>
</form>
<div id="show"></div>

细心的朋友发现多一个iframe和div,iframe的作用是,当我们要显示上传进度的时候,一定是要求页面不能刷新的,因此你会发现form的target属性等于iframe的name属性,也就是说,当form提交数据的时候,当前页面不会刷新,而是iframe刷新了,而iframe又是dispaly:none的,所以看起来页面好像是没有发生跳转。div的作用则是用于显示上传进度。

接下来关注服务器的代码部分,想想其实就可以发现,服务器需要两个Servlet,一个正在用于文件上传,而一个则是用来返回AJAX关于上传进度的数据返回。

这里要介绍fileupload中很好用的一个接口,ProgressListener,她可以用来监听文件的上传进度,使用方法很简单,即在文件上传的代码中加入

Java代码 复制代码 收藏代码
  1. FileItemFactory factory = new DiskFileItemFactory(); 
  2. ServletFileUpload upload = new ServletFileUpload(factory); 
  3. upload.setHeaderEncoding("utf8");// 支持中文文件名 
  4.              
  5. MyProgressListener myProgressListener = new MyProgressListener(request); 
  6. upload.setProgressListener(myProgressListener);//进行文件上传进度监听 
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf8");// 支持中文文件名
			
MyProgressListener myProgressListener = new MyProgressListener(request);
upload.setProgressListener(myProgressListener);//进行文件上传进度监听

MyProgressListener是实现ProgressListener的一个自定义类。下面是ProgressListener的代码。

Java代码 复制代码 收藏代码
  1. package com.mison.fileupload; 
  2.  
  3. import javax.servlet.http.HttpServletRequest; 
  4. import javax.servlet.http.HttpSession; 
  5.  
  6. import org.apache.commons.fileupload.ProgressListener; 
  7.  
  8. public class MyProgressListener implements ProgressListener { 
  9.  
  10.     private HttpSession session; 
  11.  
  12.     private FileUploadStatus fileUploadStatus; 
  13.  
  14.     public MyProgressListener(HttpServletRequest request) { 
  15.         session = request.getSession(); 
  16.     } 
  17.  
  18.     /***************************************************************************
  19.      * pBytesRead - The total number of bytes, which have been read so far.
  20.      * pContentLength - The total number of bytes, which are being read. May be
  21.      * -1, if this number is unknown.
  22.      * pItems - The number of the field, which is
  23.      * currently being read. (0 = no item so far, 1 = first item is being read,
  24.      * ...)
  25.      */ 
  26.     @Override 
  27.     public void update(long pBytesRead, long pContentLength, int pItems) { 
  28.         System.out.println("监听器被调用"); 
  29.         fileUploadStatus = (FileUploadStatus) (session.getAttribute("fileUploadStatus") == null ? new FileUploadStatus() 
  30.                 : session.getAttribute("fileUploadStatus")); 
  31.          
  32.         fileUploadStatus.setPBytesRead(pBytesRead); 
  33.         fileUploadStatus.setPContentLength(pContentLength); 
  34.         fileUploadStatus.setPItems(pItems); 
  35.          
  36.         session.setAttribute("fileUploadStatus", fileUploadStatus); 
  37.     } 
  38.  
package com.mison.fileupload;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.ProgressListener;

public class MyProgressListener implements ProgressListener {

	private HttpSession session;

	private FileUploadStatus fileUploadStatus;

	public MyProgressListener(HttpServletRequest request) {
		session = request.getSession();
	}

	/***************************************************************************
	 * pBytesRead - The total number of bytes, which have been read so far.
	 * pContentLength - The total number of bytes, which are being read. May be
	 * -1, if this number is unknown. 
	 * pItems - The number of the field, which is
	 * currently being read. (0 = no item so far, 1 = first item is being read,
	 * ...)
	 */
	@Override
	public void update(long pBytesRead, long pContentLength, int pItems) {
		System.out.println("监听器被调用");
		fileUploadStatus = (FileUploadStatus) (session.getAttribute("fileUploadStatus") == null ? new FileUploadStatus()
				: session.getAttribute("fileUploadStatus"));
		
		fileUploadStatus.setPBytesRead(pBytesRead);
		fileUploadStatus.setPContentLength(pContentLength);
		fileUploadStatus.setPItems(pItems);
		
		session.setAttribute("fileUploadStatus", fileUploadStatus);
	}

}

这样就了解了吧。在update方法中,将上传进度的有关信息保存到session对象中,那么AJAX请求就可以访问session来获取相应的上传进度了。

来看AJAX的服务器端代码

Java代码 复制代码 收藏代码
  1. package com.mison.fileupload; 
  2.  
  3. import java.io.IOException; 
  4. import java.io.PrintWriter; 
  5.  
  6. import javax.servlet.ServletException; 
  7. import javax.servlet.http.HttpServlet; 
  8. import javax.servlet.http.HttpServletRequest; 
  9. import javax.servlet.http.HttpServletResponse; 
  10.  
  11. import net.sf.json.JSONObject; 
  12.  
  13. @SuppressWarnings("serial"
  14. public class SeeProgressServlet extends HttpServlet { 
  15.  
  16.     public void doPost(HttpServletRequest request, HttpServletResponse response) 
  17.             throws ServletException, IOException { 
  18.         this.doGet(request, response); 
  19.     } 
  20.  
  21.     @Override 
  22.     protected void doGet(HttpServletRequest request, 
  23.             HttpServletResponse response) throws ServletException, IOException { 
  24.          
  25.         FileUploadStatus fileUploadStatus = (FileUploadStatus) request 
  26.                 .getSession().getAttribute("fileUploadStatus"); 
  27.          
  28.         JSONObject jsonObject = JSONObject.fromObject(fileUploadStatus); 
  29.         PrintWriter out = response.getWriter(); 
  30.         out.println(jsonObject.toString()); 
  31.         out.flush(); 
  32.         out.close(); 
  33.          
  34.     } 
  35.  
package com.mison.fileupload;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

@SuppressWarnings("serial")
public class SeeProgressServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		FileUploadStatus fileUploadStatus = (FileUploadStatus) request
				.getSession().getAttribute("fileUploadStatus");
		
		JSONObject jsonObject = JSONObject.fromObject(fileUploadStatus);
		PrintWriter out = response.getWriter();
		out.println(jsonObject.toString());
		out.flush();
		out.close();
		
	}

}

上面使用了json的包,来实现对象到json字符串的转换,不懂的自己搜索下。

最后就是客户端的js代码了

Html代码 复制代码 收藏代码
  1. <script type="text/javascript" src="js/jquery126.js"></script> 
  2. <script type="text/javascript"> 
  3.     $(function(){ 
  4.         $("#myButton").click(function(){ 
  5.             $("#show").html(""); 
  6.             $(this).attr("disabled",true); 
  7.             $("#form3").submit(); 
  8.             setTimeout("showProgress()",500); 
  9.         }); 
  10.     }); 
  11.          
  12.     function showProgress(){ 
  13.         $.getJSON("SeeProgressServlet",function(json){ 
  14.             $("#show").html("上传进度:"+(json.PBytesRead/json.PContentLength)*100+"%"); 
  15.             if(json.PBytesRead == json.PContentLength){ 
  16.                 $("#show").html($("#show").html()+"上传结束~"); 
  17.                 $("#myButton").attr("disabled",false); 
  18.             }else{ 
  19.                 setTimeout("showProgress()",500); 
  20.             } 
  21.         }); 
  22.     } 
  23. </script> 
<script type="text/javascript" src="js/jquery126.js"></script>
<script type="text/javascript">
	$(function(){
		$("#myButton").click(function(){
			$("#show").html("");
			$(this).attr("disabled",true);
			$("#form3").submit();
			setTimeout("showProgress()",500);
		});
	});
		
	function showProgress(){
		$.getJSON("SeeProgressServlet",function(json){
			$("#show").html("上传进度:"+(json.PBytesRead/json.PContentLength)*100+"%");
			if(json.PBytesRead == json.PContentLength){
				$("#show").html($("#show").html()+"上传结束~");
				$("#myButton").attr("disabled",false);
			}else{
				setTimeout("showProgress()",500);
			}
		});
	}
</script>

本地上传的话,弄个100M的东西,貌似效果比较明显,截个图给大家看下。





不过网页很卡就是,求可以优化的做法。

*********************************************************************************

第二部分是文件下载,相比简单很多了,终于可以松口气。

网页代码

Html代码 复制代码 收藏代码
  1. <h2>文件下载</h2> 
  2. <form id="form4" method="POST" action="FileDownloadServlet"> 
  3. 请输入文件名:<input type="text" name="fileName"/><br/> 
  4. <input type="submit" value="提交" /><br/> 
  5. </form> 
<h2>文件下载</h2>
<form id="form4" method="POST" action="FileDownloadServlet">
请输入文件名:<input type="text" name="fileName"/><br/>
<input type="submit" value="提交" /><br/>
</form>

Servlet代码

Java代码 复制代码 收藏代码
  1. package com.mison.filedown; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileInputStream; 
  5. import java.io.IOException; 
  6.  
  7. import javax.servlet.ServletException; 
  8. import javax.servlet.ServletOutputStream; 
  9. import javax.servlet.http.HttpServlet; 
  10. import javax.servlet.http.HttpServletRequest; 
  11. import javax.servlet.http.HttpServletResponse; 
  12.  
  13. @SuppressWarnings("serial"
  14. public class FileDownloadServlet extends HttpServlet { 
  15.  
  16.     public void doGet(HttpServletRequest request, HttpServletResponse response) 
  17.             throws ServletException, IOException { 
  18.  
  19.         this.doPost(request, response); 
  20.     } 
  21.  
  22.     public void doPost(HttpServletRequest request, HttpServletResponse response) 
  23.             throws ServletException, IOException { 
  24.  
  25.         String fileName = new String(request.getParameter("fileName").getBytes( 
  26.                 "ISO-8859-1"), "UTF-8"); 
  27.  
  28.         FileInputStream fis = new FileInputStream(new File(getServletContext() 
  29.                 .getRealPath("WEB-INF/download"
  30.                 + "\\" + fileName)); 
  31.          
  32.         System.out.println("客户端类型:" + request.getHeader("User-Agent")); 
  33.  
  34.         if (request.getHeader("User-Agent").contains("Firefox")) { 
  35.             response.addHeader("content-disposition", "attachment;filename=" 
  36.                     + request.getParameter("fileName")); 
  37.         } else if (request.getHeader("User-Agent").contains("MSIE")) { 
  38.             response.addHeader("content-disposition", "attachment;filename=" 
  39.                     + java.net.URLEncoder.encode(fileName, "UTF-8")); 
  40.         } 
  41.  
  42.         //相应的逻辑操作 
  43.          
  44.         ServletOutputStream sos = response.getOutputStream(); 
  45.  
  46.         int count = 0
  47.  
  48.         byte[] bytes = new byte[1024]; 
  49.  
  50.         while ((count = fis.read(bytes)) != -1) { 
  51.             sos.write(bytes, 0, count); 
  52.         } 
  53.  
  54.         sos.flush(); 
  55.  
  56.         sos.close(); 
  57.  
  58.     } 
  59.  
package com.mison.filedown;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class FileDownloadServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String fileName = new String(request.getParameter("fileName").getBytes(
				"ISO-8859-1"), "UTF-8");

		FileInputStream fis = new FileInputStream(new File(getServletContext()
				.getRealPath("WEB-INF/download")
				+ "\\" + fileName));
		
		System.out.println("客户端类型:" + request.getHeader("User-Agent"));

		if (request.getHeader("User-Agent").contains("Firefox")) {
			response.addHeader("content-disposition", "attachment;filename="
					+ request.getParameter("fileName"));
		} else if (request.getHeader("User-Agent").contains("MSIE")) {
			response.addHeader("content-disposition", "attachment;filename="
					+ java.net.URLEncoder.encode(fileName, "UTF-8"));
		}

		//相应的逻辑操作
		
		ServletOutputStream sos = response.getOutputStream();

		int count = 0;

		byte[] bytes = new byte[1024];

		while ((count = fis.read(bytes)) != -1) {
			sos.write(bytes, 0, count);
		}

		sos.flush();

		sos.close();

	}

}

比较有意思的是关于下载时指定中文文件名的部分,由于ie和火狐的处理方式不一样,因此写了个判断分支。对于ie来讲,使用URLEncoder这个类将中文编码为16进制的asci码,这样ie客户端可以自己进行转码,可惜火狐没有这功能;火狐的处理方式是将中文的字符用ISO-8859-1编码,火狐会在客户端自己进行转码,同样的,这样的功能ie也没有,真蛋疼!

转载自:http://huangjinqiang.iteye.com/blog/786589

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值