ftp局域⽹内实现上传⽂件资源共享**
第⼀步:搭建ftp服务器
对唱的歌**
1.安装ftp服务
2.添加站点
3.配置站点的⽤户名和密码
第⼆步:创建springboot项⽬整合ftp
1.添加ftpclient的依赖
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.1</version>
</dependency>
2.配置连接FTP的资源信息l
dagang:重阳节发朋友圈的精美句子
yitihua:
document:
uploadPath: C:\uploadFile
ftpIp: 192.168.0.52
ftpName: shiwen
ftpPassword: 1314521
3.编写使⽤ftp上传⽂件的controller
@RequestMapping("/search")
public class SearchController {
@Value("${dagang.yitihua.document.uploadPath}")
private String uploadPath;
@Value("${dagang.yitihua.document.ftpIp}")
private String ftpIp;
@Value("${dagang.yitihua.document.ftpName}")
小英雄故事private String ftpName;
@Value("${dagang.yitihua.document.ftpPassword}")
private String ftpPassword;
@PostMapping("/uploadFile")
public Map<String, Object> uploadFile(MultipartFile file) throws Exception{
Map<String, Object> map = new HashMap<String, Object>();
FileEntity fEntity = new FileEntity();
//获得源⽂件的名
String originalFileName = OriginalFilename();
//源⽂件后缀
String suffix = originalFileName.substring(originalFileName.lastIndexOf('.'));
//2、使⽤UUID⽣成新⽂件名
好看的史诗电影String uuid = UUID.randomUUID().toString();
fEntity.placeAll("-", ""));//String.NextKey()));
String newFileName = Id() + suffix;
fEntity.OriginalFilename());
fEntity.setUploadTime(new Date());
fEntity.setUploadBy("admin");
//String suffix = FileName().FileName().indexOf("."));
fEntity.setFinePathName(uploadPath + File.separator + Id() + suffix);
fEntity.setDocType(new DocType());
InputStream inputStream = InputStream();
//将⽂件上传⾄ftp服务器\
boolean uploadToFtp = this.uploadToFtp(newFileName,inputStream); if (uploadToFtp==true){
//⽂件上传ftp服务器成功删除本地⽂件
System.out.println("上传⾄ftp服务器成功!");
map.put("result", "success");
map.put("fileId", Id());
}else {
System.out.println("上传⾄ftp服务器失败!");
map.put("result", "fail");
}
return map;
}
private boolean uploadToFtp(String originFileName, InputStream input){ FTPClient ftpClient = new FTPClient();
try {纸嫁衣4攻略
//连接ftp服务器参数填服务器的ip
//进⾏登录参数分别为账号密码
ftpClient.login(ftpName,ftpPassword);
//创建ftp的存储路径
ftpClient.makeDirectory(uploadPath);
//ftp的物理存储路径
ftpClient.changeWorkingDirectory(uploadPath);
//设置⽂件类型为⼆进制⽂件
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//开启被动模式(按⾃⼰如何配置的ftp服务器来决定是否开启) LocalPassiveMode();
//上传⽂件参数:上传后的⽂件名,输⼊流
ftpClient.storeFile(originFileName, input);
} catch (IOException e) {
e.printStackTrace();
return false;
}
预备党员承诺书return true;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论