使用FastDFS实现图片服务器的功能【案例演示】
生活随笔
收集整理的這篇文章主要介紹了
使用FastDFS实现图片服务器的功能【案例演示】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 搭建圖片服務器
- Nginx模塊安裝(Storage)
- Nginx安裝(Tracker)
- 創(chuàng)建前端頁面
- index.jsp
- 搭建web服務
- pom.xml
- web.xml
- spring-mvc.xml
- 文件實體類
- 控制層
- 添加fastDFS配置文件
- 啟動FastDFS服務,開始測試
搭建圖片服務器
Nginx模塊安裝(Storage)
# 上傳 fastdfs-nginx-module_v1.16.tar.gz 到 /opt 目錄下(壓縮包請自行下載)# 解壓 tar -xf fastdfs-nginx-module_v1.16.tar.gz# 修改 config 文件,將文件中的 /usr/local/ 路徑改為 /usr/ vim /opt/fastdfs-nginx-module/src/config# 將 fastdfs-nginx-module/src下的 mod_fastdfs.conf 拷貝至 /etc/fdfs 下 cp mod_fastdfs.conf /etc/fdfs# 修改 /etc/fdfs/mod_fastdfs.conf vim /etc/fdfs/mod_fastdfs.confbase_path=/home/fastdfstracker_server=:22122 # (n個tracker配置n行) # tracker_server=10.1.220.x:22122 # url中包含group名稱 url_have_group_name=true # 指定文件存儲路徑(配置的store路徑) store_path0=/home/fastdfs/fdfs_storage# 將 libfdfsclient.so 拷貝至 /usr/lib 下 cp /usr/lib64/libfdfsclient.so /usr/lib/# 創(chuàng)建nginx/client目錄 mkdir -p /var/temp/nginx/clientNginx安裝(Tracker)
- 安裝Nginx:參考鏈接
- Nginx安裝之后進行以下操作
創(chuàng)建前端頁面
index.jsp
<%--上傳文件,文件與文字相比較起來,內容較大,必須使用post方式提交--%> <%--上傳文件,和普通文件有區(qū)別,action接收參數也會區(qū)別對待,所以聲明帶文件提交的表單為"多部件表單"--%> <form action="upload" method="post" enctype="multipart/form-data"><input type="file" name="fname"><br><button>提交</button> </form>搭建web服務
pom.xml
<!-- 打成war包 --> <packaging>war</packaging><dependencies><!-- 因為有jsp頁面,所以引用servlet依賴--><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><scope>provided</scope><version>2.5</version></dependency> <!-- 頁面提交過來的請求,使用springmvc來處理--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.7.RELEASE</version></dependency> <!-- java連接fastDFS的客戶端工具--><dependency><groupId>net.oschina.zcx7878</groupId><artifactId>fastdfs-client-java</artifactId><version>1.27.0.0</version></dependency> <!-- 圖片上傳到FastDFS需要用的到IO工具--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-io</artifactId><version>1.3.2</version></dependency> <!-- 圖片保存到web服務器需要用到的IO工具--><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><!--用來轉換java對象和json字符串,注意,2.7以上版本必須搭配spring5.0以上--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.8</version></dependency> </dependencies><!-- 使用插件將Tomcat內嵌到web項目中 --> <build><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><configuration><port>8001</port><path>/</path></configuration><executions><execution><phase>package</phase><goals><goal>run</goal></goals></execution></executions></plugin></plugins> </build>web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"id="WebApp_ID" version="3.1"><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/spring-mvc.xml</param-value></init-param></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping> </web-app>spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--掃描注解的包--><context:component-scan base-package="controller"/><!--掃描控制器中的注解:@Response--><mvc:annotation-driven/><!--上傳文件的解析器(規(guī)定上傳文件的大小限制)--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 上傳文件最大限制:2GB--><property name="maxUploadSize" value="2048000000"/></bean> </beans>文件實體類
public class FileSystem implements Serializable {private String fileId;private String filePath;private String fileName; }控制層
@Controller public class FileAction {/*** @param request 多部件表單的請求對象* @return 上傳文件對象的json對象* * 上傳文件的流程:* 1.把文件保存到web服務器* 2.從web服務器將文件上傳到FastDFS*/@RequestMapping("upload")// MultipartHttpServletRequest:是httpservletRequest的強化版本,不僅可以裝文本信息,還可以裝圖片文件信息public @ResponseBody FileSystem upload(MultipartHttpServletRequest request) throws Exception {FileSystem fileSystem = new FileSystem();/* 1.把文件保存到web服務器*/// 從頁面請求中,獲取上傳的文件對象MultipartFile file = request.getFile("fname");// 從文件對象中獲取 文件的原始名稱String oldFileName = file.getOriginalFilename();// 通過字符串截取的方式,從文件原始名中獲取文件的后綴 1. jpgString suffix = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);// 為了避免文件因為同名而覆蓋,生成全新的文件名String newFileName = UUID.randomUUID().toString() + "." + suffix;// 創(chuàng)建web服務器保存文件的目錄(預先創(chuàng)建好D:/upload目錄,否則系統(tǒng)找不到路徑,會拋出異常)File toSaveFile = new File("D:/upload/" + newFileName);// 將路徑轉換成文件file.transferTo(toSaveFile);// 獲取服務器的絕對路徑String newFilePath = toSaveFile.getAbsolutePath();/* 2、把文件從web服務器上傳到FastDFS*/ClientGlobal.initByProperties("config/fastdfs-client.properties");TrackerClient trackerClient = new TrackerClient();TrackerServer trackerServer = trackerClient.getConnection();StorageServer storageServer = null;StorageClient1 client = new StorageClient1(trackerServer, storageServer);NameValuePair[] list = new NameValuePair[1];list[0] = new NameValuePair("fileName", oldFileName);String fileId = client.upload_file1(newFilePath, suffix, list);trackerServer.close();// 封裝fileSystem數據對象fileSystem.setFileId(fileId);fileSystem.setFileName(oldFileName);fileSystem.setFilePath(fileId);// 已經上傳到FastDFS上,通過fileId來訪問圖片,所以fileId即為文件路徑return fileSystem;} }添加fastDFS配置文件
fastdfs-client.properties:
fastdfs.connect_timeout_in_seconds = 5 fastdfs.network_timeout_in_seconds = 30 fastdfs.charset = UTF-8 fastdfs.http_anti_steal_token = false fastdfs.http_secret_key = FastDFS1234567890 fastdfs.http_tracker_http_port = 80 fastdfs.tracker_servers = 10.1.220.247:22122啟動FastDFS服務,開始測試
[root@localhost /] /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart [root@localhost /] /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart [root@localhost /] /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf總結
以上是生活随笔為你收集整理的使用FastDFS实现图片服务器的功能【案例演示】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab遥感图像分类
- 下一篇: Delphi XE8 程序瘦身.编译后E