inputstream 占用内存吗_java - 确定InputStream的大小 - 堆栈内存溢出

如果您知道您的InputStream是FileInputStream或ByteArrayInputStream ,您可以使用一点反射来获取流大小而不读取整个内容 。 这是一个示例方法:

static long getInputLength(InputStream inputStream) {

try {

if (inputStream instanceof FilterInputStream) {

FilterInputStream filtered = (FilterInputStream)inputStream;

Field field = FilterInputStream.class.getDeclaredField("in");

field.setAccessible(true);

InputStream internal = (InputStream) field.get(filtered);

return getInputLength(internal);

} else if (inputStream instanceof ByteArrayInputStream) {

ByteArrayInputStream wrapper = (ByteArrayInputStream)inputStream;

Field field = ByteArrayInputStream.class.getDeclaredField("buf");

field.setAccessible(true);

byte[] buffer = (byte[])field.get(wrapper);

return buffer.length;

} else if (inputStream instanceof FileInputStream) {

FileInputStream fileStream = (FileInputStream)inputStream;

return fileStream.getChannel().size();

}

} catch (NoSuchFieldException | IllegalAccessException | IOException exception) {

// Ignore all errors and just return -1.

}

return -1;

}

我相信,这可以扩展到支持额外的输入流。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中使用HttpURLConnection上传文件需要以下步骤: 1. 创建URL对象 ```java URL url = new URL(uploadUrl); ``` 2. 创建HttpURLConnection对象 ```java HttpURLConnection conn = (HttpURLConnection) url.openConnection(); ``` 3. 设置请求方式为POST,设置其他请求头参数 ```java conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); ``` 4. 创建输出流并将数据写入输出流 ```java DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream()); outputStream.writeBytes("--" + BOUNDARY + "\r\n"); outputStream.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n"); outputStream.writeBytes("\r\n"); FileInputStream fileInputStream = new FileInputStream(file); byte[] buffer = new byte[1024]; int len; while ((len = fileInputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } outputStream.writeBytes("\r\n--" + BOUNDARY + "--\r\n"); ``` 5. 获取响应结果 ```java InputStream inputStream = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder result = new StringBuilder(); while ((line = reader.readLine()) != null) { result.append(line); } ``` 完整的代码示例: ```java public static void uploadFile(File file, String uploadUrl) throws Exception { String BOUNDARY = "---------------------------123821742118716"; URL url = new URL(uploadUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("Charset", "UTF-8"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); DataOutputStream outputStream = new DataOutputStream(conn.getOutputStream()); outputStream.writeBytes("--" + BOUNDARY + "\r\n"); outputStream.writeBytes("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n"); outputStream.writeBytes("\r\n"); FileInputStream fileInputStream = new FileInputStream(file); byte[] buffer = new byte[1024]; int len; while ((len = fileInputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } outputStream.writeBytes("\r\n--" + BOUNDARY + "--\r\n"); InputStream inputStream = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder result = new StringBuilder(); while ((line = reader.readLine()) != null) { result.append(line); } outputStream.close(); fileInputStream.close(); inputStream.close(); conn.disconnect(); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值