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

 

1.pom.xml  导入jar

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.luhuiguo/fastdfs-client -->
        <dependency>
            <groupId>com.luhuiguo</groupId>
            <artifactId>fastdfs-client</artifactId>
            <version>0.4.0</version>
        </dependency>

    </dependencies>

 

启动类

@RestController
public class UploadController {
	  static FastFileStorageClient storageClient;
	static {
		 FdfsConnectionPool pool = new FdfsConnectionPool();
		    List<String> trackers = new ArrayList<String>();
		    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页面上传

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="myfile">
<input type="submit" value="上传">
<a href="/download?path=M00/00/00/wKgBh1tsNzGAV8UWAAABJW9620Q662.txt">下载</a>
</form>
</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值