IE下上传文件返回application/json时提示下载的解决方案

application/json 仅在IE上传文件时才提示下载,其它情况下是可以正常解析的。这个不清楚其中原因。

解决方案:

1、ContentType 改为text/plain;charset=UTF-8 或者 text/html;chareset=UTF-8

2、直接response.getWriter().print(msg);不加contentType类型

java中的方案:

/**
	 * 输出信息到浏览器
	 * @param msg
	 * @throws IOException
	 */
	public void print(Object msg) throws IOException {
		if(!UTF_8.equalsIgnoreCase(response.getCharacterEncoding()))
			response.setCharacterEncoding(UTF_8);
		response.getWriter().print(msg);
	}

	public void output_json(String[] key, Object[] value) throws IOException {
		StringBuilder json = new StringBuilder("{");
		for(int i=0;i<key.length;i++){
			if(i>0)
				json.append(',');
			boolean isNum = value[i] instanceof Number ;
			json.append("\"");
			json.append(key[i]);
			json.append("\":");
			if(!isNum) json.append("\"");
			json.append(value[i]);
			if(!isNum) json.append("\"");
		}
		json.append("}");
		print(json.toString());
	}

	public void output_json(String key, Object value) throws IOException {
		output_json(new String[]{key}, new Object[]{value});
	}
调用方法:

ctx.output_json(
		            new String[]{"url","saveUrl","ext","success"}, 
		            new Object[]{ ctx.contextPath()+PORTRAIT_PATH+saveUrl,saveUrl,ext,true}
		        );



或者:

/**
	 * 发送文本。使用UTF-8编码。
	 * 
	 * @param response
	 *            HttpServletResponse
	 * @param text
	 *            发送的字符串
	 */
	public static void renderText(HttpServletResponse response, String text) {
		render(response, "text/plain;charset=UTF-8", text);
	}



	/**
	 * 发送内容。使用UTF-8编码。
	 * 
	 * @param response
	 * @param contentType
	 * @param text
	 */
	public static void render(HttpServletResponse response, String contentType,
			String text) {
		response.setContentType(contentType);
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		try {
			response.getWriter().write(text);
			response.getWriter().flush();
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
	}



public static void successText(HttpServletResponse response,
			String message,Map<String, ? extends Object> extendMap) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put(STATUS, SUCCESS);
		jsonMap.put(SUCCESS, true);
		jsonMap.put(MESSAGE, message);
		jsonMap.putAll(extendMap);
		renderText(response, JSONObject.toJSONString(jsonMap));
	}



调用方法:
Map<String, Object> extendMap=new HashMap<String, Object>(); 
 	    extendMap.put("url", ctx.contextPath()+PORTRAIT_PATH+saveUrl);
 	    extendMap.put("saveUrl", saveUrl);
 	    extendMap.put("ext", ext);
    	ResponseUtils.successText(ctx.response(),"添加成功",extendMap);



草木全
分享到:
共 0 条  此列表为空  当前1/1页

© 2014 究问社区 copyRight 豫ICP备13003319号-1