修改文件接口
Former-commit-id: 5e5570b3a18b688b13933a2a729e0eeae091e9d2
This commit is contained in:
parent
5b4d141d37
commit
fa3f020d6e
|
|
@ -1,42 +0,0 @@
|
|||
package com.xjy.ai.api.controller.fastdfs;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.xjy.ai.common.response.DataPacketUtil;
|
||||
import com.xjy.ai.common.response.ResponseData;
|
||||
import com.xjy.ai.common.utils.FastDFSClient;
|
||||
import com.xjy.ai.common.constant.Constants;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/fdfs")
|
||||
public class FastDFSController {
|
||||
|
||||
@Autowired
|
||||
private FastDFSClient fdfsClient;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param file
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@RequestMapping(value = "/upload", method = RequestMethod.POST)
|
||||
public ResponseData upload(MultipartFile file) throws Exception{
|
||||
|
||||
String url = fdfsClient.uploadFile(file);
|
||||
|
||||
Map<String,Object> result = new HashMap<>();
|
||||
result.put("url", url);
|
||||
result.put("ext", FilenameUtils.getExtension(file.getOriginalFilename()));
|
||||
|
||||
return DataPacketUtil.jsonResult(result, Constants.OP_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
package com.xjy.ai.api.controller.vsftp;
|
||||
|
||||
import com.xjy.ai.common.constant.Constants;
|
||||
import com.xjy.ai.common.response.DataPacketUtil;
|
||||
import com.xjy.ai.common.utils.FtpFileUtil;
|
||||
import com.xjy.ai.common.utils.IDUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
|
||||
|
||||
import com.xjy.ai.common.response.ResponseData;
|
||||
import com.xjy.ai.common.utils.FastDFSClient;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/vsftp")
|
||||
public class VSFtpController {
|
||||
|
||||
// 文件在服务器端保存的主目录
|
||||
@Value("${FTP.BASEPATHFILE}")
|
||||
private String basePath;
|
||||
// 访问图片时的基础url
|
||||
@Value("${FILE.BASE.URL}")
|
||||
private String baseUrl;
|
||||
|
||||
@Autowired
|
||||
private FastDFSClient fdfsClient;
|
||||
|
||||
@Autowired
|
||||
private FtpFileUtil ftpFileUtil;
|
||||
|
||||
/**
|
||||
* 文件上传
|
||||
* @param file
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
@RequestMapping(value = "/upload", method = RequestMethod.POST)
|
||||
public ResponseData upload(MultipartFile file) {
|
||||
|
||||
try {
|
||||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd");
|
||||
//获取图片名字
|
||||
String oldName = file.getOriginalFilename();
|
||||
//创建新的名字
|
||||
String newName = IDUtils.getImageName();
|
||||
newName = newName + oldName.substring(oldName.lastIndexOf("."));
|
||||
//生成文件在服务器端存储的子目录
|
||||
String filePath = simpleDateFormat.format(new Date());
|
||||
//把图片上传到服务器
|
||||
//转io流
|
||||
InputStream inputStream = file.getInputStream();
|
||||
|
||||
boolean result = ftpFileUtil.uploadFile(filePath, newName, basePath, inputStream);
|
||||
if (result){
|
||||
return DataPacketUtil.jsonResult(
|
||||
baseUrl + filePath + "/" + newName, Constants.UPLOAD_FILE + Constants.SUCCESS_MSG
|
||||
);
|
||||
}else {
|
||||
return DataPacketUtil.errorJsonResult(Constants.UPLOAD_FILE + Constants.FAIL_MSG);
|
||||
}
|
||||
|
||||
}catch (Exception e){
|
||||
return DataPacketUtil.errorJsonResult(Constants.UPLOAD_FILE + Constants.FAIL_MSG);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -42,9 +42,15 @@ mybatis-plus:
|
|||
mapper-locations: classpath*:mapper/**/*.xml
|
||||
type-aliases-package: com.xjy.ai.model.dao.domain
|
||||
|
||||
## fastdfs
|
||||
fdfs:
|
||||
so-timeout: 3000
|
||||
connect-timeout: 1000
|
||||
tracker-list:
|
||||
- 192.168.31.104:22122 # 此处ip替换为fastdfs服务器ip
|
||||
## vsftp
|
||||
#ftp相关配置
|
||||
FTP:
|
||||
ADDRESS: 192.168.31.104
|
||||
PORT: 21
|
||||
USERNAME: casia
|
||||
PASSWORD: casia717
|
||||
BASEPATHFILE: /file
|
||||
#文件地址
|
||||
FILE:
|
||||
BASE:
|
||||
URL: http://192.168.31.104/file
|
||||
|
|
@ -31,11 +31,10 @@
|
|||
<artifactId>poi-ooxml-schemas</artifactId>
|
||||
<version>4.0.1</version>
|
||||
</dependency>
|
||||
<!-- fastDFS-client -->
|
||||
<dependency>
|
||||
<groupId>com.github.tobato</groupId>
|
||||
<artifactId>fastdfs-client</artifactId>
|
||||
<version>1.26.4</version>
|
||||
<groupId>commons-net</groupId>
|
||||
<artifactId>commons-net</artifactId>
|
||||
<version>3.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
|
|
|
|||
|
|
@ -17,5 +17,7 @@ public class Constants {
|
|||
public static final String OP_EDIT = "编辑";
|
||||
public static final String OP_DELETE = "删除";
|
||||
|
||||
public static final String UPLOAD_FILE = "文件上传";
|
||||
|
||||
public static final String CHARSET_URF8 = "UTF-8";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,95 @@
|
|||
package com.xjy.ai.common.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.commons.net.ftp.FTP;
|
||||
import org.apache.commons.net.ftp.FTPClient;
|
||||
import org.apache.commons.net.ftp.FTPReply;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class FtpFileUtil {
|
||||
|
||||
@Value("${FTP.ADDRESS}")
|
||||
private String host;
|
||||
// 端口
|
||||
@Value("${FTP.PORT}")
|
||||
private int port;
|
||||
// ftp用户名
|
||||
@Value("${FTP.USERNAME}")
|
||||
private String userName;
|
||||
// ftp用户密码
|
||||
@Value("${FTP.PASSWORD}")
|
||||
private String passWord;
|
||||
|
||||
/**
|
||||
* Description: 向FTP服务器上传文件
|
||||
*
|
||||
*host FTP服务器ip
|
||||
*port FTP服务器端口
|
||||
*username FTP登录账号
|
||||
*password FTP登录密码
|
||||
*basePath FTP服务器基础目录,/home/ftpuser/images
|
||||
* @param filePath FTP服务器文件存放路径。例如分日期存放:/2018/05/28。文件的路径为basePath+filePath
|
||||
* @param filename 上传到FTP服务器上的文件名
|
||||
* @param input 输入流
|
||||
* @return 成功返回true,否则返回false
|
||||
*/
|
||||
public boolean uploadFile(String filePath, String filename,String basePath, InputStream input) {
|
||||
boolean result = false;
|
||||
FTPClient ftp = new FTPClient();
|
||||
try {
|
||||
int reply;
|
||||
ftp.connect(host, port);// 连接FTP服务器
|
||||
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
|
||||
ftp.login(userName, passWord);// 登录
|
||||
reply = ftp.getReplyCode();
|
||||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||||
ftp.disconnect();
|
||||
return result;
|
||||
}
|
||||
//切换到上传目录
|
||||
if (!ftp.changeWorkingDirectory(basePath + filePath)) {
|
||||
//如果目录不存在创建目录
|
||||
String[] dirs = filePath.split("/");
|
||||
String tempPath = basePath;
|
||||
for (String dir : dirs) {
|
||||
if (null == dir || "".equals(dir)){
|
||||
continue;
|
||||
}
|
||||
tempPath += "/" + dir;
|
||||
if (!ftp.changeWorkingDirectory(tempPath)) {
|
||||
if (!ftp.makeDirectory(tempPath)) {
|
||||
return result;
|
||||
} else {
|
||||
ftp.changeWorkingDirectory(tempPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//设置为被动模式
|
||||
ftp.enterLocalPassiveMode();
|
||||
//设置上传文件的类型为二进制类型
|
||||
ftp.setFileType(FTP.BINARY_FILE_TYPE);
|
||||
//上传文件
|
||||
if (!ftp.storeFile(filename, input)) {
|
||||
return result;
|
||||
}
|
||||
input.close();
|
||||
ftp.logout();
|
||||
result = true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (ftp.isConnected()) {
|
||||
try {
|
||||
ftp.disconnect();
|
||||
} catch (IOException ioe) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package com.xjy.ai.common.utils;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class IDUtils {
|
||||
public static String getImageName() {
|
||||
//取当前时间的长整形值包含毫秒
|
||||
long millis = System.currentTimeMillis();
|
||||
//long millis = System.nanoTime();
|
||||
//加上三位随机数
|
||||
Random random = new Random();
|
||||
int end3 = random.nextInt(999);
|
||||
//如果不足三位前面补0
|
||||
String str = millis + String.format("%03d", end3);
|
||||
|
||||
return str;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue