700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > ftp局域网内实现上传文件资源共享

ftp局域网内实现上传文件资源共享

时间:2022-05-27 15:06:54

相关推荐

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的资源信息application.yml

dagang:yitihua:document:uploadPath: C:\uploadFileftpIp: 192.168.0.52ftpName: shiwenftpPassword: 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 = file.getOriginalFilename();//源文件后缀String suffix = originalFileName.substring(originalFileName.lastIndexOf('.'));//2、使用UUID生成新文件名String uuid = UUID.randomUUID().toString();fEntity.setId(uuid.replaceAll("-", ""));//String.valueOf(Snowflake.getNextKey()));String newFileName = fEntity.getId() + suffix;fEntity.setFileName(file.getOriginalFilename());fEntity.setUploadTime(new Date());fEntity.setUploadBy("admin");//String suffix = fEntity.getFileName().substring(fEntity.getFileName().indexOf("."));fEntity.setFinePathName(uploadPath + File.separator + fEntity.getId() + suffix);fEntity.setDocType(new DocType());fEntity.getDocType().setId(getDocTypeId(fEntity.getFileName()));InputStream inputStream = file.getInputStream();//将文件上传至ftp服务器\boolean uploadToFtp = this.uploadToFtp(newFileName,inputStream);if (uploadToFtp==true){//文件上传ftp服务器成功 删除本地文件System.out.println("上传至ftp服务器成功!");map.put("result", "success");map.put("fileId", fEntity.getId());}else {System.out.println("上传至ftp服务器失败!");map.put("result", "fail");}return map;}private boolean uploadToFtp(String originFileName, InputStream input){FTPClient ftpClient = new FTPClient();try {//连接ftp服务器 参数填服务器的ipftpClient.connect(ftpIp);//进行登录 参数分别为账号 密码ftpClient.login(ftpName,ftpPassword);//创建ftp的存储路径ftpClient.makeDirectory(uploadPath);//ftp的物理存储路径ftpClient.changeWorkingDirectory(uploadPath);//设置文件类型为二进制文件ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//开启被动模式(按自己如何配置的ftp服务器来决定是否开启)ftpClient.enterLocalPassiveMode();//上传文件 参数:上传后的文件名,输入流ftpClient.storeFile(originFileName, input);} catch (IOException e) {e.printStackTrace();return false;}return true;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。