首页 技术 正文
技术 2022年11月14日
0 收藏 829 点赞 4,309 浏览 2431 个字

调用接收端

    @ApiOperation(value = "文件请求展示方法")
@RequestMapping(value = "/showFile", method = RequestMethod.GET)
public void showFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
//获取接口url
String url = "http://127.0.0.1/a/b";
//然后根据表名获取公司信息
HttpPost httppost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("comName", "111111"));
HttpResponse httpResponse = null;
HttpEntity httpEntity = null;
try {
httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(30000).setConnectionRequestTimeout(30000)
.setSocketTimeout(30000).build();
httppost.setConfig(requestConfig);
HttpClient httpclient = HttpClients.custom().setRetryHandler(new DefaultHttpRequestRetryHandler()).build();
httpResponse = httpclient.execute(httppost);
}catch (Exception e1) {
logger.error("----------------失败");
}
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 请求正常
try {
httpEntity = httpResponse.getEntity();
BufferedInputStream br = new BufferedInputStream(httpEntity.getContent());
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
response.setContentType("application/pdf");
String fileName ="report.pdf";
try {
fileName = httpResponse.getAllHeaders()[5].getValue().split(";")[1].split("=")[1];
} catch (Exception e) {
} response.setHeader("Content-Disposition",
"inline; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8")); OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) != -1)
out.write(buf, 0, len);
br.close();
out.flush(); } catch (Exception e) {
logger.error("解析失败");
}
}else {
logger.error("调用失败");
} }

文件存储方

   @ApiOperation(value = "文件支持接口")
@RequestMapping(value = "/getReport", method = RequestMethod.GET)
public Object getReport(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) {
Map<String, Object> params = WebUtil.getParameterMap(request);
String comName = (String) params.get("comName");
File file = new File("C:\\Users\\Administrator\\Desktop\\滴滴电子发票.pdf"); try {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
"inline; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} try {
ServletOutputStream pw = response.getOutputStream();
BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
byte[] by = new byte[(int) file.length()];
while(br.read(by)!=-1){
pw.write(by);
}
pw.flush();
} catch (IOException e1) {
e1.printStackTrace();
} return null;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297