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. Diycode开源项目 BaseApplication分析+LeakCanary第三方+CrashHandler自定义异常处理

    1.BaseApplication整个应用的开始 1.1.看一下代码 /* * Copyright 2017 GcsSloop * * Licensed under the Apache Licens ...

  2. Apache shiro学习总结

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  3. 史上最全的MSSQL笔记

    http://www.cnblogs.com/gameworld/archive/2015/09/08/4790881.html

  4. 【NOIP 2017 普及组】 跳房子

    裸的单调队列优化dp+二分 我居然还调了挺久 日常审题错误 #include <bits/stdc++.h> using namespace std; typedef long long ...

  5. Python+Selenium中级篇之-二次封装Selenium中几个方法

    本文来介绍,如何把常用的几个webdriver的方法封装到自己写的一个类中去,这个封装过程叫二次封装Selenium方法.我们把打开站点,浏览器前进和后退,关闭和退出浏览器这这个方法封装到一个新写的类 ...

  6. [oldboy-django][2深入django]Form总结

    1 form总结 # Form数据格式验证 - 原理: - 流程 a.写类LoginForm(Form): 字段名 = fields.xxFields() # 验证规则,本质是正则表达式(fields ...

  7. TOJ 4689: Sawtooth

    4689: Sawtooth Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit: 26     ...

  8. Solr linux安装

    1.下载安装(自行官网下载) 2.解压安装包 3.进入解压后的bin目录执行命令: ./solr start 出现如下警告: 根据提示修改solr.in.sh中的SOLR_ULIMIT_CHECKS属 ...

  9. WINDOWS开发PHP7扩展

    最近在做个项目,需要用到唯一ID的生成,原本在Java和Delphi中,做了一个生成20位字符串(160bit)形式的唯一ID的算法,但是对比GUID(128bit),除了看起来比他短之外,其他并无优 ...

  10. 解决云服务器ECS,windows server 2012不能安装SQL Server 2012,不能安装.NET Fromework 3.5

    在云服务器上安装SQL Server 2012 时出现“启用windows功能NetFx3时出错”的问题:NetFx3指的是.NET Framework 3.5,SQL Server 2012数据库系 ...