HttpServletResponse是什么意思
.bobrow.framework.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class WrapperResponse extends HttpServletResponseWrapper {
private MyPrintWriter tmpWriter;
private ByteArrayOutputStream output;
public WrapperResponse(HttpServletResponse httpServletResponse) {
super(httpServletResponse);
output = new ByteArrayOutputStream();
tmpWriter = new MyPrintWriter(output);
}
public void finalize() throws Throwable {
super.finalize();
output.close();
tmpWriter.close();
}
public String getContent() {
try {
tmpWriter.flush(); //å·æ°è¯¥æµçç¼å²ï¼è¯¦çjava.io.Writer.flush()
String s = tmpWriter.getByteArrayOutputStream().toString("UTF-8");
//æ¤å¤å¯æ ¹æ®éè¦è¿è¡å¯¹è¾åºæµä»¥åWriterçéç½®æä½
//æ¯å¦tmpWriter.getByteArrayOutputStream().reset()
return s;
} catch (UnsupportedEncodingException e) {
return "UnsupportedEncoding";
}
}
//è¦çgetWriter()æ¹æ³ï¼ä½¿ç¨æ们èªå·±å®ä¹çWriter
public PrintWriter getWriter() throws IOException {
return tmpWriter;
}
public void close() throws IOException {
tmpWriter.close();
}
//èªå®ä¹PrintWriterï¼ä¸ºçæ¯æresponseæµåå°èªå·±æå®çè¾å ¥æµå½ä¸
//èéé»è®¤çServletOutputStream
private static class MyPrintWriter extends PrintWriter {
ByteArrayOutputStream myOutput; //æ¤å³ä¸ºåæ¾responseè¾å ¥æµç对象
public MyPrintWriter(ByteArrayOutputStream output) {
super(output);
myOutput = output;
}
public ByteArrayOutputStream getByteArrayOutputStream() {
return myOutput;
}
}
}
2024-11-20 02:10
2024-11-20 01:41
2024-11-20 01:33
2024-11-20 01:24
2024-11-20 00:36