Spring Boot操作FAstdFS分散ファイルシステム(maven項目)

3249 ワード


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


ダウンロード