一. 在Linux安装Ftp服务器
(1) 查看是否安装了FTP
使用命令 rpm -qa |grep vsftpd
如果没有任何输出,表示没有安装, 如果出现如下版本信息,则表示已经安装。
(2)如果没有安装,可以使用如下命令直接安装
使用命令 yum -y install vsftpd
默认安装目录:/etc/vsftpd
(3) 添加FTP账号
useradd admin -s /sbin/nologin
该账户路径默认指向/home/admin目录,也可以指定对应目录,给用户授权。
设置密码:passwd admin
(4) 其他常用操作
① 设置匿名用户可以下载上传
修改文件/etc/vsftpd/vsftpd.conf 中anon_upload_enable=YES,anon_mkdir_write_enable=YES
② 设置防火墙:iptables -F
③ 设置开机启动sftp:chkconfig vsftpd on
(5)启动与关闭
启动使用命令:service vsftpd start
关闭使用命令:service vsftpd restart
到此,基本上Ftp的配置差不多就完成啦。
验证:
浏览器访问: ftp://103.96.XX.XX,能够看到你设置的目录就OK了。
二. 引入百度编辑器
① 依赖pom
<dependency>
<groupId>com.gitee.qdbp.thirdparty</groupId>
<artifactId>ueditor</artifactId>![在这里插入图片描述](https://img-blog.csdnimg.cn/20190123094415722.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTQ2NDQ3MDk=,size_16,color_FFFFFF,t_70)
<version>1.4.3.3</version>
</dependency>
②添加Ueditor编辑器的静态资源
特殊说明,我当时的项目是动静分离模式开发,将编辑器的资源放在Static项目里面,编辑器无法正常使用,因为Static资源不经过编译,但是编辑器里面的controller.jsp需要预编译处理的。
③ 在页面上引入css和js
④ 添加标签渲染,以及提取值和监听事件
⑤ 一切准备就绪,下边就是请求后台逻辑了
/**
* ueditor上传图片和涂鸦重写
*
* @param upfile
* @param request
* @return
* @throws IOException
/
@RequestMapping("/uploadImage")
@ResponseBody//这里upfile是config.json中图片提交的表单名称
public FileResult uploadImage(@RequestParam(“upfile”) CommonsMultipartFile upfile, HttpServletRequest request,HttpServletResponse resopnse) throws IOException {
String newsPath = request.getSession().getServletContext().getRealPath(“news”);
//文件原名称
String fileName=upfile.getOriginalFilename();
String imgPath = null;
//文件后缀名
String nowName= DateUtil.format(new Date(), “yyyyMMdd”) + “_” + new Date().getTime() + “.jpg”;
/ 验证长度是否正确 */
Long maxLength = ImageConfig.getMaxLength();
long size = upfile.getSize();
if(!upfile.isEmpty()) {
File fileDir = new File(newsPath);
if (!fileDir.exists()) {
fileDir.setWritable(true);
fileDir.mkdirs();
}
//接收文件流
try {
// 保存图片文件
File tempFile = new File(newsPath,fileName);
File targetFile = new File(newsPath,nowName);
upfile.transferTo(tempFile);
upfile.transferTo(targetFile);
// 将targetFile 上传到ftp服务器
List list = new ArrayList();
if(maxLength < size/1024) {
Thumbnails.of(tempFile)
.scale(0.9)
.outputQuality(0.9)
.toFile(targetFile);
}
list.add(targetFile);
boolean flag = FTPUtil.uploadFile(list, ImageConfig.getImageRootPath() + ImageConfig.getDefaultImagePath());
//上传成功后,删除upload 下的文件
if(flag){
targetFile.delete();
tempFile.delete();
}
} catch (IOException e) {
logger.error(“上传文件异常”, e);
}
imgPath = ImageConfig.getDefaultImagePath() + “/” + nowName;
}
FileResult fileResult = new FileResult();
//返回结果信息(UEditor需要)
//是否上传成功
fileResult.setState(“SUCCESS”);
//现在文件名称
fileResult.setTitle(nowName);
//文件原名称
fileResult.setOriginal(fileName);
//文件类型 后缀名
fileResult.setType(“jpg”);
//文件路径
fileResult.setUrl(DomainContext.getNewsResource()+"/"+imgPath);
//文件大小(字节数)
fileResult.setSize(upfile.getSize()+"");
String result = JSONObject.toJSONString(fileResult);
resopnse.getWriter().write(result);
return null;
}
⑥ 涉及的方法说明
Ftp工具类
public static boolean uploadFile(List fileList,String path) throws IOException {
FTPUtil ftpUtil = new FTPUtil(ftpIp,21,ftpUser,ftpPass);
logger.info(“开始连接ftp服务器”);
boolean result = ftpUtil.uploadFile(path,fileList);
logger.info(“开始连接ftp服务器,结束上传,上传结果:{}”,result);
return result;
}
private boolean uploadFile(String remotePath,List<File> fileList) throws IOException {
boolean uploaded = true;
FileInputStream fis = null;
//连接FTP服务器
if(connectServer(this.ip,this.port,this.user,this.pwd)){
try {
createDir(remotePath);
// ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding(“UTF-8”);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
for(File fileItem : fileList){
fis = new FileInputStream(fileItem);
ftpClient.storeFile(fileItem.getName(),fis);
}
} catch (IOException e) {
logger.error("上传文件异常",e);
uploaded = false;
e.printStackTrace();
} finally {
fis.close();
ftpClient.disconnect();
}
}else{
uploaded = false;
}
return uploaded;
}
private boolean connectServer(String ip,int port,String user,String pwd){
boolean isSuccess = false;
ftpClient = new FTPClient();
try {
ftpClient.connect(ip);
isSuccess = ftp,Client.login(user,pwd);
} catch (IOException e) {
logger.error("连接FTP服务器异常",e);
}
return isSuccess;
}
/**
* 创建目录(有则切换目录,没有则创建目录)
* @param dir
* @return
*/
public boolean createDir(String dir){
if(StringExtend.isNullOrEmpty(dir))
return true;
String d;
try {
//目录编码,解决中文路径问题
d = new String(dir.toString().getBytes("GBK"),"iso-8859-1");
//尝试切入目录
if(ftpClient.changeWorkingDirectory(d))
return true;
dir = StringExtend.trimStart(dir, "/");
dir = StringExtend.trimEnd(dir, "/");
String[] arr = dir.split("/");
StringBuffer sbfDir=new StringBuffer();
//循环生成子目录
for(String s : arr){
sbfDir.append("/");
sbfDir.append(s);
//目录编码,解决中文路径问题
d = new String(sbfDir.toString().getBytes("GBK"),"iso-8859-1");
//尝试切入目录
if(ftpClient.changeWorkingDirectory(d))
continue;
if(!ftpClient.makeDirectory(d)){
System.out.println("[失败]ftp创建目录:"+sbfDir.toString());
return false;
}
System.out.println("[成功]创建ftp目录:"+sbfDir.toString());
}
//将目录切换至指定路径
return ftpClient.changeWorkingDirectory(d);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
到此,基本上大功告成了。
自己配置下Frp的相关账号密码和IP信息。祝君好运!