@RequestMapping(value = "/downword", method = RequestMethod.GET)
public void downWord(@RequestParam("attachGuid") String attachGuid, @RequestParam("businessGuid") String businessGuid, @RequestParam("methodUrl") String methodUrl, @RequestParam("methodName") String methodName, HttpServletResponse response) {
logger.info("下载附件》》》》downWord》》》》请求参数:attachGuid:" + attachGuid + ",businessGuid:" + businessGuid + ",methodUrl:" + methodUrl + ",methodName:" + methodName);
if (StringUtil.isBlank(attachGuid) || StringUtil.isBlank(businessGuid) || StringUtil.isBlank(methodUrl) || StringUtil.isBlank(methodName)) {
throw new CustomException("下载附件请求缺失参数");
}
OutputStream out = null;
InputStream fis = null;
try {
// attachGuid可以找打附件输入流==fis
fis = hrAttachInfoService.getAttachContent(attachGuid);
FrameAttachInfo detail = attachService.getAttachInfoDetail(attachGuid);
String fileName = "";
if (detail != null) {
fileName = detail.getAttachFileName();
}
if (fis != null) {
// 反射获取域信息
JSONObject exportData = new JSONObject();
Class<?> clazz = Class.forName(methodUrl);
Object object = applicationContext.getBean(clazz);
Method method = clazz.getDeclaredMethod(methodName, String.class);
exportData = (JSONObject) method.invoke(object, businessGuid);
response.setCharacterEncoding("utf-8");
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
out = response.getOutputStream();
new GaOfficeUtil().mergeWordTemplate(fis, out, exportData, new FrameAttachInfoNewService9(), SaveOptions.createSaveOptions(SaveFormat.DOC));
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (fis != null) {
fis.close();
}
if (out != null) {
out.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
java-反射实现
于 2022-12-11 15:14:10 首次发布