使用HttpURLConnection的getContentLength()方法返回-1??

当使用HttpURLConnection的getContentLength()方法获取数据长度时遇到返回-1的情况,原因在于服务器未设置'content-length'头。解决办法是确保服务器端设置HttpServletResponse的setContentLength或setContentLengthLong方法来指定内容长度。
摘要由CSDN通过智能技术生成

今天使用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(); </
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Java使用HttpURLConnection上传文件可以通过以下步骤实现: 1. 创建URL对象,指定上传文件的URL地址。 2. 打开HTTP连接,并设置请求方法为POST。 3. 设置HTTP请求部信息,包括Content-Type和boundary。 4. 设置HTTP请求体信息,包括上传文件的内容和分割线。 5. 读取服务器端返回的响应信息。 示例代码如下: ```java import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class FileUploader { public static void main(String[] args) throws Exception { String urlStr = "http://example.com/upload"; String filePath = "C:/test.txt"; URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); String boundary = "---------------------------" + System.currentTimeMillis(); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream out = conn.getOutputStream(); // 写入分割线 out.write(("--" + boundary + "\r\n").getBytes()); // 写入文件名 out.write(("Content-Disposition: form-data; name=\"file\"; filename=\"" + filePath + "\"\r\n").getBytes()); // 写入文件类型 out.write(("Content-Type: application/octet-stream\r\n").getBytes()); // 写入空行 out.write(("\r\n").getBytes()); // 写入文件内容 FileInputStream fileIn = new FileInputStream(filePath); byte[] buffer = new byte[1024]; int len; while ((len = fileIn.read(buffer)) != -1) { out.write(buffer, 0, len); } fileIn.close(); // 写入空行 out.write(("\r\n").getBytes()); // 写入分割线 out.write(("--" + boundary + "--\r\n").getBytes()); out.flush(); out.close(); // 读取响应内容 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } ``` 其中,filePath为需要上传的文件路径,urlStr为服务器端接收文件的URL地址。在示例代码中,我们使用了multipart/form-data类型的请求体格式,可以在请求部信息中设置Content-Disposition、Content-Type等参数。同时,我们也设置了一个分割线boundary,用于分隔不同的请求体部分。最后,我们通过读取服务器端返回的响应信息,来获取上传文件的结果。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值