本文作者:ITDragon龍原文:http://cnblogs.com/itdragon/p/7864916.html
本文作者:ITDragon龍
本章內容通過Nginx 和 FTP 搭建圖片伺服器。在學習本章內容前,請確保您的Linux 系統已經安裝了Nginx和Vsftpd。
Nginx 安裝 http://www.cnblogs.com/itdragon/p/7850985.htmlVsftpd 安裝http://www.cnblogs.com/itdragon/p/7857649.html
Nginx 安裝
http://www.cnblogs.com/itdragon/p/7850985.html
效果圖:
項目結構:
首先要攻破核心技術。通過單元測試實現圖片上傳的功能。
package com.itdragon.test;
import java.io.File; import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.junit.Test;
public class PictureFTPTest {
// 測試 ftp 上傳圖片功能 @Test public void testFtpClient() throws Exception { // 1. 創建一個FtpClient對象 FTPClient ftpClient = new FTPClient(); // 2. 創建 ftp 連接 ftpClient.connect("192.168.0.11", 21); // 3. 登錄 ftp 伺服器 ftpClient.login("ftpuser", "root"); // 4. 讀取本地文件 FileInputStream inputStream = new FileInputStream(new File("F:hello.png")); // 5. 設置上傳的路徑 ftpClient.changeWorkingDirectory("/usr/local/nginx/html/images"); // 6. 修改上傳文件的格式為二進位 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 7. 伺服器存儲文件,第一個參數是存儲在伺服器的文件名,第二個參數是文件流 ftpClient.storeFile("hello.jpg", inputStream); // 8. 關閉連接 ftpClient.logout();
}
說明:這裡的ip地址,埠,ftp用戶名,密碼,本地文件路徑,以及Nginx伺服器圖片路徑等,這些字元串參數都要根據自己實際設置的來填寫的。如果你的Nginx和Vsftpd安裝是按照我提供的鏈接來做的。那你只需要改ip地址即可。
搭建Maven的Web 項目,之前有寫過。這裡就不過多描述。
首先是 Maven 的核心文件 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.itdragon.upload</groupId> <artifactId>pictrue-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging>
<!-- 集中定義依賴版本號 --> <properties> <junit.version>4.12</junit.version> <spring.version>4.1.3.RELEASE</spring.version> <mybatis.version>3.2.8</mybatis.version> <mybatis.spring.version>1.2.2</mybatis.spring.version> <mybatis.paginator.version>1.2.15</mybatis.paginator.version> <mysql.version>5.1.6</mysql.version> <slf4j.version>1.6.4</slf4j.version> <jackson.version>2.4.2</jackson.version> <druid.version>1.0.9</druid.version> <httpclient.version>4.3.5</httpclient.version> <jstl.version>1.2</jstl.version> <servlet-api.version>2.5</servlet-api.version> <jsp-api.version>2.0</jsp-api.version> <joda-time.version>2.5</joda-time.version> <commons-lang3.version>3.3.2</commons-lang3.version> <commons-io.version>1.3.2</commons-io.version> <commons-net.version>3.3</commons-net.version> <pagehelper.version>3.4.2</pagehelper.version> <jsqlparser.version>0.9.1</jsqlparser.version> <commons-fileupload.version>1.3.1</commons-fileupload.version> <jedis.version>2.7.2</jedis.version> <solrj.version>4.10.3</solrj.version> </properties> <dependencies> <!-- 時間操作組件 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>${joda-time.version}</version> </dependency> <!-- Apache工具組件 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>${commons-net.version}</version> </dependency> <!-- Jackson Json處理工具包 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <!-- httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>${httpclient.version}</version> </dependency> <!-- 單元測試 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- 日誌處理 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <dependency> <groupId>com.github.miemiedev</groupId> <artifactId>mybatis-paginator</artifactId> <version>${mybatis.paginator.version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- MySql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- 連接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <!-- JSP相關 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>${servlet-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>${jsp-api.version}</version> <scope>provided</scope> </dependency> <!-- 文件上傳組件 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <!-- Redis客戶端 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency> <!-- solr客戶端 --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>${solrj.version}</version> </dependency> </dependencies>
<build> <finalName>${project.artifactId}</finalName> <plugins> <!-- 資源文件拷貝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java編譯插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
說明:和文件上傳有直接關係的是:
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> </dependency>
然後是 Web 項目的核心文件 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="taotao" version="2.5"> <display-name>pictrue-service</display-name> <!-- 載入spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 解決post亂碼 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- springmvc的前端控制器 --> <servlet> <servlet-name>pictrue-service</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>pictrue-service</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
再是 SpringMVC 配置文件 springmvc.xml,需要添加文件上傳解析器
<!-- 定義文件上傳解析器 --> <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>
最後是 Ftp 配置文件 resource.properties
FTP_ADDRESS=192.168.0.11 FTP_PORT=21 FTP_USERNAME=ftpuser FTP_PASSWORD=root FTP_BASE_PATH=/usr/local/nginx/html/images IMAGE_BASE_URL=http://192.168.0.11/images
上傳圖片的介面 PictureService.java
package com.itdragon.service; import java.util.Map; import org.springframework.web.multipart.MultipartFile; public interface PictureService {
/** * 上傳,批量上傳介面 * @param uploadFile * @return */ Map uploadPicture(MultipartFile uploadFile); }
上傳圖片介面實現類 PictureServiceImpl.java
package com.itdragon.service.impl;
import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map;
import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile;
import com.itdragon.service.PictureService;
@Service @SuppressWarnings({"rawtypes", "unchecked"}) public class PictureServiceImpl implements PictureService {
// 通過 Spring4 的 Value註解,獲取配置文件中的屬性值 @Value("${FTP_ADDRESS}") private String FTP_ADDRESS; // ftp 伺服器ip地址 @Value("${FTP_PORT}") private Integer FTP_PORT; // ftp 伺服器port,默認是21 @Value("${FTP_USERNAME}") private String FTP_USERNAME; // ftp 伺服器用戶名 @Value("${FTP_PASSWORD}") private String FTP_PASSWORD; // ftp 伺服器密碼 @Value("${FTP_BASE_PATH}") private String FTP_BASE_PATH; // ftp 伺服器存儲圖片的絕對路徑 @Value("${IMAGE_BASE_URL}") private String IMAGE_BASE_URL; // ftp 伺服器外網訪問圖片路徑 @Override public Map uploadPicture(MultipartFile uploadFile) { Map resultMap = new HashMap<>(); try { // 1. 取原始文件名 String oldName = uploadFile.getOriginalFilename();
// 2. ftp 伺服器的文件名 String newName = oldName; //圖片上傳 boolean result = uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, uploadFile.getInputStream(), FTP_BASE_PATH, newName); //返回結果 if(!result) { resultMap.put("error", 1); resultMap.put("message", "upload Fail"); return resultMap; } resultMap.put("error", 0); resultMap.put("url", IMAGE_BASE_URL + "/" + newName); return resultMap;
} catch (Exception e) { e.printStackTrace(); resultMap.put("error", 1); resultMap.put("message", "upload Fail"); return resultMap; } }
/** * ftp 上傳圖片方法 * @param ip ftp 伺服器ip地址 * @param port ftp 伺服器port,默認是21 * @param account ftp 伺服器用戶名 * @param passwd ftp 伺服器密碼 * @param inputStream 文件流 * @param workingDir ftp 伺服器存儲圖片的絕對路徑 * @param fileName 上傳到ftp 伺服器文件名 * @throws Exception * */ public boolean uploadFile(String ip, Integer port, String account, String passwd, InputStream inputStream, String workingDir, String fileName) throws Exception{ boolean result = false; // 1. 創建一個FtpClient對象 FTPClient ftpClient = new FTPClient(); try { // 2. 創建 ftp 連接 ftpClient.connect(ip, port); // 3. 登錄 ftp 伺服器 ftpClient.login(account, passwd); int reply = ftpClient.getReplyCode(); // 獲取連接ftp 狀態返回值 System.out.println("code : " + reply); if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); // 如果返回狀態不再 200 ~ 300 則認為連接失敗 return result; } // 4. 讀取本地文件 // FileInputStream inputStream = new FileInputStream(new File("F:hello.png")); // 5. 設置上傳的路徑 ftpClient.changeWorkingDirectory(workingDir); // 6. 修改上傳文件的格式為二進位 ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 7. 伺服器存儲文件,第一個參數是存儲在伺服器的文件名,第二個參數是文件流 if (!ftpClient.storeFile(fileName, inputStream)) { return result; } // 8. 關閉連接 inputStream.close(); ftpClient.logout(); result = true; } catch (Exception e) { e.printStackTrace(); }finally { // FIXME 聽說,項目裡面最好少用try catch 捕獲異常,這樣會導致Spring的事務回滾出問題???難道之前寫的代碼都是假代碼!!! if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException ioe) { } } } return result; }
說明:
負責頁面跳轉的 PageController.java
package com.itdragon.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping;
@Controller public class PageController {
/** * 打開首頁 */ @RequestMapping("/") public String showIndex() { return "index"; }
@RequestMapping("/{page}") public String showpage(@PathVariable String page) { System.out.println("page : " + page); return page; } }
負責圖片上傳的 PictureController.java
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.itdragon.service.PictureService;
@RestController public class PictureController {
@Autowired private PictureService pictureService;
@RequestMapping("pic/upload") public String pictureUpload(@RequestParam(value = "fileUpload") MultipartFile uploadFile) { String json = ""; try { Map result = pictureService.uploadPicture(uploadFile); // 瀏覽器擅長處理json格式的字元串,為了減少因為瀏覽器內核不同導致的bug,建議用json json = new ObjectMapper().writeValueAsString(result); } catch (JsonProcessingException e) { e.printStackTrace(); } return json; } }
負責上傳圖片的jsp頁面 pic-upload.jsp
<%@ page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>ITDragon 圖片上傳</title> </head> <link href="/js/kindeditor-4.1.10/themes/default/default.css" type="text/css" rel="stylesheet"> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" charset="utf-8" src="/js/kindeditor-4.1.10/kindeditor-all-min.js"></script> <script type="text/javascript" charset="utf-8" src="/js/kindeditor-4.1.10/lang/zh_CN.js"></script> </head> <body> <h3>測試上傳圖片功能介面的form表單</h3> <form action="pic/upload" method="post" enctype="multipart/form-data"> <input type="file" name="fileUpload" /> <input type="submit" value="上傳文件" /> </form> <hr /> <h3>借用KindEditor富文本編輯器實現批量上傳圖片</h3> <textarea id="kindEditorDesc" style="width:800px;height:300px;visibility:hidden;"></textarea> <script type="text/javascript"> $(function(){ //初始化富文本編輯器 KindEditor.create("#kindEditorDesc", { // name值,必須和Controller 的參數對應,不然會提示 400 的錯誤 filePostName : "fileUpload", // action值, uploadJson : /pic/upload, // 設置上傳類型,分別為image、flash、media、file dir : "image" }); }); </script> </body> </html>
說明:pic-upload.jsp 分為兩個部分,第一個部分是為了測試上傳圖片功能的form表單。第二個部分是為了更好的體驗上傳,批量上傳,回顯功能的KindEditer 富文本編輯器。
源碼:https://github.com/ITDragonBlog/daydayup/tree/master/Nginx
Nginx 搭建圖片伺服器到這裡就結束了,有什麼不足的地方,請賜教。如果覺得不錯,可以點個贊哦!
TAG:Java | 互聯網 | 科技 |