在微服務(wù)項(xiàng)目中,我經(jīng)常會(huì)碰到從一個(gè)微服務(wù)項(xiàng)目轉(zhuǎn)發(fā)下載請(qǐng)求并實(shí)現(xiàn)下載文件的需求,因此在此做一個(gè)轉(zhuǎn)發(fā)下載的示例??偟南螺d轉(zhuǎn)發(fā)流程如下,我會(huì)按照這個(gè)流程一一介紹下載流程。
?
?1、客戶端的下載請(qǐng)求
這里主要介紹controller層是如何接收客戶端發(fā)送請(qǐng)求的代碼,接收參數(shù)的代碼如下
@GetMapping(value = "downloadFile")
public void downloadFile( HttpServletResponse response) {
}
2、轉(zhuǎn)發(fā)服務(wù)器的下載請(qǐng)求
轉(zhuǎn)發(fā)服務(wù)器的下載請(qǐng)求主要通過Feign接口實(shí)現(xiàn),具體實(shí)現(xiàn)代碼如下:
Feign接口:
@PostMapping(value = "/downloadFile")
Response downloadFile();
Feign接口實(shí)現(xiàn)類:
@Override
public Response downloadFile() {
return null;
}
之后便可以利用Feign實(shí)現(xiàn)轉(zhuǎn)發(fā)了controller層代碼完善如下:
@GetMapping(value = "downloadFile")
public void downloadFile( HttpServletResponse response) {
InputStream inputStream = null;
try {
Response serviceResponse = queryBranchService.downloadFile(url,sapNo);
} catch (IOException e) {
e.printStackTrace();
}
}
3、最終服務(wù)器的下載請(qǐng)求響應(yīng)
該代碼的總體思路就是將文件變成輸出流并寫在響應(yīng)中,并返回給轉(zhuǎn)發(fā)服務(wù)器。文章來源:http://www.zghlxwxcb.cn/news/detail-422120.html
@GetMapping(value = "downloadFile")
public void downloadFile(HttpServletResponse response) {
//獲取upload文件夾的路徑
try {
InputStream contentInput = OkHttpClientUtils.getImage(url, null);
BufferedInputStream bufferedInputStream = new BufferedInputStream(contentInput);
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(sapNo+".png", "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();
} catch (FileNotFoundException e) {
e.printStackTrace();
log.error(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
log.error(e.getMessage());
}
}
4、轉(zhuǎn)發(fā)服務(wù)器的下載請(qǐng)求響應(yīng)
對(duì)轉(zhuǎn)發(fā)服務(wù)器的controller層再繼續(xù)完善,完善的總思路拿到中轉(zhuǎn)服務(wù)器的響應(yīng),并拿出響應(yīng)中輸入流再變成輸出流回寫給客戶端文章來源地址http://www.zghlxwxcb.cn/news/detail-422120.html
@GetMapping(value = "downloadFile")
public void downloadFile( HttpServletResponse response) {
InputStream inputStream = null;
try {
InputStream inputStream = null;
try {
Response serviceResponse = queryBranchService.downloadFile();
Response.Body body = serviceResponse.body();
inputStream = body.asInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
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();
}
} catch (IOException e) {
e.printStackTrace();
}
}
到了這里,關(guān)于SpringBoot微服務(wù)項(xiàng)目,轉(zhuǎn)發(fā)并響應(yīng)下載請(qǐng)求的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!