一、将FastDFS的jar包添加到工程中
由于中央仓库中没有相关的jar包,所以需要将其jar包加入本地仓库中
- 导入fastdfs_client工程: import existing projects into workspace
- Maven install
- 在web工程的pom文件中添加依赖 :
<dependency> <groupId>fastdfs_client</groupId> <artifactId>fastdfs_client</artifactId> <version>1.25</version> </dependency>
二、新建fastdfs配置文件
- 新建配置文件client.conf:
- 设置tracker服务器ip:
tracker_server=192.168.134.129:22122
三、图片上传大致步骤
public void uploadFastDFSTest() throws Exception{
//加载配置文件
ClientGlobal.init("D:\\test\\taotao-manager\\taotao-manager-web\\src\\main\\resources\\properties\\client.conf");
//创建trackerClient对象
TrackerClient trackerClient = new TrackerClient();
//获取trackerServer对象
TrackerServer trackerServer = trackerClient.getConnection();
//声明storageServer对象
StorageServer storageServer = null;
//获取storageClient对象
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//上传图片并获取返回id
String[] strings = storageClient.upload_file("D:\\picture\\test1.png", "png", null);
for(String string : strings) {
System.out.println(string);
}
}
四、springmvc中实现图片上传
- 添加jar包: Commons-io、Commons-fileupload
- 配置多媒体解析器: 在springmvc.xml中添加如下配置