Spring Boot 操作FASTDFS分布式文件系统(maven项目)

 

1.pom.xml  导入jar


        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
    


        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.luhuiguo
            fastdfs-client
            0.4.0
        

    

 

启动类

@RestController
public class UploadController {
	  static FastFileStorageClient storageClient;
	static {
		 FdfsConnectionPool pool = new FdfsConnectionPool();
		    List trackers = new ArrayList();
		    trackers.add("192.168.1.135:22122");
		    //连接到tracker
		    TrackerConnectionManager tcm = new TrackerConnectionManager(pool, trackers);
		    TrackerClient trackerClient = new DefaultTrackerClient(tcm);

		    ConnectionManager cm = new ConnectionManager(pool);
		  storageClient = new DefaultFastFileStorageClient(trackerClient, cm);
		  
	}
	@RequestMapping("/upload")
	public String uploadfile(@RequestParam MultipartFile myfile) throws IOException {
		InputStream is=myfile.getInputStream();
		
		  StorePath returnpath=storageClient.uploadFile("group1", is, is.available(),"txt");
		  return returnpath.getFullPath();
	}
	
	
	@RequestMapping("/download")
	public void fdownload(String path,HttpServletResponse response) throws IOException {
		byte[] result=storageClient.downloadFile("group1", path);

		response.setHeader("Content-Disposition", "attachment;filename="+path);
		response.getOutputStream().write(result);
		response.getOutputStream().close();	
	}
	public void upload() throws FileNotFoundException {
           String path="c:a1.txt";
		   File file=new java.io.File(path);
		   FileInputStream fis= new FileInputStream(file);
		   StorePath returnpath=storageClient.uploadFile("group1", fis, file.length(),"txt");
		   System.out.println(returnpath);
	}
	
	
	public static void download() throws IOException    {
		byte[] downloadFile=storageClient.downloadFile("group1", "M00/00/00/wKgBh1tsAl6AYpkFAAAASNo5v_Y382.txt");
		FileOutputStream fileout=new FileOutputStream("C:/b_1.txt");
		fileout.write(downloadFile);
		fileout.close();
	}
	
	public static void main(String[] args) throws Exception {
		download();
	}

@SpringBootApplication
public class FastDfsMain {
	  public static void main(String[] args) {
		  SpringApplication.run(FastDfsMain.class, args);
	}
}

 

html页面上传





Insert title here


下载

 

你可能感兴趣的:(Spring Boot 操作FASTDFS分布式文件系统(maven项目))