freemarker通过ftl生成模板并使用ZipOutputStream压缩直接下载
代码参考如下:
@Service
public class SdkMockServiceImpl implements SdkMockService {
private static final Logger log = LoggerFactory.getLogger(SdkMockServiceImpl.class);
@Autowired
private IDeviceApi iDeviceApi;
@Autowired
private IProductApi iProductApi;
@Value("${iot.sdkTemplates.templates-path:C:\\Users\\changzheng\\Desktop\\test\\}")
private String templatesPath;
@Value("${emq.mqtt.ip:}")
private String mqttIp;
@Value("${emq.mqtt.port}")
private String mqttPort;
@Override
public ResultMap<AResultCode, String> sdkMockdeviceDownload(HttpServletResponse response, String device_id) {
ResultMap<AResultCode,String> result = new ResultMap<>();
if (StringUtil.isNull(device_id)) {
return result.setCode(AResultCode.MISS_ID);
}
ResultMap<AResultCode, DeviceMockBean> DeviceMockBeanResultMap = iDeviceApi.getBasicInfoByMockDevice(device_id);
if (AResultCode.isFailed(DeviceMockBeanResultMap)) {
return result.setCode(DeviceMockBeanResultMap.getCode());
}
DeviceMockBean deviceMockBean = DeviceMockBeanResultMap.getResult();
if (StringUtil.isNull(deviceMockBean)) {
return result.setCode(AResultCode.IS_NULL);
}
ResultMap<AResultCode, List<MockPropertyBean>> MockPropertyBeanResultMap = iProductApi.getPublishedDefaultBlockFunctionPropertyByProductKey(deviceMockBean.getProduct_key());
if (AResultCode.isFailed(MockPropertyBeanResultMap)) {
return result.setCode(MockPropertyBeanResultMap.getCode());
}
List<MockPropertyBean> MockPropertyBeanList = MockPropertyBeanResultMap.getResult();
SdkMockDeviceBean sdkMockDeviceBean = new SdkMockDeviceBean();
List<String> propertyList = new ArrayList<String>();
for(int i=0;i<MockPropertyBeanList.size();i++){
String property_id = MockPropertyBeanList.get(i).getPid();
propertyList.add(property_id);
}
sdkMockDeviceBean.setDeviceName(deviceMockBean.getDevice_name());
sdkMockDeviceBean.setDeviceSecret(deviceMockBean.getDevice_secret());
sdkMockDeviceBean.setProductKey(deviceMockBean.getProduct_key());
sdkMockDeviceBean.setPropertyList(propertyList);
FreemarkerUtil freemarkerUtil = new FreemarkerUtil();
Map<String,Object> deviceConfMap = new HashMap<>();
deviceConfMap.put("sdkMockDeviceBean",sdkMockDeviceBean);
try {
freemarkerUtil.generateFile(templatesPath,"DeviceConf.ftl",templatesPath+"talkweb_iot_mockDevice","DeviceConf.json",deviceConfMap);
} catch (IOException e) {
log.error("SdkMockServiceImpl类生成DeviceConf.json文件发生IOException");
e.printStackTrace();
} catch (TemplateException e) {
log.error("SdkMockServiceImpl类生成DeviceConf.json文件发生TemplateException");
e.printStackTrace();
}
String zipName = "talkweb_iot_mockDevice.zip";
File zipNameFile = new File(templatesPath+"talkweb_iot_mockDevice");
try {
response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(zipName, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream outputStream = null;
ZipOutputStream zipOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
outputStream = response.getOutputStream();
zipOutputStream = new ZipOutputStream(outputStream);
bufferedOutputStream = new BufferedOutputStream(zipOutputStream);
zip(zipOutputStream,zipNameFile,zipNameFile.getName(),bufferedOutputStream);
} catch (IOException e) {
log.error("SdkMockServiceImpl类生成文件输入输出流发生IOException");
e.printStackTrace();
}finally {
try {
bufferedOutputStream.close();
zipOutputStream.close();
} catch (IOException e) {
log.error("SdkMockServiceImpl类关闭文件输入输出流发生IOException");
e.printStackTrace();
}
}
result.setCode(AResultCode.OK);
return result;
}
private void zip(ZipOutputStream zipOutputStream,File targetFile,String targetName,BufferedOutputStream bufferedOutputStream){
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
if(targetFile.isDirectory()){
File[] files = targetFile.listFiles();
if(files.length == 0){
try {
zipOutputStream.putNextEntry(new ZipEntry(targetName+"/"));
} catch (IOException e) {
e.printStackTrace();
}
}
for (File file : files){
zip(zipOutputStream,file, targetName+"/"+file.getName(),bufferedOutputStream);
}
}else{
try {
zipOutputStream.putNextEntry(new ZipEntry(targetName));
fileInputStream = new FileInputStream(targetFile);
bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] bytes = new byte[1024];
int length = -1;
while((length = bufferedInputStream.read(bytes))!=-1){
bufferedOutputStream.write(bytes,0,length);
}
} catch (IOException e) {
log.error("SdkMockServiceImpl类zip压缩方法生成文件输入输出流发生IOException");
e.printStackTrace();
}
}
try {
bufferedOutputStream.flush();
if(bufferedInputStream!=null){
bufferedInputStream.close();
}
} catch (IOException e) {
log.error("SdkMockServiceImpl类zip压缩方法关闭文件输入输出流发生IOException");
e.printStackTrace();
}
}
}