chenweida 7 роки тому
батько
коміт
97ca272600

+ 73 - 0
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/web/third/image/ImageController.java

@ -0,0 +1,73 @@
package com.yihu.wlyy.web.third.image;
import com.yihu.wlyy.util.HttpClientUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by chenweida on 2018/5/10 0010.
 * 这个控制器是为了前端去引入别人服务器的图片放盗取连接的
 */
@Controller
@RequestMapping("image")
public class ImageController {
    @GetMapping(value = "getRemoteByURL")
    public void getImages(
            @ApiParam(name = "imageURL", value = "图片路径", defaultValue = "350200") @RequestParam(value = "imageURL", required = true) String imageURL,
            HttpServletResponse httpServletResponse
    ) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // 创建httpget.
            HttpGet httpget = new HttpGet(imageURL);
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);
            byte[] bs = new byte[1024];
            //定义每次读取的长度
            int len = -1;
            httpServletResponse.setHeader("content-type", "image/webp");
            httpServletResponse.getOutputStream();
            //循环读取数据,如果读取的数据为-1,说明已经读取了末尾
            InputStream inputStream = response.getEntity().getContent();
            int ch;
            while ((ch = inputStream.read()) != -1) {
                httpServletResponse.getOutputStream().write(ch);
            }
            httpServletResponse.getOutputStream().flush();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}