java上传文件限速_java HttpClient 上传限速(避免宽带占用过高)
需求: 因为工作站网络上行带宽过高会影响其他服务的正常使用,所以要限速
HttpClient.java
package com.wuchen.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wuchen.constant.Constant;
import com.wuchen.servlet.LimitRateFileBody;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import java.io.File;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
public class HttpClient {
private static Integer socketTime = null;
private static Integer connectTime = null;
private static Integer connectionRequestTime = null;
private static Logger logger = Logger.getLogger(HttpClient.class);
private static String token = null;
public static JSONObject doPost(JSONObject params, String url) {
if (socketTime == null) {
Properties properties = new PropertiesUtil().readPropertiesFile("application.properties");
socketTime = Integer.valueOf(properties.getProperty("socketTime"));
connectTime = Integer.valueOf(properties.getProperty("connectTime"));
connectionRequestTime = Integer.valueOf(properties.getProperty("connectionRequestTime"));
}
org.apache.http.client.HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setHeader("Content-type", "application/json; charset=utf-8");
httpPost.setHeader("Connection", "Close");
StringEntity entity = new StringEntity(params.toString(), Charset.forName("UTF-8"));
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
String resultString = EntityUtils.toString(response.getEntity());
JSONObject rempResult = JSON.parseObject(resultString);
return rempResult;
} catch (Exception e) {
logger.error("error", e);
return null;
}
}
public static JSONObject postFrom(JSONObject params, List paths, String url) {
org.apache.http.client.HttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost httpPost = new HttpPost(url);
StringBody comment = new StringBody(params.toString(), ContentType.TEXT_PLAIN);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
for (String filePath : paths) {
multipartEntityBuilder.addPart("file", new FileBody(new File(filePath)));
}
HttpEntity reqEntity = multipartEntityBuilder.addPart("desc", comment).build();
httpPost.setEntity(reqEntity);
HttpResponse response = httpClient.execute(httpPost);
String resultString = EntityUtils.toString(response.getEntity());
JSONObject result = JSON.parseObject(resultString);
return result;
} catch (Exception e) {
logger.error("error", e);
return null;
}
}
public static JSONObject postFromListRate(JSONObject params, List paths, String url) {
org.apache.http.client.HttpClient httpClient = HttpClientBuilder.create().build();
try {
HttpPost httpPost = new HttpPost(url);
StringBody comment = new StringBody(params.toString(), ContentType.TEXT_PLAIN);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
for (String filePath : paths) {
multipartEntityBuilder.addPart("file", new LimitRateFileBody(new File(filePath), params.getInteger(Constant.MAX_RATE)));
}
HttpEntity reqEntity = multipartEntityBuilder.addPart("desc", comment).build();
httpPost.setEntity(reqEntity);
HttpResponse response = httpClient.execute(httpPost);
String resultString = EntityUtils.toString(response.getEntity());
JSONObject result = JSON.parseObject(resultString);
return result;
} catch (Exception e) {
logger.error("error", e);
return null;
}
}
}
LimitRateBody.java
package com.wuchen.servlet;
import com.wuchen.utils.BandwidthLimiter;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.util.Args;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* @author: WangXiang
* @date: 2020/4/28 0028
**/
public class LimitRateFileBody extends FileBody {
private final File file;
private final String filename;
//限速的大小
private int maxRate = 1024;
public LimitRateFileBody(File file, int maxRate) {
this(file, ContentType.DEFAULT_BINARY, file != null ? file.getName() : null);
this.maxRate = maxRate;
}
public LimitRateFileBody(File file) {
this(file, ContentType.DEFAULT_BINARY, file != null ? file.getName() : null);
}
public LimitRateFileBody(File file, ContentType contentType, String filename) {
super(file, contentType, filename);
this.file = file;
this.filename = filename;
}
public LimitRateFileBody(File file, ContentType contentType) {
this(file, contentType, file != null ? file.getName() : null);
}
@Override
public void writeTo(OutputStream out) throws IOException {
Args.notNull(out, "Output stream");
FileInputStream in = new FileInputStream(this.file);
LimitInputStream ls = new LimitInputStream(in, new BandwidthLimiter(this.maxRate));
try {
byte[] tmp = new byte[4096];
int l;
while ((l = ls.read(tmp)) != -1) {
out.write(tmp, 0, l);
}
out.flush();
} finally {
in.close();
}
}
public void setMaxRate(int maxRate) {
this.maxRate = maxRate;
}
}
LimitInputStream.java
package com.wuchen.servlet;
import com.wuchen.utils.BandwidthLimiter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* 限流文件读取
*
* @author: WangXiang
* @date: 2019/12/20 0020
**/
public class LimitInputStream extends InputStream {
private InputStream inputStream;
private BandwidthLimiter bandwidthLimiter;
public LimitInputStream(InputStream inputStream, BandwidthLimiter bandwidthLimiter) {
this.inputStream = inputStream;
this.bandwidthLimiter = bandwidthLimiter;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (bandwidthLimiter != null) {
bandwidthLimiter.limitNextBytes(len);
}
return inputStream.read(b, off, len);
}
@Override
public int read(byte[] b) throws IOException {
if (bandwidthLimiter != null && b.length > 0) {
bandwidthLimiter.limitNextBytes(b.length);
}
return inputStream.read(b);
}
@Override
public int read() throws IOException {
if (bandwidthLimiter != null) {
bandwidthLimiter.limitNextBytes();
}
return inputStream.read();
}
}
BandwidthLimiter.java
package com.wuchen.utils;
import com.wuchen.service.FileUploadService;
import org.apache.log4j.Logger;
/**
* @author: WangXiang
* @date: 2019/12/20 0020
**/
public class BandwidthLimiter {
private static Logger logger = Logger.getLogger(BandwidthLimiter.class);
//KB代表的字节数
private static final Long KB = 1024L;
//一个chunk的大小,单位byte。设置一个块的大小为1M
private static final Long CHUNK_LENGTH = 1024 * 1024L;
//已经发送/读取的字节数
private int bytesWillBeSentOrReceive = 0;
//上一次接收到字节流的时间戳——单位纳秒
private long lastPieceSentOrReceiveTick = System.nanoTime();
//允许的最大速率,默认为 1024KB/s
private int maxRate = 1024;
//在maxRate的速率下,通过chunk大小的字节流要多少时间(纳秒)
private long timeCostPerChunk = (1000000000L * CHUNK_LENGTH) / (this.maxRate * KB);
public BandwidthLimiter(int maxRate) {
this.setMaxRate(maxRate);
}
//动态调整最大速率
public void setMaxRate(int maxRate) {
if (maxRate < 0) {
throw new IllegalArgumentException("maxRate can not less than 0");
}
this.maxRate = maxRate;
if (maxRate == 0) {
this.timeCostPerChunk = 0;
} else {
this.timeCostPerChunk = (1000000000L * CHUNK_LENGTH) / (this.maxRate * KB);
}
}
public synchronized void limitNextBytes() {
this.limitNextBytes(1);
}
public synchronized void limitNextBytes(int len) {
this.bytesWillBeSentOrReceive += len;
while (this.bytesWillBeSentOrReceive > CHUNK_LENGTH) {
long nowTick = System.nanoTime();
long passTime = nowTick - this.lastPieceSentOrReceiveTick;
long missedTime = this.timeCostPerChunk - passTime;
if (missedTime > 0) {
try {
Thread.sleep(missedTime / 1000000, (int) (missedTime % 1000000));
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
}
}
this.bytesWillBeSentOrReceive -= CHUNK_LENGTH;
this.lastPieceSentOrReceiveTick = nowTick + (missedTime > 0 ? missedTime : 0);
}
}
}
java上传文件限速_java HttpClient 上传限速(避免宽带占用过高)相关推荐
- java 获取ftp 文件路径_java在浏览器上获取FTP读文件路径
展开全部 问一下,你是62616964757a686964616fe4b893e5b19e31333337623437想做ftp上传下载么? 首先你需要安装一个ftp服务端程序,启动起来,然后下载一个 ...
- linux非root上传文件,root账号无法上传文件到Linux服务器
普通权限的账号,通过ftp工具,可以正常连上Linux服务器,可以正常上传文件.但是root账号却无法上传文件. 网上搜了半天才知道,默认情况下vsftp是不允许root用户登录的,可以通过修改限制来 ...
- 上传文件按钮美化,上传文件前后状态控制
我们在做input文本上传的时候,html自带的上传按钮比较丑,如何对其进行美化呢?同理:input checkbox美化,input radio美化是一个道理的. input file上传按钮的美化 ...
- 爬虫之上传文件,request如何上传文件
爬虫之上传文件,request如何上传文件,当我们遇到需要上传文件的接口时,如何破解上传文件的密码呢? 如图,文件的参数名files[],传输多张图片,那如何用python实现呢? 1.方法1:使用r ...
- web上传文件到ftp服务器,web 上传文件到ftp服务器上
web 上传文件到ftp服务器上 内容精选 换一换 安装传输工具在本地主机和Windows云服务器上分别安装数据传输工具,将文件上传到云服务器.例如QQ.exe.在本地主机和Windows云服务器上分 ...
- 怎样用计算机给ipd传电影,怎么拷贝电影到ipad 如何将电脑上的文件拷贝到iPad上...
苹果iPad平板电脑在全球平板电脑市场中占据了半壁江山,iPad平板电脑极强的便携性.续航性以及高清性,给影音娱乐用户带来了极大的便利.不过对于没有Wifi无线网络的用户来说,想要在iPad看电影,还 ...
- 上传文件到华为云云服务器,怎样上传文件到云服务器上
怎样上传文件到云服务器上 内容精选 换一换 在本地主机和Windows弹性云服务器上分别安装QQ.exe等工具进行数据传输.使用远程桌面连接mstsc方式进行数据传输.该方式不支持断点续传,可能存在传 ...
- nginx 限制文件上传速度_nginx上传文件速度慢 Nginx上传文件全部缓存解决方案 - 硬件设备 - 服务器之家...
nginx上传文件速度慢 Nginx上传文件全部缓存解决方案 发布时间:2017-03-09 来源:服务器之家 下面通过文字说明给大家详解Nginx上传文件全部缓存解决方案. 因为应用服务器(Jett ...
- 阿里云OSS上传文件时,如何显示上传网速
阿里云OSS上传文件时,如何显示上传网速 业务场景 用户上传时,网速很慢,或者在上传大文件时,虽然有进度条,但是动的很慢,或者不明显,用户会产生困惑. 所以就产生了一个显示网速的需求点. 实现方式 E ...
最新文章
- R语言使用线性回归模型来预测(predict)单个样本的目标值(响应值、response)实战
- win10 无法打开 APICloud Studio 2 的解决方案
- python win32库与subprocess_依赖管理:Python2.7需要subprocess32
- java工程师面试如何自我介绍
- 服务器操作系统字符集,设置服务器字符集
- BAT执行DOS命令查找本地浏览器
- 一起学习设计模式--02.简单工厂模式
- 少女为什么会身上香香的?
- 剑指offer二十二之从上往下打印二叉树
- springmvc的ModelAttribute注解
- Laravel 5.1 源码阅读
- 一次Linux服务器***查杀经历
- abb和plcsocket通讯_abb与西门子plc通讯问题
- Newton 3 牛顿动力学插件 - 主体属性面板
- 艾默生手操器TREXLFPNAWS1S
- 【安全牛学习笔记】密钥交换、AIRCRACK-NG基础、AIRODUMP-NG排错
- Java 监控方案_Java 服务端监控方案
- 《Flutter技术入门与实践》——[中]亢少军
- SpringBoot集成TkMybatis
- Jmeter 环境搭建