📄 multipart.java
字号:
/** * Web server PUSH implementation. * (adapted from pushlets by Just Objects B.V) * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */import javax.servlet.*;import javax.servlet.http.*;import java.io.*;/** * Handle a multipart HTTP response using multipart MIME. */public class multipart{ private String boundary = "End."; private static final String CRLF = "\015\012"; private static final String multipartMimeType = "multipart/x-mixed-replace"; public ServletOutputStream out = null; /** * MultiPartResponse constructor. */ public multipart(HttpServletRequest req, HttpServletResponse rsp) throws IOException { if (!agentCanHandleMultipart(req)) { throw new IOException("MultiPart MIME not supported by browser (MSIE?)"); } // Make boundary unique boundary += Long.toString(System.currentTimeMillis(), 36); rsp.setContentType(multipartMimeType+";boundary=\""+boundary+"\""); rsp.setHeader("Expires","1 Jan 1971"); out = rsp.getOutputStream(); out.print("--"+boundary+CRLF); out.flush(); /* SHOULD WE USE CLOSE FOR HTTP1.1 ???? */ if ("HTTP/1.1".equals(req.getProtocol())) { rsp.setHeader("Connection", "close"); } } /** * Determine if our remote (browser) agent can handle multipart. */ static public boolean agentCanHandleMultipart(HttpServletRequest req) throws IOException { String ua = req.getHeader("User-Agent"); // Fpr now assume only MS Internet Exploder cannot do Multipart if (ua != null && ua.indexOf("MSIE") > 0) { return false; } // Assume all others (NS,...) support it return true; } /** * Begin a next part containing plain HTML. */ public void beginPart() throws IOException { beginPart("text/html"); } /** * Begin a next part with specified MIME content type. */ public void beginPart(String contentType) throws IOException { out.print("Content-type: "+contentType+CRLF+CRLF); } /** * End the current part but not the response. */ public void endPart() throws IOException { endPart(false); } /** End the entire response. */ public void endResponse() throws IOException { endPart(true); } /** * End the current part; optionally ends the response. */ public void endPart(boolean endRsp) throws IOException { out.print(CRLF+"--"+boundary+(endRsp?"--":"")+CRLF); out.flush(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -