阿里云上传下载文件(execl)
//此处参数设置在propertis内
@Value("${ftp.host}")
private String ftpHost;
@Value("${ftp.port}")
private int ftpPort;
@Value("${ftp.user}")
private String ftpUser;
@Value("${ftp.pwd}")
private String ftpPwd;
@Value("${ftp.excelpath}")
private String excelPath;
@RequestMapping(value = "export", method = RequestMethod.POST)
@ResponseBody
public String export(SysLog syslog, HttpServletRequest request) throws MalformedURLException {
List<SysLog> list = null;
String path = null;
ClassLoader classLoader = getClass().getClassLoader();
URL url = classLoader.getResource("template/sysExcel/log.xlsx");//日志路径
String tempFilePath = url.getFile();
String tempfolder = System.getProperty("java.io.tmpdir");//获取系统临时目录
String tempName = DateUtils.getCurrentDate() + "_" +"log.xlsx";//日志名称
try {
list = logService.getExportLogInfoList(syslog);
OutputStream os = new FileOutputStream(tempfolder + tempName);
if (list != null) {
List<Map<Integer, Object>> datalist = new ArrayList<Map<Integer, Object>>();
Map<Integer, Object> data;
Map<?, ?> descMap=null;
ExcelUtils excel = new ExcelUtils();
for (SysLog item : list) {
data = new HashMap<Integer, Object>();
if(item.getLogDesc()!=null){
descMap = JsonUtils.getMap(item.getLogDesc());
}
if (descMap != null) {
if(descMap.get("desc")!=null)
item.setLogDesc(descMap.get("desc").toString());
if(descMap.get("ip")!=null)
item.setOperatorIp(descMap.get("ip").toString());
}
if(item.getLogId()!=null)
data.put(1, item.getLogId());
else
data.put(1, "");
if(item.getModuName()!=null && !StringUtils.isEmpty(item.getModuName()))
data.put(2, item.getModuName());
else
data.put(2, "");
if(item.getLogDesc()!=null && !StringUtils.isEmpty(item.getLogDesc()))
data.put(3, item.getLogDesc());
else
data.put(3, "");
if(item.getOperatorName()!=null && !StringUtils.isEmpty(item.getOperatorName()))
data.put(4, item.getOperatorName());
else
data.put(4, "");
if(item.getOperatorTime()!=null)
data.put(5, DateUtils.getStringDate(item.getOperatorTime(),
"yyyy-mm-dd hh:mm:ss"));
else
data.put(5, "");
if(item.getOperatorIp()!=null && !StringUtils.isEmpty(item.getOperatorIp()))
data.put(6, item.getOperatorIp());
else
data.put(6, "");
datalist.add(data);
}
// 写EXEL
if (excel.writeDateList(tempFilePath, new String[] { "A1",
"B1", "C1", "D1", "E1", "F1" }, datalist, 0)) {
excel.writeAndClose(tempFilePath, os);
os.flush();
os.close();
File uploadFile = new File(tempFilePath);
// String remotePath = "aliyFlie";
String suffix = uploadFile.getName().indexOf(".") != -1 ? uploadFile
.getName().substring(
uploadFile.getName().lastIndexOf("."),
uploadFile.getName().length()) : null;
String fileName = StringCommUtils.getUUID() + suffix;
//----------ftp上传start------------------
// if (FtpUtils.uploadFile(ftpHost, ftpPort, ftpUser, ftpPwd,
// excelPath, fileName, new FileInputStream(tempfolder
// + tempName))) {
//----------ftp上传end------------------
//------------------阿里云所需参数 start----------------//
//存储路径
String savePath = "sysLog";
//上传文件
InputStream file = new FileInputStream(tempfolder+ tempName);
//文件名称
String logName = tempName;
//上次阿里云相关 讲execl转换成输入字节流
String logPath = OSSManageUtil.uploadFile(file, savePath, logName);
//------------------阿里云所需参数 ----------------//
if( logPath != null){
//上传成功
short type = 2;
// 写文件表
List<SysFile> fileList = new ArrayList<SysFile>();
SysFile sysFile = new SysFile();
sysFile.setFileId(fileService.getNewFileID()+1);
sysFile.setFileName(tempName);
sysFile.setFileType(type);