spring post 图片

@RequestMapping(value = "/post",method = RequestMethod.POST)
    @ResponseBody
     String GPost(@RequestParam("img1") MultipartFile[] img1,@RequestParam("img2") MultipartFile[] img2) throws IOException {
    	
    	byte[] bytes1 = img1[0].getBytes(); 
    	byte[] bytes2 = img2[0].getBytes(); 
    	
    	 HttpEntity reqEntity = MultipartEntityBuilder.create()
         		.addPart("img1", new ByteArrayBody(bytes1, "img1"))
         		.addPart("img2", new ByteArrayBody(bytes2, "img2"))
         		.build();
    	
    	 String result="0";
    	 CloseableHttpClient httpclient = HttpClients.createDefault();
         try {
             HttpPost httppost = new HttpPost("http://1.6.1.192:806/F/Comp");

             httppost.setEntity(reqEntity);

             System.out.println("executing request " + httppost.getRequestLine());
             CloseableHttpResponse response = httpclient.execute(httppost);
             
             int code = response.getStatusLine().getStatusCode(); // 这个取HTTP状态码。
             if (code == 302) {
            	 result="302";
             }
             
             
             try {
                 System.out.println("----------------------------------------");
                 System.out.println(response.getStatusLine());
                 HttpEntity resEntity = response.getEntity();
                 if (resEntity != null) {
                     System.out.println("Response content length: " + resEntity.getContentLength());
                     result = EntityUtils.toString(resEntity);
                 }
                 EntityUtils.consume(resEntity);
                
             } finally {
                 response.close();
             }
         } finally {
             httpclient.close();
         }
         
        return result; 
    	
    	
    	
    	
    	
     }
}

  

原文地址:https://www.cnblogs.com/ahuo/p/7506086.html