@SneakyThrows
@ApiOperation(value = "上传文件")
@RecordLog(actionType = OPERATE_TYPE_2, descrption = "上传文件")
@AccessLimit(needLogin = false)
@RequestMapping("/api/Files")
public ResultVo apiFiles (MultipartFile file) {
String originalFilename = file.getOriginalFilename();
String extension = "." + FilenameUtils.getExtension(originalFilename);
String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())
+ UUID.randomUUID().toString().substring(0, 4)
+ extension;
String path = "";
if (JudgeSystem.JudgeSystem()){
path = "/usr/local/src/wj/mp/";
}else{
path = ResourceUtils.getURL("classpath:").getPath() + "static/files/";
}
String dataDir = path;
System.out.println(dataDir);
File dataFile = new File(dataDir);
if (!dataFile.exists()) {
dataFile.mkdirs();
}
file.transferTo(new File(dataFile, newFileName));
return ResultVo.success("https://lljt.agedlife.cn/mp/"+newFileName);
}
package com.demo.utils;
import org.junit.Test;
public class JudgeSystem {
public static boolean isLinux() {
return System.getProperty("os.name").toLowerCase().contains("linux");
}
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}
public static Boolean JudgeSystem() {
if (isLinux()) {
return true;
} else if (isWindows()) {
return false;
}
return true;
}
@Test
public void fun() {
boolean flag1 = isLinux();
boolean flag2 = isWindows();
System.out.println(JudgeSystem());
}
}
使用java执行luinx命令
package org.ml.deployer.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class CommandUtil {
public static String run(String command) throws IOException {
Scanner input = null;
String result = "";
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
try {
process.waitFor(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
InputStream is = process.getInputStream();
input = new Scanner(is);
while (input.hasNextLine()) {
result += input.nextLine() + "\n";
}
result = command + "\n" + result;
} finally {
if (input != null) {
input.close();
}
if (process != null) {
process.destroy();
}
}
return result;
}
public static String run(String[] command) throws IOException {
Scanner input = null;
String result = "";
Process process = null;
try {
process = Runtime.getRuntime().exec(command);
try {
process.waitFor(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
InputStream is = process.getInputStream();
input = new Scanner(is);
while (input.hasNextLine()) {
result += input.nextLine() + "\n";
}
result = command + "\n" + result;
} finally {
if (input != null) {
input.close();
}
if (process != null) {
process.destroy();
}
}
return result;
}
}