下载图片

package cn.anzhongwei.lean.demo.downfileforweb;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

@RestController
@ResponseBody
public class DownImgController {

    //http://localhost:8080/ttpng?seriesUniqueCode=1
    @GetMapping(value = "/writeImg1")
    public void writeImg1(HttpServletResponse response) throws IOException {
        //设置响应头信息需要在输出流之前, 就可以触发浏览器下载机制, 注释下列3行或将其写在输出流之后, 都只会在浏览器显示不会触发下载
//        response.setHeader("Content-disposition", "attachment;filename=cargoExport.jpg");
//        response.setContentType("multipart/form-data");
//        response.setCharacterEncoding(StandardCharsets.UTF_8.name());

        URL url = null;
        InputStream is = null;
        ByteArrayOutputStream outStream = null;
        HttpURLConnection httpUrl = null;
        //随便找一张图片
        url = new URL("http://img.ay1.cc/uploads/20221114/9f3e884272b562cf8b27a048c3634621.jpg");
        httpUrl = (HttpURLConnection) url.openConnection();
        httpUrl.connect();
        httpUrl.getInputStream();
        is = httpUrl.getInputStream();
        outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度,如果为-1,代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = is.read(buffer)) != -1) {
            //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
            response.getOutputStream().write(buffer, 0, len);
        }

        response.flushBuffer();
    }


    @GetMapping(value = "/writeImg2", produces = MediaType.IMAGE_JPEG_VALUE)
    public void writeImg2(HttpServletResponse response) throws IOException {
        URL url = null;
        InputStream is = null;
        ByteArrayOutputStream outStream = null;
        HttpURLConnection httpUrl = null;

        url = new URL("http://img.ay1.cc/uploads/20221114/9f3e884272b562cf8b27a048c3634621.jpg");
        httpUrl = (HttpURLConnection) url.openConnection();
        httpUrl.connect();
        httpUrl.getInputStream();
        is = httpUrl.getInputStream();
        outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度,如果为-1,代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = is.read(buffer)) != -1) {
            //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        byte[] temp = outStream.toByteArray();

        response.setHeader("Content-disposition", "attachment;filename=cargoExport.jpg");
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.getOutputStream().write(temp);
        response.flushBuffer();
    }


    public void imgToBase64() {
        URL downUrl = new URL("http://localhost/test.png");
        HttpURLConnection conn = (HttpURLConnection) downUrl.openConnection();
        conn.setRequestMethod("GET");
        conn.setConnectTimeout(5000);
        InputStream inputStream = conn.getInputStream();

        ByteArrayOutputStream data = new ByteArrayOutputStream();
        byte[] by = new byte[1024];
        int len = -1;
        while((len = inputStream.read(by)) != -1){
            data.write(by,0,len);
        }
        inputStream.close();
        Base64.getEncoder().encode(data.toByteArray());
        String imgStr = Base64.getEncoder().encodeToString(data.toByteArray());
        System.out.println(imgStr);
    }
}