日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用FastDFS实现图片服务器的功能【案例演示】

發(fā)布時(shí)間:2023/12/16 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用FastDFS实现图片服务器的功能【案例演示】 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • 搭建圖片服務(wù)器
    • Nginx模塊安裝(Storage)
    • Nginx安裝(Tracker)
  • 創(chuàng)建前端頁面
    • index.jsp
  • 搭建web服務(wù)
    • pom.xml
    • web.xml
    • spring-mvc.xml
    • 文件實(shí)體類
    • 控制層
    • 添加fastDFS配置文件
    • 啟動(dòng)FastDFS服務(wù),開始測試

搭建圖片服務(wù)器

Nginx模塊安裝(Storage)

# 上傳 fastdfs-nginx-module_v1.16.tar.gz 到 /opt 目錄下(壓縮包請(qǐng)自行下載)# 解壓 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個(gè)tracker配置n行) # tracker_server=10.1.220.x:22122 # url中包含group名稱 url_have_group_name=true # 指定文件存儲(chǔ)路徑(配置的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/client

Nginx安裝(Tracker)

  • 安裝Nginx:參考鏈接
  • Nginx安裝之后進(jìn)行以下操作
# 拷貝配置文件 cd /opt/FastDFS/conf cp http.conf mime.types /etc/fdfs/ # 是否覆蓋:yes # 修改nginx配置文件 vim /usr/local/nginx/conf/nginx.conf server { listen 80; server_name 10.1.220.247; #charset koi8-r; #access_log logs/host.access.log main; location /group1/M00 { root /home/fastdfs/fdfs_storage/data; ngx_fastdfs_module;} # 重啟Nginx pkill -9 nginx /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

創(chuàng)建前端頁面

index.jsp

<%--上傳文件,文件與文字相比較起來,內(nèi)容較大,必須使用post方式提交--%> <%--上傳文件,和普通文件有區(qū)別,action接收參數(shù)也會(huì)區(qū)別對(duì)待,所以聲明帶文件提交的表單為"多部件表單"--%> <form action="upload" method="post" enctype="multipart/form-data"><input type="file" name="fname"><br><button>提交</button> </form>

搭建web服務(wù)

pom.xml

<!-- 打成war包 --> <packaging>war</packaging><dependencies><!-- 因?yàn)橛衘sp頁面,所以引用servlet依賴--><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><scope>provided</scope><version>2.5</version></dependency> <!-- 頁面提交過來的請(qǐng)求,使用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服務(wù)器需要用到的IO工具--><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.3.1</version></dependency><!--用來轉(zhuǎn)換java對(duì)象和json字符串,注意,2.7以上版本必須搭配spring5.0以上--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.8</version></dependency> </dependencies><!-- 使用插件將Tomcat內(nèi)嵌到web項(xiàng)目中 --> <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>

文件實(shí)體類

public class FileSystem implements Serializable {private String fileId;private String filePath;private String fileName; }

控制層

@Controller public class FileAction {/*** @param request 多部件表單的請(qǐng)求對(duì)象* @return 上傳文件對(duì)象的json對(duì)象* * 上傳文件的流程:* 1.把文件保存到web服務(wù)器* 2.從web服務(wù)器將文件上傳到FastDFS*/@RequestMapping("upload")// MultipartHttpServletRequest:是httpservletRequest的強(qiáng)化版本,不僅可以裝文本信息,還可以裝圖片文件信息public @ResponseBody FileSystem upload(MultipartHttpServletRequest request) throws Exception {FileSystem fileSystem = new FileSystem();/* 1.把文件保存到web服務(wù)器*/// 從頁面請(qǐng)求中,獲取上傳的文件對(duì)象MultipartFile file = request.getFile("fname");// 從文件對(duì)象中獲取 文件的原始名稱String oldFileName = file.getOriginalFilename();// 通過字符串截取的方式,從文件原始名中獲取文件的后綴 1. jpgString suffix = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);// 為了避免文件因?yàn)橥采w,生成全新的文件名String newFileName = UUID.randomUUID().toString() + "." + suffix;// 創(chuàng)建web服務(wù)器保存文件的目錄(預(yù)先創(chuàng)建好D:/upload目錄,否則系統(tǒng)找不到路徑,會(huì)拋出異常)File toSaveFile = new File("D:/upload/" + newFileName);// 將路徑轉(zhuǎn)換成文件file.transferTo(toSaveFile);// 獲取服務(wù)器的絕對(duì)路徑String newFilePath = toSaveFile.getAbsolutePath();/* 2、把文件從web服務(wù)器上傳到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數(shù)據(jù)對(duì)象fileSystem.setFileId(fileId);fileSystem.setFileName(oldFileName);fileSystem.setFilePath(fileId);// 已經(jīng)上傳到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

啟動(dòng)FastDFS服務(wù),開始測試

[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

總結(jié)

以上是生活随笔為你收集整理的使用FastDFS实现图片服务器的功能【案例演示】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。