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:\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 = 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服務器 參數填服務器的ip
            ftpClient.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;
    }
 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM