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的后端上传方式的更多相关文章

  1. Spring Boot + Vue 前后端分离,两种文件上传方式总结

    在Vue.js 中,如果网络请求使用 axios ,并且使用了 ElementUI 库,那么一般来说,文件上传有两种不同的实现方案: 通过 Ajax 实现文件上传 通过 ElementUI 里边的 U ...

  2. Django文件上传(经典上传方式)

    经典文件上传方式 创建URL from django.contrib import admin from django.urls import path from django.conf.urls i ...

  3. upload三种上传方式(上)---Servlet---post---commons-fileupload.1.2.1.jar方式请求上传文件

    上传前进行的配置选项: 1.在下方的Servers中,右键你的tomcat--open,选中下面两个配置. 第一个:Serve modules without publishing 作用:tomcat ...

  4. Web的几种上传方式总结

    问题 文件上传在WEB开发中应用很广泛. 文件上传是指将本地图片.视频.音频等文件上传到服务器上,可以供其他用户浏览或下载的过程. 以下总结了常见的文件(图片)上传的方式和要点处理. 表单上传 这是传 ...

  5. input type=file实现图片上传,预览以及图片删除

    背景 前两天在做一个PC网站的意见反馈,其中涉及到了图片上传功能,要求可以上传多张图片,并且支持图片上传预览及图片删除, 图片上传这一块以前没怎么搞过,而且一般也很少会碰到这样的需求,所以在做这个功能 ...

  6. django之创建第10个项目-图片上传方式1

    1.upload.HTMl <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang=& ...

  7. Springmvc file多附件上传 显示 删除操作

    之前项目需求要做一个多附件上传 并显示上传文件 带删除操作 一筹莫展之际搜到某个兄弟发的博客感觉非常好用被我copy下来了此贴算是改良版 再次感谢(忘记叫什么了时间也有点久没有历史记录了)先上图 基于 ...

  8. 图片base64格式转为file文件类型上传方法

    日常使用文件上传方式,都是通过input type='file'的文件选择框进行文件上传.但是会通过其他交互方式等到图片的base64格式进行上传.具体情况如下示意: 在项目开发中,需要进行照片采集, ...

  9. [转载]ASP.NET Core文件上传与下载(多种上传方式)

    ASP.NET Core文件上传与下载(多种上传方式)   前言 前段时间项目上线,实在太忙,最近终于开始可以研究研究ASP.NET Core了. 打算写个系列,但是还没想好目录,今天先来一篇,后面在 ...

随机推荐

  1. 动态规划:HDU1176-免费馅饼

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  2. python os模块进程函数

    Table of Contents 1. 系统进程 2. 举例 2.1. os.fork 2.2. os.exec 和 os.system 2.3. os.wait 3. 总结 系统进程 今天在看&l ...

  3. Diycode开源项目 搭建可以具有下拉刷新和上拉加载的Fragment

    1.效果预览 1.1.这个首页就是一个Fragment碎片,本文讲述的就是这个碎片的搭建方式. 下拉会有一个旋转的刷新圈,上拉会刷新数据. 1.2.整体结构 首先底层的是BaseFragment 然后 ...

  4. 给B公司的一些建议(又一篇烂尾的文章)

    感慨:太多太多的悲伤故事,发生在自己身上,发生在自己的身边.因此,为了避免总是走"弯路",走"错误"的道路,最近一直在完善自己的理论模型. 烂尾说明:本文是一篇 ...

  5. 等比例适配所有屏幕---css3 rem用法

    1,rem的定义 rem(font size of the root element)是指相对于根元素的字体大小的单位.rem是一个相对单位.和em非常相似.em(font size of the e ...

  6. 谋哥:App开发者的苦逼不值得怜悯!

    [谋哥每天一干货,第四十篇]        为什么取这个标题呢?因为昨天一些本来“支持”谋哥的人看到谋哥搞收费VIP群,觉得谋哥赚苦逼开发者的钱很不道德,且说谋哥我写的东西都不切实际,全部是一些思想性 ...

  7. Python+Selenium练习篇之2-利用ID定位元素

    在前面一篇文章,我们介绍了如何摘取页面字段,通过正则进行匹配符合要求的字段.如果感觉有点困难,不能立马理解,没有关系.把字符串摘取放到第一篇,是因为自动化测试脚本,经常要利用字符串操作,字符串切割,查 ...

  8. [oldboy-django][2深入django]FBV + CBV + 装饰器

    FBV django CBV & FBV - FBV function basic view a. urls 设置 urls(r'^test.html$', views.test) b. vi ...

  9. java 课堂笔记

  10. java io 流 输入输出 大牛经典总结

    在软件开发中,数据流和数据库操作占据了一个很重要的位置,所以,熟悉操作数据流和数据库,对于每一个开发者来说都是很重要的,今天就来总结一下I/O,数据库操作 一:从数据流开始 首先先有一个结构图看一下整 ...