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了. 打算写个系列,但是还没想好目录,今天先来一篇,后面在 ...
随机推荐
- 2017 ACM-ICPC网络赛 H.Skiing 有向图最长路
H.Skiing In this winter holiday, Bob has a plan for skiing at the mountain resort. This ski resort h ...
- 【Search In Rotated Sorted Array】cpp
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- leetcode 【 Subsets 】python 实现
题目: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset mus ...
- MongoDB快速入门学习笔记4 MongoDB的文档查询操作
先把student删除,再重新插入数据 > db.student.drop() true > db.student.insert([{ "_id" : 1, " ...
- [oldboy-django][1初始django]web框架本质 + django框架 + ajax
web框架本质 浏览器(socket客户端) - 发送请求(ip和端口,url http://www.baidu.com:80/index/) - GET 请求头(数据请求行的url上: Http1. ...
- Python面向对象之类的继承(2)
1.除了封装,Python面向对象还有继承这一功能,如下代码,这是简单的继承功能. class Animal: def chi(self): print(self.name+' 吃') def he( ...
- (转载) Linux五种IO模型
转载:http://blog.csdn.net/jay900323/article/details/18141217 Linux五种IO模型及分析 目录(?)[-] 概念理解 Linux下 ...
- 用jquery动态生成的元素绑定事件
$(document).on("click",".class a",function(){ })
- MessageBox:弹出窗口
Ext.onReady(function () { Ext.MessageBox.alert("提示信息!","Hello World!"); }); Ext, ...
- Log4j官方文档翻译(九、输出到数据库)
log4j提供了org.apache.log4j.JDBCAppender对象,可以把日志输出到特定的数据库. 常用的属性: bufferSize 设置buffer的大小,默认是1 driver 设置 ...