Este artigo descreve o método de programação Java para implementar o suporte ao lado do servidor para transmissão contínua do ponto de interrupção. Compartilhe -o para sua referência, como segue:
Todo mundo sabe que os fluxos do Tomcat podem suportar a transmissão contínua do ponto de interrupção para recursos estáticos, mas se for um fluxo controlado, se tiver controle de permissão ou se o endereço de download for apenas um proxy, você precisar . O código é publicado e todos vão compartilhar juntos:
Servlet
importar java.io.bufferedOutputStream; importar java.io.file; importar java.io.ioException; importar java.io.randomaccessfile; importar java.io.unsupPorp edEncodingException; import java.net.uri; importação java.net.urisnyxception; importar java.net.urldecoder; importar javax.servlet.servletexception; importar javax.servlet.servletoutputStream; importar javax.servlet.http.httpservletrequest; Base .BaseServlet; importar com.bsteel.cloud.storage.utils.fileutil;/** * Download de arquivo (suporta transmissão contínua do ponto de interrupção [Thunder/Express/Tornado/Firefox/Chrome]) * @author jdkleo * */classe pública f IleiosServlet estende BaseServlet {private estático final serialversionuid = 1L; onse resposta) : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : ::::::::::::::::::::::Here :::::::::::::::::::hee :::::::::::::::::heo the :::::::::::::::::::::::::::::here ::: lança servletexception, ioexception {download (request, resposta); Param Solicitação * @param Resposta * @THOWS UNSUPPORTEDENCEDENCODING GEXCECTION */ Download de void privado (solicitação de HttpServletRequest, Resposta HttpServletResponse) lança sem suportesencodingException {download filefile = getFile (request); Log.info (Skip "+POS); ); ))! = -1) {out.write (b, 0, n); , e); null) {tente {raf.close (); getParameter ("URI"); (7); : ::::::::::::::::::::::Here :::::::::::::::::::hee :::::::::::::::::heo the : ::::::::::::::::::::::Here :::::::::::::::::::hee :::::::::::::::::heo the ::::: um Uri real ");}}
Suporte à faixa
importar java.io.file; importar javax.servlet.http.httpServletRequest; importar javax.servlet.http.httpServletResponse;/*** Ferramenta de processamento de arquivos* @author jdkleo**/public FileUtil {*** quebra Suporte * Arquivo @param * @param request * @param Resposta * @return Quantos bytes pularem */ public static Long HeaderSetting (arquivo de arquivo, solicitação httpServletRequest, httpServleTres Ponse Response) {Len Len = file.Length (); Comprimento do arquivo if (null == request.getheader ("range")) {setResponse (novos intervalos (len), file.getName (), resposta); Replacado ("Bytes =" ""); Configurações, nome do arquivo da string, resposta httpServletResponse) {Response.addheader ("Content-Disposition", "Applement; nome do arquivo =/" " + ioutil.toutf8string (filenam e) +"/""); nome do arquivo)); // Defina o tipo MIME. Settings.getStart (); String.valueof (contentLength)); Settings.getToTALL Length ()). ToStrin G (); end = 0; START = LEN -CONTEÚDO; ; 1]);
Pacote de gama
Classe pública Rangettings {Start Private Start; {this.start = start; ) {return start; ) {Return ContentLenth; () {Return range;}}
IO Stream Related Processing Tool Class
importar java.io.inputStream; public classe 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 = "imagem/tiff"; equals ("ico")) {contentType = "imagem/x-icon"; "BMP") {contentType = "Image/X-MS-BMP"; "JAR") ||. Tipo de conteúdo = "Application/msword"; /rtf ";} else if (returnfilename .equals (" xls ")) {contentType =" Application/vnd.ms-excel ";} else if (returnfilename.equals (" ppt ")) {contentType =" Application/VND .ms-Po werpoint ";} else if (returnfileName.equals (" 7z ")) {contentType =" Application/X-7z compactados ";} else if (returnFilename.equals (" rar ")) {contentType =" Appli-cation/x-rar comprovado ";} else if (returnfileName.equals (" swf ")) {contentType =" Application/x-shockwave-flash ";} else (returnFilename.equals (" rpm ") { Tipo de conteúdo = "Application/X-Rredhat-Package- Gerenciador"; = "Applicati ON/X-X509-CAt"; ) {conttype = "Application/Zip"; midi ";} else if (returnfilename.equals (" mp3 ")) {contentType =" audio/mpeg ";} else if (returnfilename.equals (" ogg ")) {contentType =" audio/ogg ";} se se (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 =" video/x-flv ";} else if (returnfileName.equals (" m4v ")) {contentType =" 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 ";} retorna contentType;} // utf8 transcodificando public static string toutf8string (string s) {stringbuffer sb = new stringbuffer (); int len = s.ToCharArray () .Length; else {byte [] b; tente {b = caractere.ToString (c) .getBytes ("utf-8"); ; ) .TOUPCASE (); Skipe (inputStream, Longo, Long) lança ex -cepção {Long Remaintning = Howmany;
Nota: Algumas categorias como o método ioutil vem do resumo dos internautas do CSDN.
Espero que este artigo seja útil para a programação Java de todos.