이 방법을 사용하면 파일을 즉시 패키지하고 캐시를 사용하지 않고 포장 중에 파일을 전송할 수있어 사용자가 0을 기다릴 수 있습니다!
코드 사본은 다음과 같습니다.
/**
*
* MySocket 클라이언트 소켓
* @param 파일 포장 할 폴더 또는 파일
* @param filename 패키지 다운로드의 파일 이름
* @throws ioexception
*/
private void down (파일 파일, 문자열 filename)은 ioexception {
outputStream outputStream = mySocket.getOutputStream ();
StringBuffer SB = New StringBuffer ( "HTTP/1.1 200 OK/R/N");
sb.append ( "서버 : Java/1.1/r/n");
SB.Append ( "내용 유형 : 응용 프로그램/Octet-stream; charset = utf-8/r/n");
//sb.append("user-agent : Mozilla/4.0 (호환 가능; msie6.0; Windows NT 5.0)/r/n ");
SB.Append ( "내용화 : 첨부 파일; filename =" + filename
+ "/r/n");
sb.append ( "전송 인코딩 : 청크/r/n");
SB.Append ( "연결 : repoy-alive/r/n/r/n");
outputStream.write (sb.toString (). getBytes ());
outputStream.flush ();
ZipCompressor ZipCompressor = 새로운 Zipcompressor (New MyoutputStream (
출력 스트림));
zipcompressor.compress (파일);
System.out.println ( "Zip End");
System.out.println ( "쓰기 '0 // r // n // r // n'");
outputStream.write ( "0/r/n/r/n".getBytes ()); // 전송 인코딩 : 청크 전송 엔드 플래그
outputStream.flush ();
outputStream.close ();
System.out.println ( "다운로드 중지");
노력하다 {
mysocket.close ();
} catch (Throwable t) {
}
}
코드 사본은 다음과 같습니다.
패키지 cn.liangjintang.webserver.zipfile;
import java.io.bufferedInputStream;
import java.io.file;
import java.io.fileInputStream;
import java.io.outputStream;
import java.lang.reflect.field;
import java.util.zip.crc32;
import java.util.zip.checkedoutputstream;
java.util.zip.zipentry import;
import java.util.zip.zipoutputstream;
공개 수업 zipcompressor {
정적 최종 int 버퍼 = 8192;
개인 출력 스트림 출력 스트림;
public zipcompressor (myoutputstream outputstream) {
this.outputStream = outputStream;
}
공개 void compress (파일 파일) {
if (! file.exists ())
새로운 runtimeexception을 던지십시오 (file.getabsolutepath () + "존재하지 않습니다!");
노력하다 {
CheckedOutputStream cos = 새로운 CheckedOutputStream (outputStream,
새로운 CRC32 ());
ZipOutputStream out = 새로운 ZipOutputStream (COS);
String basedir = "";
압축 (파일, out, basediir);
out.close (); // zip의 최종 정보가 작성되도록 닫아야합니다. 그렇지 않으면 ZIP 파일이 계속 작성되면 outputStream.close () 메소드를 다시 작성할 수 있습니다.
} catch (예외 e) {
새로운 runtimeexception (e)을 던지십시오.
}
}
개인 void compress (파일 파일, zipoutputStream out, String Basedir) {
// 디렉토리인지 파일인지 판단합니다
if (file.isdirectory ()) {
System.out.println ( "압축 :" + basedir + file.getName ());
이.
} 또 다른 {
System.out.println ( "압축 :" + basedir + file.getName ());
this.compressfile (파일, out, basediir);
}
}
// 디렉토리를 압축합니다
개인 void compressDirectory (파일 dir, zipoutputStream out, String Basedir) {
if (! dir.exists ())
반품;
file [] files = dir.listfiles ();
for (int i = 0; i <files.length; i ++) {
/** 재귀*/
compress (files [i], out, basedir + dir.getname () + "/");
}
}
// 파일을 압축합니다
private void compressfile (파일 파일, zipoutputStream out, String Basedir) {
if (! file.exists ()) {
반품;
}
노력하다 {
bufferedInputStream bis = 새로운 bufferedInputStream (
새 fileInputStream (file));
zipentry entry = new Zipentry (기반 + file.getName ());
out.putnextentry (Entry);
int count;
바이트 데이터 [] = 새로운 바이트 [버퍼];
while ((count = bis.read (data, 0, buffer))! = -1) {
out.write (data, 0, count);
}
bis.close ();
} catch (예외 e) {
새로운 runtimeexception (e)을 던지십시오.
}
}
}
코드 사본은 다음과 같습니다.
패키지 cn.liangjintang.webserver.zipfile;
import java.io.filteroutputStream;
import java.io.ioexception;
import java.io.outputStream;
공개 클래스 MyOutputStream 확장 FilterOutputStream {
public myoutputStream (outputStream out) {
슈퍼 (아웃);
}
최종 바이트 [] OneBytes = "1/r/n".getBytes ();
최종 바이트 [] rnbytes = "/r/n".getBytes ();
공개 void write (int b)는 ioexception {
out.write (onebytes); // bytes 1+crlf
out.write (b); // 데이터 엔티티
out.write (rnbytes); // crlf
}
public void write (byte [] b)는 ioexception {
out.write (integer.tohexstring (b.length) .getBytes ()); // bytes, hexadecimal
out.write (rnbytes); // crlf
out.write (b); // 데이터 엔티티
out.write (rnbytes); // crlf
}
public void write (byte [] b, int off, int len) ioexception {
out.write (integer.tohexstring (len -off) .getBytes ()); // bytes, hexadecimal
out.write (rnbytes); // crlf
out.write (b, off, len); // 데이터 엔티티
out.write (rnbytes); // crlf
}
/**
*이 메소드를 다시 작성하십시오. 그렇지 않으면 OutputStream이 닫히고 다른 데이터 <br/>
* (예를 들어, 전송 인코딩 : 청크 트랜스미션 엔드 플래그) 더 이상 계속 쓰일 수 없습니다.
*/
public void close ()는 ioexception {
}
}