我使用的是GZIPOutputStream的GZIP類的圖像。我嘗試通過OutputStream發送GZIP文件時收到損壞的文件。我知道如何將GZIP轉換爲FileOutputStream。下面的代碼工作完美:如何在Java上通過GZIP文件並通過OutputStream發送它
Static private void GZIPCompress(String fileName)
{
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
FileOutputStream fos = new FileOutputStream(fileName + ".gz");
GZIPOutputStream gzos = new GZIPOutputStream(fos);
gzos.write(data);
fos.close();
fis.close();
}
輸出文件是myfile.png.gz並具有以下細節:
myfile.png.gz:gzip壓縮的數據,從FAT文件( MS-DOS,OS/2, NT)
我的問題是,當我嘗試GZIP文件並將其發送到的OutputStream。由於我正在使用服務器,因此我正在從服務器調用此服務器,並使用套接字。
Static void SendGZIPFile(String fileName, OutputStream os)
{
DataOutputStream dos = new DataOutputStream(os);
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
byte[] dataAux = new byte[(int) file.length()];
dos.writeBytes("HTTP/1.1 200 OK\r\n");
dos.writeBytes("Content-Type: application/x-gzip \r\n");
dos.writeBytes("Content-Disposition: form-data; filename="+"\""+fileName+".gz"+"\""+"\n");
dos.writeBytes("\r\n\r\n");
dos = new DataOutputStream(new GZIPOutputStream(os));
fis.read(data);
dos.write(data);
dos.close();
fis.close();
gzos.close();
}
我得到的是沒有包含任何損壞的GZIP文件:這裏是細節:
myfile.gz數據
我覺得我做錯了什麼而GZipping,因爲我注意到詳細信息之間的差異。我用下面的命令來得到它:file myfile.gz
只是出於興趣,你爲什麼要手工做HTTP,而不是使用庫/框架? – RAnders00
PNG已經被壓縮,所以試圖GZIP它可能會使它更大。 –
你能否也請顯示調用'SendGZIPFile(fileName,outputStream)'的部分? – RAnders00