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; }
时间: 2024-10-06 00:36:51