controller下载文件(中转文件下载)

controller下载文件(中转文件下载)

通过contrller中转 下载指定url路径的文件

FileController.java

@Controller
@RequestMapping("/file")
@Api(tags = "文件")
public class FileController {
          
   



    @Autowired
    ILinkappSaveFileService linkappSaveFileService;


    @GetMapping("/download")
    @ApiOperation("下载")
    public InputStream downloadFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
          
   
        return linkappSaveFileService.downloadFile(response);
    }
}

ILinkappSaveFileService.java

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

public interface ILinkappSaveFileService {
          
   


    InputStream downloadFile(HttpServletResponse response) throws IOException;
}

LinkappSaveFileService.java

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;


@Component
@Slf4j
public class LinkappSaveFileService implements ILinkappSaveFileService {
          
   


    @Override
    public InputStream downloadFile(HttpServletResponse response) {
          
   
        byte[] bytes = new byte[0];
        try {
          
   
            bytes = getInputStreamByUrl("https://img.alicdn.com/imgextra/i2/734400460/O1CN01ifcE7W1FGirl9nP2K_!!734400460.jpg_60x60q90.jpg");
        } catch (IOException e) {
          
   
        }
//        byte[] buffer = new byte[inputStream.available()];
//        inputStream.read(buffer);
//        inputStream.close();
//        response.reset();
        // 设置response的Header //ISO-8859-1可以显示中文的文件名
        //获取要下载的文件输入流
        OutputStream out=null;
        try {
          
   
            response.setContentType("application/force-download;charset=UTF-8");
            response.setHeader("Content-disposition", "attachment; filename=" + "1.jpg");
//            int len = 0;
//            //创建数据缓冲区
//            byte[] buffer = new byte[1024];
//            //通过response对象获取outputStream流
            out = response.getOutputStream();
            out.write(bytes,0,bytes.length);
//            //将FileInputStream流写入到buffer缓冲区
//            while((len = in.read(buffer)) > 0) {
          
   
//                //使用OutputStream将缓冲区的数据输出到浏览器
//                out.write(buffer,0,len);
//            }
        }catch (Exception e){
          
   

        }finally {
          
   
            if (out != null) {
          
   
                try {
          
   
                    out.close();
                } catch (IOException e) {
          
   
                    e.printStackTrace();
                }
            }
        }


        return null;
    }

    /**
     * 根据地址获得数据的输入流
     * @param strUrl 网络连接地址
     * @return url的输入流
     */
    public static byte[] getInputStreamByUrl(String strUrl) throws IOException {
          
   
        URL url = new URL(strUrl);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //设置超时间为3秒
        conn.setConnectTimeout(3*1000);
        //防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

        //得到输入流
        InputStream inputStream = conn.getInputStream();
        //获取自己数组
        byte[] getData = readInputStream(inputStream);
        return getData;
    }
    /**
     * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
          
   
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
          
   
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }


}
经验分享 程序员 微信小程序 职场和发展