Este artículo describe el método de programación Java para implementar el soporte del lado del servidor para la transmisión continua del punto de interrupción. Compártelo para su referencia, como sigue:
Todos saben que las transmisiones de Tomcat pueden soportar la transmisión continua del punto de interrupción para recursos estáticos, pero si es una transmisión controlada, si tiene control de permiso, o si la dirección de descarga es solo un proxy, debe implementar el soporte para la transmisión continua de Breakpoint por usted. ., No soy talentoso, aquí proporcionamos una implementación simple de la transmisión continua de punto de ruptura básica [A-,-B, AB], que se ha demostrado que admite múltiples transmisiones continuas de Thunder 7 y Firefox. El código se publica y todos lo compartirán juntos:
Servir
import java.io.bufferedOutputStream; import java.io.file; import java.io.ioException; import java.io.randomaccessfile; import java.io.unsupport edencodingException; import java.net.uri; import java.neta.urisyntaxexception; import java.net.urldecoder; import javax.servlet.servletException; import javax.servlet.servletutputstream; import javax.servlet.http.httpservletrequest; base .BasesServlet; import com.bsteel.cloud.storage.utils.fileUtil;/** * Descargar el archivo (admite la transmisión continua del punto de interrupción [Thunder/Express/Tornado/Firefox/Chrome]) * @Author JDKLeo * */clase pública F ileIoServlet extends BaseServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) thro ws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse respuesta) : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :::::::::::::::::::::::::::::::::::::::::::::::::::::: ::: ::::::::::::::::::::::::::::: Surly ServletException, IOException {Descargar (solicitud, respuesta); Solicitud de param * @param Respuesta * @throws UnsupportedEncodin Gexception */ private void download (HttpServletRequest Soly, httpServletResponse Response) lanza UnsupportedEnCodingException {downloadfile = getFile (request); log.info ("Skip"+Pos); ); )! = -1) {out.write (b, 0, n); , e); nulo) {try {raf.close (); GetParameter ("Uri"); (7) devuelve el nuevo archivo (Uristr); :::::::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::::::: : ::::: {return nuevo archivo (nuevo uri (urist)); un uri real ");}}
Soporte de rango
import java.io.file; import javax.servlet.http.httpservletRequest; import javax.servlet.http.httpservletResponse;/*** Herramienta de procesamiento de archivos* @author jdkeo**/public class Fileutil {/*** PASE DE CONTINUACIÓN Soporte * @param archivo * @param solicitud * @param respuesta * @return cuántos bytes se omitir */ public static long Headersetting (archivo de archivo, solicitud httpservletrequest, respuesta httpservletres ponde) {long len = file.length (); // Longitud del archivo if (null == request.getheader ("rango")) {setResponse (new RangeSettings (len), file.getName (), respuesta); . Configuración, Nombre de archivo de cadena, HttpServletResponse Respuesta) {Response.AddHeader ("Content-Disposition", "Adjunta; FileName =/" " + Ioutil.Toutf8String (FileNam E) +"/""); nombre de archivo)); // Establezca el tipo de mime. settings.getStart (); String.ValueOf (contentLength)); settings.gettotall longitud end = 0; ; ; 1]);
Paquete de gama
Public Class RangeSettings {Inicio Privado de Long; {this.Start = Start; ) {return start; ) {return contentLength; () {rango de retorno;
Clase de herramienta de procesamiento relacionada con la transmisión de IO
import java.io.inputstream; public class Ioutil {public static String setContentType (String returnFileName) {String ContentType = "Application/Octet -Stream"; .tolowercase (); "shtml")) {contentType = "text/html"; {contentType = "text/xml"; "jpg")) {contentType = "image/jpeg"; )) {contentType = "Application/Atom+Xml"; ) {contentType = "text/Mathml"; /vnd.sun. )) {contentType = "text/x-component"; FileName.equals ("TIFF")) {contentType = "Image/tiff"; iguales ("ico")) {contentType = "image/x-icon"; "bmp")) {contentType = "image/x-ms-bmp"; "JAR") || Content Type = "Application/MSWord"; /rtf ";} else if (returnFileName .equals (" xls "))) {contentType =" Application/vnd.ms-Excel ";} else if (returnFileName.equals (" ppt ")) {contentype =" Application/vnd .ms-po werpoint ";} else if (returnFileName.equals (" 7z ")) {contentType =" Application/X-7Z-Comprised ";} else if (returnFileName.equals (" RAR ")) {ContentType =" aplication/x-rar-comprimido ";} else if (returnFileName.equals (" swf ")) {contentType =" Application/x-shockwave-flash ";} else if (returnFileName.equals (" rpm ")) { Content Type = "Application/X-Redhat-Package- Manager"; = "Applicati On/X-X509-Ca-Cert"; ) {cont EntType = "Application/Zip"; midi ";} else if (returnFileName.equals (" mp3 ")) {contentype =" audio/mpeg ";} else if (returnFileName.equals (" ogg ")) {contentype =" audio/ogg ";} else if si (returnFileName.equals ("m 4a")) {contentType = "audio/x-m4a"; returnFileName.equals ("3gpp") || } else if (returnFileName.equal s ("mpeg") || returnFileName .equals ("mpg")) {contentType = "video/mpeg"; video/quicktime ";} else if (retu rnfilename.equals (" flv ")) {contentType =" videType/"Video/x -m4v ";} else if (returnFileName.equals ls (" mng ")) {contentType =" video/x-mng ";} else if (returnFileName.equals (" asx ") || returnFileName.equals (" asf " )) {contentType = "video/x-ms-asf"; avi ")) {contentType =" video/x-msv ideO ";} return contentType;} // utf8 transcoding public static string toutf8String (String S) {StringBuffer sb = new StringBuffer (); int Len = S.ToCharArray () .length; else {byte [] b; ; ) .ToUpperCase ()); Skiply (InputStream in, Long HowMany) lanza Exception {Long Rephed = HowMany;
Tenga en cuenta que algunas categorías, como el método Ioutil, provienen del resumen de los internautas CSDN.
Espero que este artículo sea útil para la programación Java de todos.