1.上传方法
package com.*;
import *;
public class BaseController {
protected Logger logger = LoggerFactory.getLogger(getClass());
/**
* 输出JSON数据
*
* @param response
* @param jsonStr
*/
public void writeJson(HttpServletResponse response, String jsonStr) {
response.setContentType("text/json;charset=utf-8");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
PrintWriter pw = null;
try {
pw = response.getWriter();
pw.write(jsonStr);
pw.flush();
} catch (Exception e) {
logger.info("输出JSON数据异常", e);
}finally{
if(pw!=null){
pw.close();
}
}
}
/**
*
* 向页面响应json字符数组串流.
*
* @param response
* @param jsonStr
* @throws IOException
* @return void
*/
public void writeJsonStr(HttpServletResponse response, String jsonStr) throws IOException {
OutputStream outStream = null;
try {
response.reset();
response.setCharacterEncoding("UTF-8");
outStream = response.getOutputStream();
outStream.write(jsonStr.getBytes("UTF-8"));
outStream.flush();
} catch (IOException e) {
logger.info("输出JSON数据异常(writeJsonStr)", e);
} finally {
if(outStream!=null){
outStream.close();
}
}
}
public void writeJsonStr(HttpServletResponse response, InputStream in) throws IOException {
if(null == in ){
return ;
}
OutputStream outStream = null;