新增文件接口

This commit is contained in:
Yutousama 2022-01-28 20:06:15 +08:00
parent 5fa740c58f
commit bcae613e74
3 changed files with 46 additions and 1 deletions

View File

@ -5,10 +5,13 @@ import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.utlis.AppTools;
import com.yutou.qqbot.utlis.HttpTools;
import com.yutou.qqbot.utlis.RedisTools;
import com.yutou.qqbot.utlis.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
@ -69,4 +72,16 @@ public class AppController {
}
return ret;
}
@ResponseBody
@RequestMapping("/qq/file.do")
public String sendFile(@RequestParam("image") MultipartFile file) throws Exception {
String path=AppTools.createFile("tmp",file,"tmp.png");
if(StringUtils.isEmpty(path)){
return "not file";
}
File _file=new File(path);
QQBotManager.getInstance().sendMessage(_file,583819556L,"time = "+AppTools.getToDayNowTimeToString());
return "ok";
}
}

View File

@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class QQBotApplication {
public static final String version="QQBot v.1.2.7.2";
public static final String version="QQBot v.1.2.8";
public static void main(String[] args) {
System.out.println("version = " + version);
SpringApplication.run(QQBotApplication.class, args);

View File

@ -9,6 +9,7 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.lang.annotation.Annotation;
@ -181,4 +182,33 @@ public class AppTools {
e.printStackTrace();
}
}
/**
* 保存上传的文件
*
* @param path 路径
* @param file 文件
* @return
* @throws Exception
*/
public static String createFile(String path, MultipartFile file, String newFileName) throws Exception {
String savePath = new File("").getAbsolutePath() + File.separator + "html" + File.separator + path;
File servicePath = new File(savePath);
if (!servicePath.exists()) {
servicePath.mkdirs();
}
String fileName = file.getOriginalFilename();
if (newFileName != null) {
fileName = newFileName;
}
File saveFile = new File(savePath + "/" + fileName);
if (saveFile.exists()) {
if (!saveFile.delete()) {
saveFile = new File(savePath + "/" + fileName.replace(".", "_" + new Date().getTime() + "."));
}
}
file.transferTo(saveFile);
fileName = URLEncoder.encode(fileName, "UTF-8");
System.out.println("上传文件保存路径:" + saveFile.getAbsolutePath());
return path + fileName;
}
}