servlet返回application/json数据类型在ie浏览器中提示下载文件问题

概述

    在servlet中设置response.setContentType(“application/json”)返回json格式数据类型时在ie浏览器中提示下载文件。这种现象是ie浏览不支持application/json数据格式引起的,只要在servlet中再填上一句response.setHeader("Content-Type", "text/html")就能解决问题(应为浏览器对数据格式的识别靠的是响应头里面的Content-Type,所以,此种情况下页面在处理返回数据的时候,要先利用js把文本格式转化为json格式:eval()函数可以实现此功能)。

(注意:有文章说,ie浏览器重ie10开始就开始支持application/json了,但是我亲测过后并不是这个样子,不说ie10,就连ie11也不支持,也有可能是我的浏览器问题)

servlet返回application/json数据类型在ie浏览器中提示下载文件问题_第1张图片


    乍一看response.setContentType(XX)和response.setHeader("Content-Type", XX)不是一样的概念吗,其实并不是这样子的,前者设置返回的数据的类型,和浏览器没多大关系(其实也有关系,在设置前者而不设置后者的情况下,后者的值默认为前者,但是两者确实是有本质的区别的),后者设置的是相应头参数,响应头中的Content-Type决定了浏览器以什么方式来处理相应内容。

response.setContentType(“application/json”):表示响应数据类型是json

servlet返回application/json数据类型在ie浏览器中提示下载文件问题_第2张图片


response.setHeader("Content-Type",“application/json”):表示响应头参数值

servlet返回application/json数据类型在ie浏览器中提示下载文件问题_第3张图片


代码检验

服务器端只设置response.setContentType,出现浏览器接收到服务器端返回的数据类型和响应头Content-Type值都为response.setContentType设置的值:


代码:

package com.sends.test;

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

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

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public TestServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		//response.setHeader("Content-Type", "text/html");
		
		PrintWriter writer = response.getWriter();
		writer.write("{\"msg\":\"success\"}");
		writer.flush();
		writer.close();
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}


浏览器效果:

servlet返回application/json数据类型在ie浏览器中提示下载文件问题_第4张图片    


服务器端同时设置response.setContentType("XXX")和response.setHeader("Content-Type","XXX"),浏览器显示返回数据类型为response.setContentType("XXX")设置值,响应头值为response.setHeader("Content-Type","XXX")设置值:


代码:

package com.sends.test;

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

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

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public TestServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-Type", "text/html");
		
		PrintWriter writer = response.getWriter();
		writer.write("{\"msg\":\"success\"}");
		writer.flush();
		writer.close();
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

浏览器效果:

               servlet返回application/json数据类型在ie浏览器中提示下载文件问题_第5张图片


ie浏览器就要设置为此种情况程序才能正常工作:

servlet返回application/json数据类型在ie浏览器中提示下载文件问题_第6张图片

为了进一步验证,来一个绝一点的例子,响应内容类型为text/html,响应头Content-Type参数值设置为application/json。结果显示,虽然响应内容为text/html格式,ie浏览器仍旧提示下载文件。

代码:

package com.sends.test;

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

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

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public TestServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-Type", "application/json");
		
		PrintWriter writer = response.getWriter();
		writer.write("{\"msg\":\"success\"}");
		writer.flush();
		writer.close();
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

ie效果:

servlet返回application/json数据类型在ie浏览器中提示下载文件问题_第7张图片    



更正:

通过同时反映,了解到以上描述是错误的,setHeader("Content-Type", "XXX")和setContentType("XXX")描述的是一样的概念(https://github.com/apache/tomcat80/blob/trunk/java/org/apache/coyote/Response.java#L32),如果要ie浏览器里面返回的数据不提示下载,只需要setContentType("text/html")就行了,然后在处理返回信息的时候先利用js把数据转化为想要的对象再进行处理。


你可能感兴趣的:(servlet返回application/json数据类型在ie浏览器中提示下载文件问题)