25,980
社区成员
发帖
与我相关
我的任务
分享
public static void writeDataStream(HttpServletResponse response,String dataStr)throws Exception{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
OutputStream os = null;
try {
dos.writeUTF(dataStr);
byte data [] = baos.toByteArray();
response.setCharacterEncoding("UTF-8");
response.setContentLength(data.length);
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/octet-stream");
os = response.getOutputStream();
os.write(data);
os.flush();
dos.flush();
baos.flush();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (os != null) {
os.close();
}
if (dos != null) {
dos.close();
}
if (baos != null) {
baos.close();
}
}
}
