【发布时间】:2016-08-10 15:10:25
【问题描述】:
我的目标是从另一个文件中删除一个文件的内容,我可以通过 HttpURLConnection 访问这些文件。
我的想法是从第一个文件中获取内容长度,我们将这个内容长度称为 N。并从第二个输入流(file2)中删除N个字节。
HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
HttpURLConnection connection2 = (HttpURLConnection) url2.openConnection();
String contentLength1 = connection1.getHeaderFields().get("Content-Length").get(0);
String contentLength2 = connection2.getHeaderFields().get("Content-Length").get(0);
InputStream is = connection2.getInputStream();
编辑:
我找到了办法,不知道有没有更好的办法。
ByteArrayOutputStream into = new ByteArrayOutputStream();
byte[] buf = new byte[4096];
for (int n; 0 < (n = is.read(buf));) {
into.write(buf, 0, n);
}
into.close();
byte[] data = into.toByteArray();
int length1 = Integer.parseInt(contentLength1);
int length2 = Integer.parseInt(contentLength2);
byte[] newData = new byte[length2-length1];
System.arraycopy(data, 0, newData, 0, newData.length);
ByteArrayInputStream newStream = new ByteArrayInputStream(newData);
【问题讨论】:
-
请标记您的平台。还请说明到目前为止您所尝试的问题,因为问题太宽泛了 - 请参阅stackoverflow.com/help/how-to-ask
-
@Yassine 你在用 Java 编码吗?这是我所知道的唯一一种在标准库中包含
HttpURLConnection类的流行语言。请出示相关代码供我们帮助。 -
我编辑了我的问题以提供有关我的问题的更多信息
标签: java byte inputstream httpurlconnection