springboot集成fastdfs实例

pom配置


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SPTINGCLOUDartifactId>
        <groupId>cn.psgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>SPRINGBOOT_FASTDFSartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-jpaartifactId>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>com.luhuiguogroupId>
            <artifactId>fastdfs-spring-boot-starterartifactId>
            <version>0.2.0version>
        dependency>

    dependencies>
project>

项目原图
springboot集成fastdfs实例_第1张图片application.yml配置

server:
  port: 8888

fdfs:
  connect-timeout: 10000(设置连接超时时间)
  so-timeout: 3000(设置请求超时时间)
  tracker-list: #TrackerList参数,支持多个
    - 192.168.87.129:22122
spring:
  datasource:
    url: jdbc:mysql://localhost/unit02
    password: ps123456
    driver-class-name: com.mysql.jdbc.Driver
    username: root
http:
  multipart:
    max-file-size: 25698000(配置上传的最大字节数)

在unit02数据库创建一个myfile表
在这里插入图片描述

控制层

package cn.ps;
import com.luhuiguo.fastdfs.domain.StorePath;
import com.luhuiguo.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
public class UploadController {

 @Autowired
 protected FastFileStorageClient storageClient;

 @Autowired
 JdbcTemplate jdbcTemplate;

 /**
  *
  * @param myFile 从浏览器提交过来
  * @return
  * @throws IOException
  */
 @PostMapping("/fupload")
 public  String upload(@RequestParam("myFile")MultipartFile myFile) throws IOException {
     String extName= FilenameUtils.getExtension(myFile.getOriginalFilename());
     StorePath sp=storageClient.uploadFile("group1",myFile.getInputStream(),myFile.getSize(),extName);
     String sql="insert into  myfile(filename,groupname,fileipath) values(?,?,?)";
     jdbcTemplate.update(sql,myFile.getOriginalFilename(),sp.getGroup(),sp.getPath());
     return sp.getFullPath();
 }
 @GetMapping("/fdownload/{id}")
 public void  download(@PathVariable String id, HttpServletResponse response) throws IOException {
     List list =jdbcTemplate.query("select *from  myfile where fileid="+id,new ColumnMapRowMapper());

     Map map =(Map)list.get(0);
     String fileName= URLEncoder.encode(map.get("filename").toString(),"UTF-8");
     String groupName=map.get("groupname").toString();
     String fileiPath=map.get("fileipath").toString();

     //告诉浏览器 下载的文件名
     response.setHeader("Content-Disposition","attachment; filename="+fileName+"");
     //将文件的内容输出到浏览器中
    byte[] bt= storageClient.downloadFile(groupName,fileiPath);
    response.getOutputStream().write(bt);
 }

}

运行的main方法


package cn.ps;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

一个简单的上传文件页面


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <form method="post" enctype="multipart/form-data" action="fupload">
        文件<input type="file" name="myFile"/>
        <input type="submit" value="上传"/>

    form>
body>
html>

一个简单的下载文件页面


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
    <a href="fdownload/1">下载a>
body>
html>

你可能感兴趣的:(fastdfs)