bytearray和file的后端上传方式
2016-04-14 15:00
344 查看
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; }
相关文章推荐
- mysql 函数 GROUP_CONCAT 单元格中最长字符串和excel导出问题
- Android 输入校验库简介及使用教程 - Next-Inputs
- java获取当前时间转换字符
- Qt keypressEvent中快捷键 组合键响应
- leetCode 25. Reverse Nodes in k-Group
- 组播(多播)的C程序实战
- Eclipse安装Tomcat
- Android 编程下的代码混淆
- [疯狂Java]JDBC:CallableStatement执行存储过程
- 关于xcode7下的ios模拟器输入内容无法显示系统键盘的解决办法
- 【0-1问题】 金矿模型 java实现
- 面试内容大纲
- tnt_esri.dat Arcgis8.1安装license
- bzoj 4455: [Zjoi2016]小星星
- Html 实现浮动窗口
- 单例与序列化的那些事儿
- Two implemations of the store procedure
- Oracle正规表达式替换函数
- Xcode中工程的发布版本设置
- Xcode编译Undefined symbols for architecture xxx 错误总结