bytearray和file的后端上传方式
public static String readAndUpload(String serverpath,String imgid) {
if(serverpath==null){
serverpath = OLD_IMG_SERVER_URL;
}
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
// 创建httpget
URL url = new URL(serverpath+imgid);
URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
HttpGet httpget = new HttpGet(uri);
System.out.println("executing request " + httpget.getURI());
// 执行get请求.
response = httpclient.execute(httpget);
// 获取响应实体
HttpEntity entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity != null) {
long len = entity.getContentLength();
// 打印响应内容长度
System.out.println("image content length: " + len);
System.out.println("------------------------------------");
//准备上传图片数据
byte[] buffer = null; InputStream fis=null;
if(len>=1000000){
System.err.println(imgid+"====Scale:"+1000000.0f/len);
Thumbnails.of(new URL(serverpath+imgid)).outputFormat("jpeg").scale(1)
// .scale(1000000.0f/len)
.toFile("temp.jpg");
fis = new FileInputStream("temp.jpg");
}else{
fis = entity.getContent();
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
EntityUtils.consume(entity);
//开始上传
HttpPost httppost = new HttpPost(NEW_IMG_SERVER_URL);
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(buffer);
httppost.setEntity(byteArrayEntity);
httppost.addHeader("Content-Type", "jpeg");
System.out.println("executing request " + httppost.getRequestLine());
Header[] headers = httppost.getAllHeaders();
response = httpclient.execute(httppost);
System.out.println("----------------Response-----------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String resp = EntityUtils.toString(resEntity);
System.out.println("Response content length: " + resEntity.getContentLength());
System.out.println("Response content : " + resp);
ImgInfo o = JsonUtil.fromJsonToObject(resp, ImgInfo.class);
if(o==null || o.getInfo()==null){
return null;
}
return o.getInfo().getMd5();
}
EntityUtils.consume(resEntity);
}else{
return null;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (URISyntaxException e) {
e.printStackTrace();
return null;
} finally {
try {
if(response!=null){
response.close();
response = null;
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static String readFileAndUpload(File file) {
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
//准备上传图片数据
byte[] buffer = null; InputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
//开始上传
HttpPost httppost = new HttpPost(NEW_IMG_SERVER_URL);
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(buffer);
httppost.setEntity(byteArrayEntity);
httppost.addHeader("Content-Type", "jpeg");
System.out.println("executing request " + httppost.getRequestLine());
Header[] headers = httppost.getAllHeaders();
response = httpclient.execute(httppost);
System.out.println("----------------Response-----------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String resp = EntityUtils.toString(resEntity);
System.out.println("Response content length: " + resEntity.getContentLength());
System.out.println("Response content : " + resp);
ImgInfo o = JsonUtil.fromJsonToObject(resp, ImgInfo.class);
if(o==null || o.getInfo()==null){
return null;
}
return o.getInfo().getMd5();
}
EntityUtils.consume(resEntity);
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
try {
if(response!=null){
response.close();
response = null;
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
bytearray和file的后端上传方式的更多相关文章
- Spring Boot + Vue 前后端分离,两种文件上传方式总结
在Vue.js 中,如果网络请求使用 axios ,并且使用了 ElementUI 库,那么一般来说,文件上传有两种不同的实现方案: 通过 Ajax 实现文件上传 通过 ElementUI 里边的 U ...
- Django文件上传(经典上传方式)
经典文件上传方式 创建URL from django.contrib import admin from django.urls import path from django.conf.urls i ...
- upload三种上传方式(上)---Servlet---post---commons-fileupload.1.2.1.jar方式请求上传文件
上传前进行的配置选项: 1.在下方的Servers中,右键你的tomcat--open,选中下面两个配置. 第一个:Serve modules without publishing 作用:tomcat ...
- Web的几种上传方式总结
问题 文件上传在WEB开发中应用很广泛. 文件上传是指将本地图片.视频.音频等文件上传到服务器上,可以供其他用户浏览或下载的过程. 以下总结了常见的文件(图片)上传的方式和要点处理. 表单上传 这是传 ...
- input type=file实现图片上传,预览以及图片删除
背景 前两天在做一个PC网站的意见反馈,其中涉及到了图片上传功能,要求可以上传多张图片,并且支持图片上传预览及图片删除, 图片上传这一块以前没怎么搞过,而且一般也很少会碰到这样的需求,所以在做这个功能 ...
- django之创建第10个项目-图片上传方式1
1.upload.HTMl <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang=& ...
- Springmvc file多附件上传 显示 删除操作
之前项目需求要做一个多附件上传 并显示上传文件 带删除操作 一筹莫展之际搜到某个兄弟发的博客感觉非常好用被我copy下来了此贴算是改良版 再次感谢(忘记叫什么了时间也有点久没有历史记录了)先上图 基于 ...
- 图片base64格式转为file文件类型上传方法
日常使用文件上传方式,都是通过input type='file'的文件选择框进行文件上传.但是会通过其他交互方式等到图片的base64格式进行上传.具体情况如下示意: 在项目开发中,需要进行照片采集, ...
- [转载]ASP.NET Core文件上传与下载(多种上传方式)
ASP.NET Core文件上传与下载(多种上传方式) 前言 前段时间项目上线,实在太忙,最近终于开始可以研究研究ASP.NET Core了. 打算写个系列,但是还没想好目录,今天先来一篇,后面在 ...
随机推荐
- 牛课第二次多校I
链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 题目描述 White Cloud has a square of n*n from (1,1) ...
- 洛谷P1079 Vigenère 密码
题目链接:https://www.luogu.org/problemnew/show/P1079
- day09 threading, paramiko, queue 模块
1 模拟ssh 2 锁 内部锁,程序锁,信号量 3 多线程 4 简单消息队列 先来看模拟ssh ,python 的强大之处就是因为有很多模块,可以很简单的完成复杂的事情,今天我们用paramiko ...
- 非常全的API接口查询
http://www.apix.cn/services/category/3 https://www.showapi.com/ https://www.juhe.cn/docs http://deve ...
- leetcode 【 Best Time to Buy and Sell Stock 】python 实现
思路: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...
- IOS开发学习笔记007-数据结构
目录: 1.全局变量和局部变量 2.结构体 3.结构体数组 4.结构体做函数参数 5.结构体指针 6.枚举 7.总结 一.全局变量和局部变量 全局变量和局部变量的区别 1. 全局变量,再函数外定义的变 ...
- js 遍历对象属性(for in、Object.keys、Object.getOwnProperty) 以及高效地输出 js 数组
js中几种遍历对象的方法,包括for in.Object.keys.Object.getOwnProperty,它们在使用场景方面各有不同. for in 主要用于遍历对象的可枚举属性,包括自有属性. ...
- Leetcode 592.分数加减运算
分数加减运算 给定一个表示分数加减运算表达式的字符串,你需要返回一个字符串形式的计算结果. 这个结果应该是不可约分的分数,即最简分数. 如果最终结果是一个整数,例如 2,你需要将它转换成分数形式,其分 ...
- linux自动执行指令crontab和at
目录 1 at和crontab指令 2 batch 一.at与crontab的区别 运行方式不同 at只运行一次,crontab循环运行 依赖的服务不同 at 对应的服务是 atd crontab 对 ...
- win7分盘(复制)
1/10 右击“计算机”选择“管理” 2/10 打开管理之后点击“磁盘管理器”,在想要新建磁盘的分区上右击,点击“压缩卷” 3/10 在“输入压缩空间量”后面输入需要新建磁盘的大小,输入的单位为MB( ...