Commit 7873393f by tyfanchz

增加文件上传接口

1 parent d6d9a20e
package com.glsx.controller;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import cn.com.glsx.openplatform.enums.ResultCodeEnum;
import cn.com.glsx.openplatform.util.FdfsUtils;
import cn.com.glsx.openplatform.vo.R;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/file")
public class FileController {
private final Logger logger = LoggerFactory.getLogger(FileController.class);
/**
* 上传文件
*
* @param file 文件
* @return 结果
*/
@RequestMapping(value = "/uploadFile", method = {RequestMethod.POST})
public R uploadFile(@RequestPart("file") MultipartFile file) {
if (file == null) {
throw ResultCodeEnum.PARAM_ERROR.exception();
}
try {
String fileUrl = FdfsUtils.uploadFile(file);
this.logger.info("$$$$ /uploadFile result: {}", fileUrl);
return R.ok().data(fileUrl);
} catch (IOException e) {
throw ResultCodeEnum.FILE_UPLOAD_ERROR.exception();
}
}
/**
* 上传多个文件
*
* @param files 文件
* @return 结果
*/
@RequestMapping(value = "/uploadMultipleFile", method = {RequestMethod.POST})
public Object uploadMultipleFile(@RequestPart("file") MultipartFile[] files) {
if (files == null || files.length == 0) {
throw ResultCodeEnum.PARAM_ERROR.exception();
}
try {
List<String> fileUrlList = new ArrayList<>();
for (MultipartFile file : files) {
fileUrlList.add(FdfsUtils.uploadFile(file));
}
this.logger.info("$$$$ /uploadMultipleFile result: {}", JSON.toJSONString(fileUrlList));
return R.ok().data(fileUrlList);
} catch (IOException e) {
throw ResultCodeEnum.FILE_UPLOAD_ERROR.exception();
}
}
/**
* 上传文件
*
* @return 结果
*/
@RequestMapping(value = "/uploadFile2", method = {RequestMethod.POST})
public Object uploadFile2() {
byte[] bytes;
try {
bytes = Files.readAllBytes(Paths.get(
"D:/Users/wbh/Desktop/glsx_car_service_sys_area.sql"));
String fileUrl = FdfsUtils.uploadFile(bytes, "sql");
System.out.println(fileUrl);
return R.ok().data(fileUrl);
} catch (IOException e) {
throw ResultCodeEnum.FILE_UPLOAD_ERROR.exception();
}
}
/**
* 删除服务器上的文件
*
* @param fileUrl 文件路径
* @return 结果
*/
@RequestMapping(value = "/deleteFile", method = {RequestMethod.POST})
public Object deleteFile(String fileUrl) {
if (StringUtils.isBlank(fileUrl)) {
throw ResultCodeEnum.PARAM_ERROR.exception();
}
try {
this.logger.info("$$$$ /deleteFile param: {}", fileUrl);
FdfsUtils.deleteFile(fileUrl);
return R.ok();
} catch (RuntimeException e) {
throw ResultCodeEnum.FILE_DELETE_ERROR.exception();
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!