今天使用HttpURLConnection類的getContentLength()方法時得到-1, 這是爲什麼呢??
是這樣的, 用HttpURLConnection的getContentLength獲取傳輸數據的字節數時, 必須與服務器端協商, 即服務器端必須設置過"content-length"頭:
HttpURLConnection.getContentLength()方法對應於服務端的的HttpServletResponse.setContentLength(int)
HttpURLConnection.getContentLengthLong()方法對應於服務端的HttpServletResponse.setContentLengthLong(long)
如果服務端沒有設置Content-Longth, 那麼客戶端獲取Content-Length時就是-1
下面是client和server端的相關代碼:
client:
public class Demo2 {
public static void main(String[] argv) throws IOException {
URL url = new URL("http://127.0.0.1:8080/webserver01/demo01");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//關於下面一句, 參考: http://my.oschina.net/u/133352/blog/96582
conn .setRequestProperty("Accept-Encoding", "identity");
conn.connect();
//必須要服務器的HttpServletResponse.setContentLength(int), 否則下面的getContentLength()將返回-1
int length = conn.getContentLength(); //對應於HttpServletResponse.setContentLength(int)
long length1 = conn.getContentLengthLong(); //對應於HttpServletResponse.setContentLengthLong(long)
InputStream in = conn.getInputStream();
ReadableByteChannel srcChannel = Channels.newChannel(in);
//將網絡通道中的數據寫入到本地文件中
File file = new File("out.txt");
FileOutputStream fout = new FileOutputStream(file);
FileChannel dstChannel = fout.getChannel();
dstChannel.transferFrom(srcChannel, 0, length1);
//釋放資源
dstChannel.close();
fout.close();
srcChannel.close();
in.close();
conn.disconnect();
}
}
server:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Demo01
*/
@WebServlet("/demo01")
public class Demo01 extends HttpServlet {
private static final long serialVersionUID = 1L;
public Demo01() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
File file = new File("aa.txt");
File file1 = new File(getServletContext().getRealPath("/files/test.txt"));
if(file1.exists()) {
System.out.println("exists ......");
}
ServletOutputStream out = response.getOutputStream();
/* 客戶端獲取傳輸數據長度
conn.getContentLength();
conn.getContentLengthLong();
*/
// response.setContentLength((int)file1.length()); //對應於HttpURLConnection.getContentLength()
response.setContentLengthLong(file1.length()); //對應於HttpURLConnection.getContentLengthLong()
FileChannel channel = new FileInputStream(file1).getChannel();
channel.transferTo(0, file1.length(), Channels.newChannel(out));
channel.close();
out.close();
}
}