不知道我在這裏可能會做錯什麼。當我試着用螢火蟲來看看回應時,它只是說我需要重新加載頁面才能獲得它的源代碼。我嘗試使用GZIPOutputStream代替,但後來它只是在開頭寫了一些奇怪的字符,甚至沒有生成有效的頭文件。gzip壓縮不會與我自定義的java HTTP服務器一起工作
還嘗試了幾個其他的隨機事物,沒有一個做任何幫助,所以與試驗和錯誤,並與開明的stackoverflow智慧。
這是怎麼還在抱怨我需要添加上下文?
嗯,我真的很累,並且傾斜,我的編碼技能已經退化,只是在顯示器上大喊大叫。那對於上下文來說如何?
噢,我使用的服務器是一個NanoHTTPD的模組,我需要使用它,因爲這個原因。
private void sendResponse(String status, String mime, Properties header, InputStream data,boolean acceptEncoding)
{
try
{
if (status == null)
throw new Error("sendResponse(): Status can't be null.");
OutputStream out = mySocket.getOutputStream();
PrintWriter pw = new PrintWriter(out);
if(acceptEncoding){
out = new java.util.zip.DeflaterOutputStream(out);//tried using GZIPInputStream here
header.setProperty("Content-Encoding","deflate"); // and gzip here, worked even worse
}
pw.print("HTTP/1.1 " + status + " \r\n");
if (mime != null)
pw.print("Content-Type: " + mime + "\r\n");
if (header == null || header.getProperty("Date") == null)
pw.print("Date: " + gmtFrmt.format(new Date()) + "\r\n");
if (header != null)
{
Enumeration e = header.keys();
while (e.hasMoreElements())
{
String key = (String)e.nextElement();
String value = header.getProperty(key);
pw.print(key + ": " + value + "\r\n");
}
}
pw.print("\r\n");
pw.flush();
if (data != null)
{
byte[] buff = new byte[2048];
while (true)
{
int read = data.read(buff, 0, 2048);
if (read <= 0)
break;
out.write(buff, 0, read);
}
}
out.flush();
out.close();
if (data != null)
data.close();
}
catch(IOException ioe)
{
// Couldn't write? No can do.
try { mySocket.close(); } catch(Throwable t) {}
}
}
試圖刪除Content-Length和Content-Range頭部參數,根本沒有任何幫助。 – Seppo420