使用HttpURLConnection的getContentLength()方法返回-1??
来源:互联网 发布:腾讯视频网络电视版 编辑:程序博客网 时间:2024/06/05 03:23
今天使用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(); }}
0 0
- 使用HttpURLConnection的getContentLength()方法返回-1??
- HttpURLConnection getContentLength返回-1
- 解决 HttpURLConnection类中getContentLength()方法返回-1问题
- 关于httpurlconnection getcontentlength返回值为-1的问题
- HttpURLConnection getContentLength();返回时-1或者是0
- HttpURLConnection getContentLength();返回时-1或者是0
- HttpURLConnection getContentLength();返回时-1或者是0
- HttpURLConnection getContentLength();返回时-1或者是0
- HttpURLConnection getContentLength();返回时-1或者是0
- HttpURLConnection getContentLength();返回时-1或者是0
- Android HttpURLConnection.getContentLength() 等于 -1
- Android HttpURLConnection.getContentLength() 等于 -1
- HttpURLConnection.getContentLength()为-1 解决方法
- 关于inputStream.available()与HttpURLConnection.getContentLength()方法获取下载文件的大小
- 关于inputStream.available()与HttpURLConnection.getContentLength()方法获取下载文件的大小
- android getContentLength()函数返回为-1的问题
- 你看到的entity.getcontentlength返回-1了
- 使用HttpURLConnection 的post方法来返回一个xml
- tomcat启动时报错找不到mina的包
- XTU ACM 1195 Large Population 并查集->最小生成树
- 接口和抽象类有什么区别
- Linux下的代码统计工具--sloccount
- Xcode storyboard开发有感
- 使用HttpURLConnection的getContentLength()方法返回-1??
- Extjs中grid 的ColumnModel 属性配置
- 黑马程序员----集合框架(一)
- wcf的svcutil.exe工具生成代码的奇怪问题
- Zend Framework手册 中文版
- Javascript 模块化编程
- 用memset对非字符型数组初始化可能会出现错误
- 回调函数
- 2014年最热门的国人开发开源软件TOP100