图片上传FastDFS服务器

安装图片服务器FastDFS

图片上传FastDFS服务器

启动CentOS-fastDFS-189虚拟机

图片上传FastDFS服务器

选我已移动该虚拟机,只需要修改虚拟机IP、关网

选我已复制该虚拟机,需要修改虚拟机IP、关网以及网卡地址

图片上传FastDFS服务器

CentOS-fastDFS-189虚拟机的用户名:root 密码:123123

 

修改虚拟机IP、关网

图片上传FastDFS服务器

图片上传FastDFS服务器

 

vi/etc/sysconfig/network

 图片上传FastDFS服务器

init 0

secureCRT工具启动tracker、storage、nginx

ps -ef |grep tracker

ps -ef |grep storage

ps -ef |grep nginx

关闭防火墙:service iptables stop

 

FastDFS 服务器的配置:

图片上传FastDFS服务器

图片上传FastDFS服务器

图片上传FastDFS服务器

重启tracker、storage

图片上传FastDFS服务器

/usr/bin/fdfs_trackerd/etc/fdfs/tracker.conf restart

查看tracker是否重启

图片上传FastDFS服务器

重启storage

图片上传FastDFS服务器

/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

 

FastDFS-nginx-module配置:

图片上传FastDFS服务器

vim/etc/fdfs/mod_fastdfs.conf

 图片上传FastDFS服务器

 

 

Nginx中nginx.conf配置:

图片上传FastDFS服务器

 图片上传FastDFS服务器

图片上传FastDFS服务器

重启nginx查看:

图片上传FastDFS服务器

 

 

 

 

 

 

 

 

安装FastDFS 客户端

图片上传FastDFS服务器

图片上传FastDFS服务器

 图片上传FastDFS服务器

工具类上传图片:

配置tracker服务器:

图片上传FastDFS服务器

图片上传FastDFS服务器

buy-common工具工程pom中配置fastdfs客户端依赖

图片上传FastDFS服务器

图片上传FastDFS服务器

<!-- fastDFS客户端 -->

<dependency>

       <groupId>fastdfs_client</groupId>

       <artifactId>fastdfs_client</artifactId>

       <version>1.25</version>

     </dependency>

 

Web工程pom中配置文件上传组件依赖

图片上传FastDFS服务器

<!-- 文件上传组件 -->

       <dependency>

         <groupId>commons-fileupload</groupId>

         <artifactId>commons-fileupload</artifactId>

       </dependency>

 

 

 

Web工程springmvc.xml配置文件上传的相关配置

图片上传FastDFS服务器

<!-- 文件上传的相关配置 -->

         <bean id="multipartResolver"

                   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

                   <!-- 设定默认编码 -->

                   <property name="defaultEncoding" value="UTF-8"></property>

                   <!-- 设定文件上传的最大值5MB,5*1024*1024 -->

                   <property name="maxUploadSize" value="5242880"></property>

         </bean>

 

Controller层配置

图片上传FastDFS服务器

图片上传FastDFS服务器

图片上传FastDFS服务器

@Controller

public class PictuerController {

        

        

         @Value("${IMAGE_SERVER}")

         private String IMAGE_SERVER;

        

         @RequestMapping(value="/pic/upload",produces=MediaType.TEXT_PLAIN_VALUE+";charset=utf-8")

         @ResponseBody

         public String uploadFile(MultipartFile uploadFile) throws JsonProcessingException{

                   Map result = new HashMap();

                   //将图片上传到图片服务器

                   try {

                            FastDFSClient client = new FastDFSClient("classpath:conf/client.conf");

                            //获取文件后缀

                            String fileName = uploadFile.getOriginalFilename();

                            //注意不能包含 .

                            String ext = fileName.substring(fileName.lastIndexOf(".")+1);

                            //利用工具类,将图片上传到图片服务器

                            String path = client.uploadFile(uploadFile.getBytes(),ext);

                            //  group1/M00/00/00/wKgIcFrxtzSAdEybAADbmEFe3tA712.jpg

                            System.out.println(path);

                            //上传成功

                            result.put("error", 0);

                            result.put("url", IMAGE_SERVER+path);

                   } catch (Exception e) {

                            //上传失败

                            result.put("error", 1);

                            result.put("message", "上传失败!");

                            e.printStackTrace();

                   }

                   return new ObjectMapper().writeValueAsString(result);

         };

}

上传图片页面有问题及解决如图:

图片上传FastDFS服务器

 成功:

图片上传FastDFS服务器

 

断点调试设置:

图片上传FastDFS服务器

图片上传FastDFS服务器

查看fastDFS服务器中上传的图片:

先运行服务层,在运行表现层

图片上传FastDFS服务器

运行时报错如图:

图片上传FastDFS服务器

解决:1、重启storagetracker

图片上传FastDFS服务器

      2、检查storagetrackerIP是否配正确

      3storage.conftracker.confclient.confIP配置是不是在最原始的storage.conftracker.confclient.conf如图下的位置,而不是在其他位置storage.conftracker.confclient.conf的修改IP

图片上传FastDFS服务器

相关文章: