Commit b0dad9e9 authored by suntao's avatar suntao

结清文件下载

parent 26b1d9cd
package cn.quantgroup.customer.rest;
import cn.quantgroup.customer.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* 文件下载
*
* @author tao
* @version 2020-09-30 17:12
*/
@Controller
@RequestMapping("/download")
public class FileRest {
@Autowired
private HttpServletRequest request;
@Autowired
private IFileService fileService;
// 文件下载相关代码
@RequestMapping("/settle_prove/{loanId}")
public String downloadFile(HttpServletResponse response, @PathVariable Long loanId) throws Exception {
BufferedInputStream bis = fileService.getBufferedInputStream(loanId);
if (bis != null) {
response.setContentType("application/force-download");// 设置强制下载不打开
//response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
//response.setContentType("multipart/form-data;charset=UTF-8");也可以明确的设置一下UTF-8,测试中不设置也可以。
response.setHeader("Content-Disposition", "attachment;fileName="+ new String(("结清证明_"+loanId).getBytes("GB2312"),"ISO-8859-1"));
byte[] buffer = new byte[1024];
FileInputStream fis = null;
try {
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("下载成功");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return null;
}
}
......@@ -96,4 +96,15 @@ public class OrderRest {
log.info("[申请结清证明],请求参数:loanId={}", loanId);
return orderService.applySettle(loanId);
}
/**
* 结清证明下载
*
* @param loanId
* @return
*/
@GetMapping("/download_settle/{loanId}")
public JsonResult downloadSettle(@PathVariable Long loanId) {
log.info("[结清证明下载],请求参数:loanId={}", loanId);
return orderService.downloadSettle(loanId);
}
}
package cn.quantgroup.customer.service;
import java.io.BufferedInputStream;
import java.net.MalformedURLException;
/**
* @author tao
* @version 2020-09-30 17:27
*/
public interface IFileService {
BufferedInputStream getBufferedInputStream(Long loanId) throws MalformedURLException, Exception;
}
......@@ -17,4 +17,6 @@ public interface IIceService {
JsonResult<Map<Long, Integer>> findSettleFlag(List<Long> loanIds);
JsonResult applySettle(Long loanId);
JsonResult downloadSettle(Long loanId);
}
......@@ -65,5 +65,17 @@ public interface IOrderService{
*/
JsonResult<List<OpLog>> queryOperateLog(Long loanId);
/**
* 申请结清证明
* @param loanId
* @return
*/
JsonResult applySettle(Long loanId);
/**
* 结清证明下载
* @param loanId
* @return
*/
JsonResult downloadSettle(Long loanId);
}
package cn.quantgroup.customer.service.impl;
import cn.quantgroup.customer.service.IFileService;
import org.springframework.stereotype.Service;
import java.io.BufferedInputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* 文件流转发
*
* @author tao
* @version 2020-09-30 17:28
*/
@Service
public class FileServiceImpl implements IFileService {
//随便写一个
private static final String boundary = java.util.UUID.randomUUID().toString();
@Override
public BufferedInputStream getBufferedInputStream(Long loanId) throws Exception {
HttpURLConnection con = (HttpURLConnection) new URL("").openConnection();
// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true, 默认情况下是false;
con.setDoOutput(true);
// 设置是否从httpUrlConnection读入,默认情况下是true;
con.setDoInput(true);
// 设定请求的方法为"POST",默认是GET
con.setRequestMethod("POST");
// Post 请求不能使用缓存
con.setUseCaches(false);
//设置接收返回值的格式
con.setRequestProperty("Accept", "text/plain, */*");
//设置接收编码
con.setRequestProperty("Accept-Language", "zh-cn");
con.setRequestProperty("Host", "127.0.0.1");
//设置请求参数格式以及boundary分割线
con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
con.setRequestProperty("User-Agent", " WinHttpClient");
//开启长连接可以持续传输
con.setRequestProperty("Connection", "Keep-Alive");
//连接超时时间20秒
con.setConnectTimeout(20000);
//读取超时时间20秒
con.setReadTimeout(20000);
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
try {
fileName = con.getHeaderField(1).substring(21, con.getHeaderField(1).length() - 1);
fileName = "EBDT" + fileName.split("_")[1];
System.out.println(fileName);
} catch (Exception e) {
return null;
}
return new BufferedInputStream(con.getInputStream());
}
return null;
}
}
......@@ -202,4 +202,9 @@ public class IceServiceImpl implements IIceService {
JsonResult<Boolean> jsonResult = JSONTools.deserialize(result, typeToken);
return jsonResult;
}
@Override
public JsonResult downloadSettle(Long loanId) {
return null;
}
}
......@@ -473,6 +473,11 @@ public class OrderServiceImpl implements IOrderService {
return iceService.applySettle(loanId);
}
@Override
public JsonResult downloadSettle(Long loanId) {
return iceService.downloadSettle(loanId);
}
/**
* 处理查询不到orderMapping的情况,可能是进件失败,也可能不存在该笔订单
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment