Commit 215eece7 by jscat

nyx jaapp: 功能新增

1. 添加图片上传接口
parent c854d6f0
package cn.com.fun.nyxkey.api.utils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.springframework.web.multipart.MultipartFile;
public class UploadFile {
/**
*
* 多张/单张都可以用这个
* 保存图片
*
* refer: https://blog.csdn.net/zhangleiyes123/article/details/82792010
*
* @param List<MultipartFile>
* 要批量上传的文件
* @param path
* 图片保存的路径
* @return "WRONG_FILE_EXTENSION"-错误的后缀, "FILE_EMPTY"-空文件 或者 保存后的绝对路径
* @throws IOException
*/
public static List<String> uploadFiles(List<MultipartFile> files, String path, String subpath) throws IOException {
List<String> msgs = new ArrayList<>();
if (files.size() < 1) {
return msgs;
}
for (int i = 0; i < files.size(); ++i) {
MultipartFile file = files.get(i);
if (!file.isEmpty()) {
String filename = file.getOriginalFilename();
String type = filename.indexOf(".") != -1
? filename.substring(filename.lastIndexOf("."), filename.length())
: null;
if (type == null) {
// msgs.add("file_empty");
// return msgs;
continue;
}
if (!(".PNG".equals(type.toUpperCase()) || ".JPG".equals(type.toUpperCase()))) {
// msgs.add("wrong_file_extension");
// return msgs;
continue;
}
}
}
for (int i = 0; i < files.size(); ++i) {
MultipartFile file = files.get(i);
String filename = file.getOriginalFilename();
String type = filename.indexOf(".") != -1 ? filename.substring(filename.lastIndexOf("."), filename.length())
: null;
String newname = subpath + UUID.randomUUID() + type;
String filepath = path + newname;
File filesPath = new File(path + subpath);
if (!filesPath.exists()) {
filesPath.mkdir();
}
BufferedOutputStream out = null;
type = filepath.indexOf(".") != -1 ? filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length())
: null;
try {
out = new BufferedOutputStream(new FileOutputStream(new File(filepath)));
out.write(file.getBytes());
msgs.add(newname);
} catch (Exception e) {
// 没有上传成功
e.printStackTrace();
} finally {
out.flush();
out.close();
}
}
return msgs;
}
}
\ No newline at end of file
package cn.com.fun.nyxkey.api.web.controller;
import cn.com.fun.nyxkey.api.common.JSONResult;
import cn.com.fun.nyxkey.api.utils.UploadFile;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static cn.com.fun.nyxkey.api.common.ExceptionMsg.FileEmpty;
/**
* Created by jscat on 2020-11-21.
*/
@RestController
@RequestMapping("/api")
public class FileController {
//上传文件的路径
@Value("${upload.image}")
public String imagePath;
private static final Logger LOGGER = LoggerFactory.getLogger(PayApiController.class);
/**
* @Description 文件上传
*
* 流程:
* 1) 图片一张check
* 2) 通过的直接上传
* 3) 返回
*
* postman测试
* single: https://blog.csdn.net/maowendi/article/details/80537304/
* multi : https://blog.csdn.net/weixin_45739720/article/details/103724064
* 1. POST
* 2. Headers: Key:Content-Type/Value:multipart/form-data
* 3. body:
* single: Key: file/选择文件file/Value:mid_123.jpg
* multi : Key:files/选择文件file/Value:mid_123.jpg
* Key:files/选择文件file/Value:微信截图_20201018200554.png
*
* @param
* @return Map
*/
@ApiOperation(value="单张图片上传", notes="单张图片上传")
@ApiImplicitParams({
})
@RequestMapping(value = "/nyx/upload/pic", method = RequestMethod.POST)
@ResponseBody
public JSONResult uploadPic(
@RequestParam(value = "file", required = false, defaultValue = "0") MultipartFile multipartFile
) throws Exception {
List<MultipartFile> files = new ArrayList<MultipartFile>();
files.add(multipartFile);
Date date=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String subpath = sdf.format(date) + "/";
List<String> paths = UploadFile.uploadFiles(files, imagePath, subpath);
LOGGER.info("=============/api/nyx/upload/pic:{}");
int totalCount = paths.size();
if(totalCount>=1)
{
return new JSONResult(totalCount, paths);
}
else
{
return new JSONResult(FileEmpty);
}
}
@ApiOperation(value="多张图片上传", notes="多张图片上传")
@ApiImplicitParams({
})
@RequestMapping(value = "/nyx/upload/pics", method = RequestMethod.POST)
@ResponseBody
public JSONResult uploadPics(
@RequestParam(value = "files", required = false, defaultValue = "0") List<MultipartFile> files
) throws Exception {
Date date=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String subpath = sdf.format(date) + "/";
List<String> paths = UploadFile.uploadFiles(files, imagePath, subpath);
LOGGER.info("=============/api/nyx/upload/pics:{}");
int totalCount = paths.size();
if(totalCount>=1)
{
return new JSONResult(totalCount, paths);
}
else
{
return new JSONResult(FileEmpty);
}
}
}
......@@ -78,4 +78,7 @@ aliyun:
dirLogo: logo-dir/
weixin:
notifyUrl: https://wx.hisuhong.com/api/nyx/wx/pay/notify
\ No newline at end of file
notifyUrl: https://wx.hisuhong.com/api/nyx/wx/pay/notify
upload:
image: e:\dev\pic\
\ No newline at end of file
......@@ -62,4 +62,7 @@ aliyun:
dirLogo: logo-dir/
weixin:
notifyUrl: https://fun.hisuhong.com/api/nyx/wx/pay/notify
\ No newline at end of file
notifyUrl: https://fun.hisuhong.com/api/nyx/wx/pay/notify
upload:
image: e:\dev\pic\
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论