Feign调用文件下载服务接口实例
服务提供者:
@PostMapping(value = “/downLoadFile”)
public void downloadFile(@RequestParam String path, HttpServletResponse response) {
File file = new File(path);
FileUtil.fileDownload(response, file, false);
}
FileUtil工具类
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
public class FileUtil {
private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
public static void fileDownload(HttpServletResponse response, File file, Boolean isDeleteOriginal) {
try {
InputStream inputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
response.setContentType(“multipart.form-data”)
response.setHeader(“Content-Disposition”, “attachment; filename=” + URLEncoder.encode(file.getName(), “UTF-8”));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
int length = 0;
byte[] temp = new byte[1024 * 10];
while ((length = bufferedInputStream.read(temp)) != -1) {
bufferedOutputStream.write(temp, 0, length);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
bufferedInputStream.close();
inputStream.close();
if (isDeleteOriginal) {
file.delete();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
logger.error(e.getMessage());
}
}
}
Feign客户端(服务消费者)的代码
import feign.Response;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = “smis-mbl”)
public interface UserFeignClient extends FeignClientParent {
@GetMapping(value = “/update/downLoadFile”,consumes = MediaType.APPLICATION_PROBLEM_JSON_VALUE)
Response downloadFile(@RequestParam String path);
}
Feign客户端(服务消费者)的Controller接口方法
@GetMapping(value = “/downLoadFile”)
public void downloadFile(@RequestParam String path, HttpServletResponse response) {
InputStream inputStream = null;
try {
Response serviceResponse = this.userFeignClient.downloadFile(path);
Response.Body body = serviceResponse.body();
inputStream = body.asInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
response.setContentType(“multipart.form-data”)
response.setHeader(“Content-Disposition”, serviceResponse.headers().get(“Content-Disposition”).toString().replace("[","").replace("]",""));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
int length = 0;
byte[] temp = new byte[1024 * 10];
while ((length = bufferedInputStream.read(temp)) != -1) {
bufferedOutputStream.write(temp, 0, length);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
bufferedInputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
这一段代码是为了保持服务提供者一致的文件下载输出,其中就包括文件名!
serviceResponse.headers().get(“Content-Disposition”).toString().replace("[","").replace("]","")
1
原理:就是将服务提供者的文件下载响应的响应体(文件内容)复制到服务消费者对外的文件下载响应体中
————————————————
版权声明:本文为CSDN博主「fate急速出击」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43627706/article/details/109256074