이 기사에서는 중단 점 연속 전송에 대한 서버 측 지원을 구현하는 Java 프로그래밍 방법에 대해 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
Tomcat 스트림은 정적 자원에 대한 중단 점 연속 전송을 지원할 수 있다는 것을 모두 알고 있지만, 제어 스트림 인 경우 권한 제어가 있거나 다운로드 주소가 프록시 인 경우 직접 브레이크 포인트 연속 전송에 대한 지원을 구현해야합니다. , 나는 재능이 없다. 여기서 우리는 기본 브레이크 포인트 연속 전송 [A-, -B, AB]의 간단한 구현을 제공하며, 이는 Thunder 7 및 Firefox의 다중 중단 점 연속 전송을 지원하는 것으로 입증되었습니다. 코드가 게시되고 모든 사람이 함께 공유합니다.
서틀
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.net.net.urisyntaxception; import java.net.urldecoder; import javax.servlet.servletexception; import javax.servlet.servletoutputstream; import javax.servlet.http.httpervletrequest import javax.servlet.http.httpervletreponse; Base .BaseServlet; import com.bsteel.cloud.storage.utils.fileutil;/** * 파일 다운로드 (파일 다운로드 (Breakpoint Continuous Transmission 지원 Ileioservlet는 {private static final long serialversionuid = 1l을 확장합니다 onse 응답) : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : param request * @param response * @throws UnsupportedEncodin gexception */ private void download (httpservletrequest 요청, httpservletreponse response)는 unsupportedencodingexception {file downloadfile = getfile (request); log.info (spew "+pos); out = 새로운 BufferedOutputStream (OS); ))! = -1) {out.write (b, 0, n); , e); {if (out! = null) {out.close () catch (ioexception e) {log.error (e.getMessage (), e); null) {try {raf.close ()} catch (ioexception e) {log.error (e.getMessage (),}}; getParameter ( "uri"); if (null! = urist) {urist = urldecoder.decode ( "utf-8"); (7); 반환 파일 (urist)} else if (urist.startswith ( "hbase : //")) {: : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : return new 파일 (new uri (urist))} catch (urisyntaxexception e) {log.error (e.getMessage (),}}} 새 runtimeexception ( "It ' 진짜 uri ");}}
범위 지원
import java.io.file; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse;/*** 파일 처리 도구* @author jdkleo**/public class fileUtil {/*** breakpoint contination pass 지원 * @param 파일 * @param request * @param response * @return 건너 뛰기위한 바이트 수 */ public static long headersetting (파일 파일, httpservletrequest request, httpservletres ponse responsk) {long len = file.length (); // 파일 길이 if (null == request.getheader ( "range")) {setresponse (len), file.getName (), reture 0); REPLACEALL (BYTES = ", rangesettings (range); 설정, 문자열 파일 이름, httpservletresponse 응답) {response.addheader ( "content-disposition", "첨부 파일;" " + ioutil.toutf8string (filenam e) +"/""); filename); // mime 유형을 설정합니다 getStart (); Long Ends.getend (); getContentLength (); string.valueof (contentlength)); Strange = new StringBuffer ( "bytes") .append (start) .append ( "-"). setting.getTallength (). tostrin g () end = 0; if.startSwith ( " -")) // -500, 마지막 500 {contentlength = long.parselong (range.substring (1)); // 다운로드 할 금액 = len -1 start = len -contentlength; contentlength = len} else // a {stare.split ( " -"); 1]);
범위 패키지
공공 클래스 세팅 {개인 장거리 antall 길이; {start. end. ) {반환 시작; ) {returnlength; () {반환 범위}}
IO 스트림 관련 처리 도구 클래스
import java.io.inputStream; public class ioutil {public static string setContentType (String ReturnFilename) {application/octet -Stream "; .TOLOWERCASE (); returnFilename = returnFilename.SubString (returnFilename.lastIndexof ( ")+1; "shtml")) {contenttype = "text/html"; {contentType = "text/xml"} else if (returnfilename.equals ( "gif")) {intecttype = "image/gif"; "jpg")) {contenttype = "image/jpeg"; )) {contenttype = "Application/Atom+XML"} else if (returnFilename.equals ( "rss")) {contenttype = "application/rss+xml"; ) {contenttype = "text/mathml"} else if (returnfilename.equals ( "txt")) {reptionfilename.equals ( "jad")) {contenttype = "텍스트 /vnd.sun. )) {contenttype = "text/x-component"; filename.equals ( "tiff")) {contenttype = "image/tiff"; equals ( "ico")) {contenttype = "image/x-icon"else if (returnfilename.equals ( "jng")) {incenttype = "image/x-jng"; "bmp")) {contenttype = "image/x-ms-bmp"; "jar"|| returnfilename ( "var") || content type = "application/msword"; else if (returnfilename.equals ( "pdf")) {contenttype = "application/pdf"; /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- 코프워드 ";} else if (returnFilename.equals ("rar ")) {contentType =" Appli Cation/x-rar-compressed ";} else if (returnfilename.equals ("swf ")) {contenttype ="application/x-shockwave-flash ";} else if (returnfilename.equals ("rpm ")) { Content Type = "Application/X-REDHAT-PACKAGE-MANGER"} else if (returnFilename.equals ( "der") || returnFilename.equals ( "pem") || returnFilename.equals ( "crt")) {contenttype = "/x-x509-ca-cert on} else if (returnfilename.equals ("xhtml ")) {contenttype ="application/xhtml+xml "; ) {cont Enttype = "application/zip"} else if (returnfilename.equals ( "mid") || returnfilename.equals ( "kar") {contenttype = "Audio/ midi ";} else if (returnfilename.equals ("mp3 ")) {contenttype ="audio/mpeg ";} else if (returnfilename.equals ("oogg ")) {contenttype ="Audio/Ogg ";} else if if if. (returnfilename.equals ( "m 4a")) {contenttype = "audio/x-m4a"; returnfilename.equals ( "3gpp") || } else if (returnFilename.equal s ( "mpeg") || returnFilename .Equals ( "mpg")) {contentType = "video/mpeg"else if (returnFilename.equals ( "mov")) {contentType = " 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"} else (returnfilename.equals ( "wmv")) {video/x-ms-wmv "; avi "))) {contenttype ="video/x-msv ideo ";} return contenttype;} // utf8 public static string toutf8string (String s) {StringBuffer sb = new StringBuffer (); int len = s.tochararray (). lenger (int i = 0; i <len; i ++) {char c = s.charat (c> = 0 && c <= 255); {byte [] b; b = 문자. (int j = 0; J ++) {int k = b [j]; ).}} string (0, sb.length); 건너 뛰기 (Long Howmany)는 겨우 {Howmany = 0;
ioutil 방법과 같은 일부 범주는 CSDN 네티즌 요약에서 나옵니다.이 범주는 [AB, CD, -E] 등과 같은 여러 범위 구성을 지원하지 않습니다.
이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.