Ver código fonte

fastdfs工具添加

lyr 8 anos atrás
pai
commit
3aa6f0f4f7

+ 18 - 248
patient-co-wlyy/pom.xml

@ -470,7 +470,11 @@
            <version>2.8.1</version>
        </dependency>
        <!-- TEST end -->
        <dependency>
            <groupId>org.csource</groupId>
            <artifactId>fastdfs_lib</artifactId>
            <version>1.25</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
@ -503,149 +507,6 @@
                    </compilerArguments>
                </configuration>
            </plugin>
            <!-- war打包插件, 设定war包名称不带版本号 -->
            <!--<plugin>-->
            <!--<groupId>org.apache.maven.plugins</groupId>-->
            <!--<artifactId>maven-war-plugin</artifactId>-->
            <!--<version>2.4</version>-->
            <!--<configuration>-->
            <!--<warName>${project.artifactId}</warName>-->
            <!--</configuration>-->
            <!--</plugin>-->
            <!-- test插件, 仅测试名称为*Test的类,使用支持分组测试的surefire-junit47 driver -->
            <!--<plugin>-->
            <!--<groupId>org.apache.maven.plugins</groupId>-->
            <!--<artifactId>maven-surefire-plugin</artifactId>-->
            <!--<version>2.16</version>-->
            <!--<configuration>-->
            <!--<includes>-->
            <!--<include>**/*Test.java</include>-->
            <!--</includes>-->
            <!--<argLine>-Xmx256M</argLine>-->
            <!--</configuration>-->
            <!--<dependencies>-->
            <!--<dependency>-->
            <!--<groupId>org.apache.maven.surefire</groupId>-->
            <!--<artifactId>surefire-junit47</artifactId>-->
            <!--<version>2.16</version>-->
            <!--</dependency>-->
            <!--</dependencies>-->
            <!--</plugin>-->
            <!-- 增加functional test的Source目录 -->
            <!--<plugin>-->
            <!--<groupId>org.codehaus.mojo</groupId>-->
            <!--<artifactId>build-helper-maven-plugin</artifactId>-->
            <!--<version>1.8</version>-->
            <!--<executions>-->
            <!--<execution>-->
            <!--<id>add-functional-source</id>-->
            <!--<phase>generate-sources</phase>-->
            <!--<goals>-->
            <!--<goal>add-test-source</goal>-->
            <!--</goals>-->
            <!--<configuration>-->
            <!--<sources>-->
            <!--<source>src/test/functional</source>-->
            <!--</sources>-->
            <!--</configuration>-->
            <!--</execution>-->
            <!--</executions>-->
            <!--</plugin>-->
            <!-- cobertura插件, 设置不需要计算覆盖率的类 -->
            <!--<plugin>-->
            <!--<groupId>org.codehaus.mojo</groupId>-->
            <!--<artifactId>cobertura-maven-plugin</artifactId>-->
            <!--<version>2.6</version>-->
            <!--<configuration>-->
            <!--<instrumentation>-->
            <!--<excludes>-->
            <!--<exclude>**/entity/**/*.class</exclude>-->
            <!--<exclude>**/*Controller.class</exclude>-->
            <!--</excludes>-->
            <!--</instrumentation>-->
            <!--</configuration>-->
            <!--</plugin>-->
            <!-- enforcer插件, 避免被依赖的依赖引入过期的jar�? -->
            <!--<plugin>-->
            <!--<groupId>org.apache.maven.plugins</groupId>-->
            <!--<artifactId>maven-enforcer-plugin</artifactId>-->
            <!--<version>1.3.1</version>-->
            <!--<executions>-->
            <!--<execution>-->
            <!--<id>enforce-banned-dependencies</id>-->
            <!--<goals>-->
            <!--<goal>enforce</goal>-->
            <!--</goals>-->
            <!--<configuration>-->
            <!--<rules>-->
            <!--<requireMavenVersion>-->
            <!--<version>3.0.3</version>-->
            <!--</requireMavenVersion>-->
            <!--<requireJavaVersion>-->
            <!--<version>1.6</version>-->
            <!--</requireJavaVersion>-->
            <!--<bannedDependencies>-->
            <!--<searchTransitive>true</searchTransitive>-->
            <!--<excludes>-->
            <!--<exclude>commons-logging</exclude>-->
            <!--<exclude>aspectj:aspectj*</exclude>-->
            <!--<exclude>org.springframework</exclude>-->
            <!--</excludes>-->
            <!--<includes>-->
            <!--<include>org.springframework:*:4.0.*</include>-->
            <!--</includes>-->
            <!--</bannedDependencies>-->
            <!--</rules>-->
            <!--<fail>true</fail>-->
            <!--</configuration>-->
            <!--</execution>-->
            <!--</executions>-->
            <!--</plugin>-->
            <!-- jetty插件, 设定context path与spring profile -->
            <!--<plugin>-->
                <!--<groupId>org.mortbay.jetty</groupId>-->
                <!--<artifactId>jetty-maven-plugin</artifactId>-->
                <!--<version>${jetty.version}</version>-->
                <!--<configuration>-->
                    <!--<systemProperties>-->
                        <!--<systemProperty>-->
                            <!--<name>spring.profiles.active</name>-->
                            <!--<value>development</value>-->
                        <!--</systemProperty>-->
                    <!--</systemProperties>-->
                    <!--<useTestClasspath>true</useTestClasspath>-->
                    <!--<webAppConfig>-->
                        <!--<contextPath>/${project.artifactId}</contextPath>-->
                    <!--</webAppConfig>-->
                <!--</configuration>-->
            <!--</plugin>-->
            <!-- resources插件 -->
            <!--<plugin>-->
            <!--<groupId>org.apache.maven.plugins</groupId>-->
            <!--<artifactId>maven-resources-plugin</artifactId>-->
            <!--<version>2.6</version>-->
            <!--</plugin>-->
            <!--&lt;!&ndash; install插件 &ndash;&gt;-->
            <!--<plugin>-->
            <!--<groupId>org.apache.maven.plugins</groupId>-->
            <!--<artifactId>maven-install-plugin</artifactId>-->
            <!--<version>2.5.1</version>-->
            <!--</plugin>-->
            <!--&lt;!&ndash; ant插件 &ndash;&gt;-->
            <!--<plugin>-->
            <!--<groupId>org.apache.maven.plugins</groupId>-->
            <!--<artifactId>maven-antrun-plugin</artifactId>-->
            <!--<version>1.7</version>-->
            <!--</plugin>-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
@ -664,112 +525,21 @@
                            <file>${basedir}/src/main/resources/kaptcha-2.3.2.jar</file>
                        </configuration>
                    </execution>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <configuration>
                            <groupId>org.csource</groupId>
                            <artifactId>fastdfs_lib</artifactId>
                            <version>1.25</version>
                            <packaging>jar</packaging>
                            <file>${basedir}/src/main/resources/fastdfs_lib.jar</file>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <!--<pluginManagement>-->
        <!--<plugins>-->
        <!--&lt;!&ndash;This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.&ndash;&gt;-->
        <!--<plugin>-->
        <!--<groupId>org.eclipse.m2e</groupId>-->
        <!--<artifactId>lifecycle-mapping</artifactId>-->
        <!--<version>1.0.0</version>-->
        <!--<configuration>-->
        <!--<lifecycleMappingMetadata>-->
        <!--<pluginExecutions>-->
        <!--<pluginExecution>-->
        <!--<pluginExecutionFilter>-->
        <!--<groupId>-->
        <!--org.codehaus.mojo-->
        <!--</groupId>-->
        <!--<artifactId>-->
        <!--build-helper-maven-plugin-->
        <!--</artifactId>-->
        <!--<versionRange>-->
        <!--[1.8,)-->
        <!--</versionRange>-->
        <!--<goals>-->
        <!--<goal>add-test-source</goal>-->
        <!--</goals>-->
        <!--</pluginExecutionFilter>-->
        <!--<action>-->
        <!--<ignore></ignore>-->
        <!--</action>-->
        <!--</pluginExecution>-->
        <!--</pluginExecutions>-->
        <!--</lifecycleMappingMetadata>-->
        <!--</configuration>-->
        <!--</plugin>-->
        <!--</plugins>-->
        <!--</pluginManagement>-->
    </build>
    <profiles>
        <!-- 执行functional test -->
        <profile>
            <id>functional-test</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <argLine>-Xmx256M -XX:MaxPermSize=128m</argLine>
                            <includes>
                                <include>**/*FT.java</include>
                            </includes>
                            <!-- 支持taglib tld文件查找的必要设置 -->
                            <useSystemClassLoader>false</useSystemClassLoader>
                            <!-- 将mvn命令行传入的selenium driver参数传入surefire的JVM -->
                            <systemPropertyVariables>
                                <selenium.driver>${selenium.driver}</selenium.driver>
                            </systemPropertyVariables>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <!-- 执行@Category(Smoke.class)的TestCase -->
        <profile>
            <id>run-smoke</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <groups>org.springside.modules.test.category.Smoke</groups>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <!-- 刷新开发环境数据库 -->
        <profile>
            <id>refresh-db</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <configuration>
                            <target>
                                <property file="src/main/resources/application.development.properties"/>
                                <property file="src/main/resources/application.properties"/>
                                <sql driver="${jdbc.driver}" url="${jdbc.url}" userid="${jdbc.username}"
                                     password="${jdbc.password}" onerror="continue"
                                     encoding="${project.build.sourceEncoding}">
                                    <classpath refid="maven.test.classpath"/>
                                    <transaction src="src/main/resources/sql/h2/schema.sql"/>
                                    <transaction src="src/test/resources/data/h2/import-data.sql"/>
                                </sql>
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

+ 27 - 0
patient-co-wlyy/src/main/java/com/yihu/wlyy/config/FastDFSConfig.java

@ -0,0 +1,27 @@
package com.yihu.wlyy.config;
import com.yihu.wlyy.util.fastdfs.FastDFSClientPool;
import com.yihu.wlyy.util.fastdfs.FastDFSUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * @author Sand
 * @version 1.0
 * @created 2015.11.27 16:08
 */
@Configuration
public class FastDFSConfig {
    @Bean
    public FastDFSClientPool fastDFSClientPool(){
        FastDFSClientPool clientPool = FastDFSClientPool.getInstance();
        return clientPool;
    }
    @Bean
    public FastDFSUtil fastDFSUtil(){
        FastDFSUtil util = new FastDFSUtil();
        return util;
    }
}

+ 1 - 2
patient-co-wlyy/src/main/java/com/yihu/wlyy/service/app/consult/ConsultTeamService.java

@ -912,8 +912,7 @@ public class ConsultTeamService extends ConsultService {
        if (StringUtils.isEmpty(returnJson)) {
            throw new Exception("send consult finished IM message failed");
        } else {
            JSONObject jo = new JSONObject(returnJson);
            //设置消息ID
            JSONObject jo = new JSONObject(returnJson);            //设置消息ID
            consultTeam.setEndMsgId(jo.getInt("startId"));
            consultTeam.setEndOperator(endOperator);
            consultTeam.setEndType(endType);

+ 134 - 0
patient-co-wlyy/src/main/java/com/yihu/wlyy/util/fastdfs/FastDFSClientPool.java

@ -0,0 +1,134 @@
package com.yihu.wlyy.util.fastdfs;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
 * Created by szx on 2015/9/19.
 */
public class FastDFSClientPool {
    private static volatile FastDFSClientPool pool;
    private Map<StorageClient, Boolean> map;
    private int maxPoolSize = 20;
    private int waitTime = 500;
    private int initPoolSize = 5;
    static {
        try {
            
            String basePath = FastDFSClientPool.class.getResource("/").getPath();
           
            ClientGlobal.init(FastDFSUtil.class.getResource("/config/fdfs_client.conf").getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private FastDFSClientPool() {
        init();
    }
    public static FastDFSClientPool getInstance() {
        if (pool == null) {
            synchronized (FastDFSClientPool.class) {
                if(pool == null) {
                    pool = new FastDFSClientPool();
                }
            }
        }
        System.out.println("FastDFSClientPool:"+pool.toString());
        return pool;
    }
    private void init() {
        try {
            map = new HashMap<StorageClient, Boolean>();
            //不做初始缓存
//            for (int i = 0; i < initPoolSize; i++) {
//                map.put(getNewStorageClient(), true);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public TrackerServer getTrackerServer(){
        try {
            TrackerClient tracker = new TrackerClient();
            return tracker.getConnection();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    private StorageClient getNewStorageClient() {
        try {
            TrackerClient tracker = new TrackerClient();
            TrackerServer trackerServer = tracker.getConnection();
            StorageClient client = new StorageClient(trackerServer, null);
            return client;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public synchronized StorageClient getStorageClient() {
        StorageClient client = null;
        try {
            for (Entry<StorageClient, Boolean> entry : map.entrySet()) {
                if (entry.getValue()) {
                    client = entry.getKey();
                    System.out.println("从缓存中获得fastdfs client");
                    map.put(client, false);
                    break;
                }
            }
            if (client == null) {
                if(map.size()<maxPoolSize) {
                    System.out.println("创建新fastdfs client:");
                    client = getNewStorageClient();
                    map.put(client, false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return client;
    }
    public void releaseStorageClient(StorageClient client) {
        if (client == null) {
            return;
        }
        try {
            if(map.containsKey(client)) {
                    map.put(client, true);
            } else {
                client =null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

+ 287 - 0
patient-co-wlyy/src/main/java/com/yihu/wlyy/util/fastdfs/FastDFSUtil.java

@ -0,0 +1,287 @@
package com.yihu.wlyy.util.fastdfs;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.ProtoCommon;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.TrackerServer;
/**
 * FastDFS 客户端工具.
 *
 * @author szx
 */
public class FastDFSUtil {
    public final static String GroupField = "groupName";
    public final static String RemoteFileField = "remoteFileName";
    public final static String FileIdField = "fid";
    public final static String FileUrlField = "fileUrl";
//    static TrackerClient tracker;
//    static TrackerServer trackerServer;
//    static StorageServer storageServer;
//    static StorageClient client;
//
//    static {
//        try {
//            XEnvironmentOption environmentOption = ServiceFactory.getService(Services.EnvironmentOption);
//            String basePath = FastDFSUtil.class.getResource("/").getPath();
//            String configFile = basePath + environmentOption.getOption(EnvironmentOptions.FastDFSConfig);
//
//            ClientGlobal.init(configFile);
//
//            tracker = new TrackerClient();
//            trackerServer = tracker.getConnection();
//            storageServer = null;
//            client = new StorageClient(trackerServer, storageServer);
//        } catch (FileNotFoundException e) {
//            LogService.getLogger(FastDFSUtil.class).fatal("FastDFS配置文件打开失败: " + e.getMessage());
//        } catch (IOException e) {
//            LogService.getLogger(FastDFSUtil.class).fatal("FastDFS初始化失败: " + e.getMessage());
//        } catch (MyException e) {
//            LogService.getLogger(FastDFSUtil.class).fatal("FastDFS初始化失败: " + e.getMessage());
//        }
//    }
    /**
     * 以输入流的方式上传文件
     * InputStream in = new FileInputStream("C://Desert.jpg");
     * ObjectNode msg = FileUtil.upload(in,"jpg", "沙漠");
     * in.close();
     *
     * @param in                输入流
     * @param fileExtension   文件扩展名,不要带“.”
     * @param description             文件名称(中文)
     * @return 返回值的格式如下:
     * {
     *      "groupName": "healthArchiveGroup",
     *      "remoteFileName": "/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg",
     *      "fid": "group1/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg",
     *      "fileURL": "http://172.19.103.13/healthArchiveGroup/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg"
     * }
     *
     * groupName 及 remoteFileName 可以用于查询在 fastDFS 中文件的信息,如果只是图片显示,可以忽略这两个值。
     * fid 保存了在 fastDFS 上的完整路径,为了避免将来服务器域名发生变更,最好使用本值.服务器的域名另外配置。
     * fileURL 保存了完整的 web 访问路径,为了避免将来服务器域名发生变更,最好不要直接使用本值。
     * 如果需要在下载时,可以显示原始文件名,请在访问file_url时,增加 attname 参数,如:
     *
     * http://host/healthArchiveGroup/M00/00/00/rBFuH1XdIseAUTZZAA1rIuRd3Es062.jpg?attname=a.jpg
     *
     * @throws Exception
     */
    public static ObjectNode upload(InputStream in, String fileExtension,
                                    String description) throws Exception {
        StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
        try {
            NameValuePair[] fileMetaData;
            fileMetaData = new NameValuePair[1];
            fileMetaData[0] = new NameValuePair("description", description == null ? "" : description);
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode message = objectMapper.createObjectNode();
            byte fileBuffer[] = new byte[in.available()];
            int len = 0;
            int temp = 0;                             //所有读取的内容都使用temp接收
            while ((temp = in.read()) != -1) {            //当没有读取完时,继续读取
                fileBuffer[len] = (byte) temp;
                len++;
            }
            in.close();
            TrackerServer trackerServer = FastDFSClientPool.getInstance().getTrackerServer();
            String[] results = client.upload_file(fileBuffer, fileExtension, fileMetaData);
            if (results != null) {
                String fileId;
                int ts;
                String token;
                String fileURl;
                InetSocketAddress socketAddress;
                String groupName = results[0];
                String remoteFile = results[1];
                message.put(GroupField, groupName);
                message.put(RemoteFileField, remoteFile);
                fileId = groupName + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remoteFile;
                message.put(FileIdField, fileId);
                socketAddress = trackerServer.getInetSocketAddress();
                fileURl = "http://" + socketAddress.getAddress().getHostAddress();
                if (ClientGlobal.g_tracker_http_port != 80) {
                    fileURl += ":" + ClientGlobal.g_tracker_http_port;
                }
                fileURl += "/" + fileId;
                if (ClientGlobal.g_anti_steal_token) {
                    ts = (int) (System.currentTimeMillis() / 1000);
                    token = ProtoCommon.getToken(fileId, ts, ClientGlobal.g_secret_key);
                    fileURl += "?token=" + token + "&ts=" + ts;
                }
                message.put(FileUrlField, fileURl);
                
                return message;
            } else {
                return null;
            }
        }finally {
            FastDFSClientPool.getInstance().releaseStorageClient(client);
        }
    }
    /**
     * 上传本地文件
     * ObjectNode  a = FileUtil.upload("C://Desert.jpg", "沙漠");
     * System.out.println(a.toString());
     *
     * @param fileName 本地文件的绝对路径,如 C://Desert.jpg
     * @param description    文件备注, 可以为空
     * @return {"groupName":"group1","remoteFileName":"/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg"
     * {
     *      "groupName": "healthArchiveGroup",
     *      "remoteFileName": "/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg",
     *      "fid": "group1/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg",
     *      "fileURL": "http://172.19.103.13/healthArchiveGroup/M00/00/24/rBFuH1XdQC6AP3CDAAzodQCbVVc052.jpg"
     * }
     *
     * groupName 及 remoteFileName 可以用于查询在 fastDFS 中文件的信息,如果只是图片显示,可以忽略这两个值。
     * fid 保存了在 fastDFS 上的完整路径,为了避免将来服务器域名发生变更,最好使用本值.服务器的域名另外配置。
     * fileURL 保存了完整的 web 访问路径,为了避免将来服务器域名发生变更,最好不要直接使用本值。
     * 如果需要在下载时,可以显示原始文件名,请在访问file_url时,增加 attname 参数,如:
     *
     * http://host/healthArchiveGroup/M00/00/00/rBFuH1XdIseAUTZZAA1rIuRd3Es062.jpg?attname=a.jpg
     *
     * @throws Exception
     */
    public static ObjectNode upload(String fileName, String description) throws Exception {
        StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
        try {
            NameValuePair[] meta_list;
            meta_list = new NameValuePair[1];
            meta_list[0] = new NameValuePair("description", description == null ? "" : description);
            ObjectMapper objectMapper = new ObjectMapper();
            ObjectNode message = objectMapper.createObjectNode();
            String fileExtName = "";
            if (fileName.contains(".")) {
                fileExtName = fileName.substring(fileName.lastIndexOf(".") + 1);
            } else {
                throw new RuntimeException("上传失败, 文件缺失扩展名.");
            }
            TrackerServer trackerServer = FastDFSClientPool.getInstance().getTrackerServer();
            String[] results = client.upload_file(fileName, fileExtName, meta_list);
            if (results != null) {
                String fileId;
                int ts;
                String token;
                String fileUrl;
                InetSocketAddress inetSockAddr;
                String groupName = results[0];
                String remoteFileName = results[1];
                message.put(GroupField, groupName);
                message.put(RemoteFileField, remoteFileName);
                fileId = groupName + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remoteFileName;
                message.put(FileIdField, fileId);
                inetSockAddr = trackerServer.getInetSocketAddress();
                fileUrl = "http://" + inetSockAddr.getAddress().getHostAddress();
                if (ClientGlobal.g_tracker_http_port != 80) {
                    fileUrl += ":" + ClientGlobal.g_tracker_http_port;
                }
                fileUrl += "/" + fileId;
                if (ClientGlobal.g_anti_steal_token) {
                    ts = (int) (System.currentTimeMillis() / 1000);
                    token = ProtoCommon.getToken(fileId, ts, ClientGlobal.g_secret_key);
                    fileUrl += "?token=" + token + "&ts=" + ts;
                }
                message.put(FileUrlField, fileUrl);
                
                return message;
            } else {
                return null;
            }
        }finally {
            FastDFSClientPool.getInstance().releaseStorageClient(client);
        }
    }
    /**
     * 下载文件, 返回文件字节数组.
     *
     * @param groupName      在fastdfs上的卷名
     * @param remoteFileName 在fastdfs上的路径
     * @return 文件的字节码
     * @throws Exception
     */
    public static byte[] download(String groupName, String remoteFileName) throws Exception {
        StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
        try {
            byte[] b = client.download_file(groupName, remoteFileName);
            return b;
        }
        finally {
            FastDFSClientPool.getInstance().releaseStorageClient(client);
        }
    }
    /**
     * 下载文件到本地路径上.
     *
     * @param groupName          在 fastDFS 上的卷名
     * @param remoteFileName    在 fastDFS 上的路径
     * @param localPath          本地路径
     *
     * @return 是否下载成功
     */
    public static String download(String groupName, String remoteFileName, String localPath) throws IOException, MyException {
        StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
        try {
            String localFileName = localPath + "\\" + remoteFileName.replaceAll("/", "_");
            client.download_file(groupName, remoteFileName, 0, 0, localFileName);
            return localFileName;
        }finally {
            FastDFSClientPool.getInstance().releaseStorageClient(client);
        }
    }
    /**
     * 删除文件。
     *
     * @param groupName
     * @param remoteFileName
     */
    public static void delete(String groupName, String remoteFileName) throws IOException, MyException {
        StorageClient client = FastDFSClientPool.getInstance().getStorageClient();
        try {
            client.delete_file(groupName, remoteFileName);
        }finally {
            FastDFSClientPool.getInstance().releaseStorageClient(client);
        }
    }
}

+ 9 - 0
patient-co-wlyy/src/main/resources/config/fdfs_client.conf

@ -0,0 +1,9 @@
connect_timeout = 2
network_timeout = 30
charset = ISO8859-1
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
#tracker_server = 172.19.103.13:22122
tracker_server = 172.19.103.54:22122

BIN
patient-co-wlyy/src/main/resources/fastdfs_lib.jar