直接上代码把,作为自己的笔记方便复习查找
一、上传文件的代码:
(这里把上传的文件保存到了服务器本地的文件目录中)
task_log_path 为路径的配置文件如下:
#任务日志的路径
task.log.path = D:/TaskLogStore
在application.properties中进行配置,在使用时用
@Value("${task.log.path}")
String task_log_path;
来进行获取使用。
这里的AllTask_Log是日志文件的数据库,由于日志文件较大,储存在数据库中的某个字段可能会有未知的问题,保险起见,直接保存在了服务器的目录文件夹下, AllTask_Log类只有两个字段 String task_id和String task_log
//任务执行日志的上传
@PutMapping("/uploadLog")
String uploadLog(@RequestBody AllTask_Log data)
{
int temp = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
FileOutputStream fos = null;
List<Task_New> currentAllTask = task_newService.getAll();
List<Task_New> result = currentAllTask.stream().filter(item->item.getId().equals(data.getTask_id())).collect(Collectors.toList());
if (result==null || result.size()==0)
{
return "{\"state\":1000,\"msg\":\"日志上传失败,task_id不存在任务列表中\"}";
}
//保存日志文件到服务端
String fileName = data.getTask_id()+"_"+sdf.format(new Date())+".log";
String logFileSavePath = task_log_path + "/"+fileName;
File taskLogFile = new File(logFileSavePath);
if (!taskLogFile.exists()){
try {
taskLogFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
byte[] bytes =new byte[1024];
bytes = data.getTask_log().getBytes();
int b = bytes.length;
try {
fos=new FileOutputStream(taskLogFile);
try {
fos.write(bytes,0,b);
fos.write(bytes);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
AllTask_Log currentLog = service.getById(data.getTask_id());
if (currentLog ==null){
data.setTask_log(fileName);
temp = service.add(data);
}else {
data.setTask_log(fileName);
temp = service.update(data);
}
if (temp>0)
{
return "{\"state\":200,\"msg\":\"日志上传成功\"}";
}else {
return "{\"state\":1000,\"msg\":\"日志上传失败\"}";
}
}
二、下载日志文件的代码:
HttpServletResponse response 作为一个网络 传输协议,在参数列表的传值里是不会出现的
//任务日志的下载
@GetMapping("/downloadLog")
void downloadLog(HttpServletResponse response,AllTask_Log data)throws IOException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String fileName = data.getTask_id()+"_"+sdf.format(new Date())+".log";
String downloadFileName =new String(fileName.getBytes("UTF-8"),"iso-8859-1");
FileInputStream inputStream = null;
OutputStream outputStream = null;
File file = null;
try {
file = new File(task_log_path+"/"+data.getTask_log());
if (!file.exists()) {
} else {
inputStream = new FileInputStream(file);
response.setContentType("application/txt;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;Filename=" + new String(downloadFileName.getBytes("GB2312"), "ISO8859-1"));// 设置下载后文件的名字、防止中文乱码
outputStream = response.getOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, len);
}
}
} catch (IOException e) {
} finally {
try {
if (inputStream != null) { inputStream.close(); }
if (outputStream != null) { outputStream.close();}
} catch (IOException e) {
}
}
// // 直接下载String信息
// OutputStream outputStream = null;
// //将字符串转化为文件
// byte[] currentLogByte = getById(task_id).getTask_log().getBytes();
// try {
// // 告诉浏览器用什么软件可以打开此文件
// response.setHeader("content-Type","application/vnd.ms-excel");
// // 下载文件的默认名称
// response.setHeader("Content-disposition","attachment;filename="+ URLEncoder.encode(downloadFileName,"utf-8"));
//
// outputStream = response.getOutputStream();
//
// outputStream.write(currentLogByte);
//
// }catch (UnsupportedEncodingException e) {
//
// e.printStackTrace();
//
// }
}
三、日志文件的前端显示获取:
//通过id得到任务日志
@GetMapping("/getTaskLogById")
String getTaskLogById(String task_id)
{
String taskLogInfo = "";
AllTask_Log currentTaskLog=getById(task_id);
String logSavePath = task_log_path+"/"+currentTaskLog.getTask_log();
try {
FileInputStream fileInputStream = new FileInputStream(logSavePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line = null;
while (true)
{
try {
if (!((line = bufferedReader.readLine())!=null)) {break;}
else {
taskLogInfo += line;
}
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
JSONObject json = new JSONObject(true);
json.put("state",200);
json.put("msg","success");
json.put("data",taskLogInfo);
return JSON.toJSONString(json, SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty);
}