import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.sun.corba.se.spi.legacy.connection.Connection;
@ApiOperation(value = “下载模版”, notes = “下载模版”)
@RequestMapping(value = “/exportExcel”, method = RequestMethod.GET)
public Object exportExcel(@RequestParam String id) throws Exception {
String path = DemoService.exportExcel(templateId);
return generateSuccessRes(path);
}
@Service
public String exportExcel(String id){
// 文件输出位置win
String localPath = "E:\\";
String outPath = "";
String system = System.getProperty("os.name");
if (system.toLowerCase().startsWith("win")) {
outPath = "E:\\";
} else {
outPath = "/usr/local/qdfgw/exceldir/";
}
TemplateDo templateDo = templateDoMapper.selectByPrimaryKey(templateId);
String templateName = templateDo.getTemplateName();
outPath = outPath + templateName + ".xlsx";
if (templateDo != null) {
LineDo lineDo = new LineDo();
lineDo.setTemplateId(templateId);
List<LineDo> lineList = lineDoMapper.select(lineDo);
List<String> strList = new ArrayList<>();
for (LineDo ld : lineList) {
// if (ld.getIsTaskLine() == 0) {
strList.add(ld.getLineChineseName());
// }
}
ExcelWriter excelWriter = null;
try {
excelWriter = EasyExcelFactory.getWriter(new FileOutputStream(outPath));
// 表单
Sheet sheet = new Sheet(1, 0);
sheet.setSheetName(“Sheet1”);
Table table = new Table(1);
List<List> headList = new ArrayList<List>();
for (String str : strList) {
List headTitle = new ArrayList();
headTitle.add(str);
headList.add(headTitle);
}
table.setHead(headList);
excelWriter.write1(null, sheet, table);
excelWriter.finish();
///
/if (!system.toLowerCase().startsWith(“win”)) {
ChannelSftp sftp = null;
Channel channel = null;
Session sshSession = null;
try {
JSch jsch = new JSch();
jsch.getSession(“root”, “10.16.3.5”, 22);
sshSession = jsch.getSession(“root”, “10.16.3.5”, 22);
sshSession.setPassword(“password”);
Properties sshConfig = new Properties();
sshConfig.put(“StrictHostKeyChecking”, “no”);
sshSession.setConfig(sshConfig);
sshSession.connect();
channel = sshSession.openChannel(“sftp”);
channel.connect();
sftp = (ChannelSftp) channel;
Vector<?> vector = sftp.ls("/usr/local/qdfgw/exceldir/");
for (Object item : vector) {
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) item;
String fileName = entry.getFilename();
if (fileName.contains(templateName)) {
File localFile = new File(localPath + fileName);
OutputStream is = new FileOutputStream(localFile);
sftp.get(fileName, is);
is.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
sftp.disconnect();
channel.disconnect();
sshSession.disconnect();
}
}/
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return outPath;
}
pom.xml:
com.jcraft jsch 0.1.49