成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Nginx 搭建圖片服務(wù)器

jas0n / 1073人閱讀

摘要:搭建圖片服務(wù)器本章內(nèi)容通過和搭建圖片服務(wù)器。第二個(gè)部分是為了更好的體驗(yàn)上傳,批量上傳,回顯功能的富文本編輯器。總結(jié)搭建服務(wù)器的思維實(shí)現(xiàn)上傳圖片的功能上傳圖片的功能源碼搭建圖片服務(wù)器到這里就結(jié)束了,有什么不足的地方,請賜教。

Nginx 搭建圖片服務(wù)器

本章內(nèi)容通過Nginx 和 FTP 搭建圖片服務(wù)器。在學(xué)習(xí)本章內(nèi)容前,請確保您的Linux 系統(tǒng)已經(jīng)安裝了Nginx和Vsftpd。

Nginx 安裝:https://segmentfault.com/a/11...
Vsftpd 安裝:https://segmentfault.com/a/11...

本章知識(shí)點(diǎn)

效果圖:

需求:實(shí)現(xiàn)圖片的上傳和批量上傳
技術(shù):Nginx,Vsftpd,Spring,SpringMVC,KindEditor,CentOS
說明:本章節(jié)內(nèi)容主要是實(shí)現(xiàn)圖片的上傳功能。使用 KindEditer 是為了更好的演示圖片的上傳,回顯,批量效果。后臺(tái)代碼與KindEditer沒有直接關(guān)系,放心閱讀。另外源碼中有Mybatis的jar,不用理會(huì),本章內(nèi)容用不到,是為后續(xù)內(nèi)容做準(zhǔn)備!
源碼:見文章底部
場景:用戶將圖片上傳到 tomcat 服務(wù)器上,再由 tomcat 服務(wù)器通過FTP上傳到 Nginx 服務(wù)器上。

項(xiàng)目結(jié)構(gòu):

單元測試

首先要攻破核心技術(shù)。通過單元測試實(shí)現(xiàn)圖片上傳的功能。

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. 創(chuàng)建一個(gè)FtpClient對象
        FTPClient ftpClient = new FTPClient();
        // 2. 創(chuàng)建 ftp 連接
        ftpClient.connect("192.168.0.11", 21);
        // 3. 登錄 ftp 服務(wù)器
        ftpClient.login("ftpuser", "root");
        // 4. 讀取本地文件
        FileInputStream inputStream = new FileInputStream(new File("F:hello.png"));
        // 5. 設(shè)置上傳的路徑
        ftpClient.changeWorkingDirectory("/usr/local/nginx/html/images");
        // 6. 修改上傳文件的格式為二進(jìn)制
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        // 7. 服務(wù)器存儲(chǔ)文件,第一個(gè)參數(shù)是存儲(chǔ)在服務(wù)器的文件名,第二個(gè)參數(shù)是文件流
        ftpClient.storeFile("hello.jpg", inputStream);
        // 8. 關(guān)閉連接
        ftpClient.logout();
        
    }
    
}

說明:這里的ip地址,端口,ftp用戶名,密碼,本地文件路徑,以及Nginx服務(wù)器圖片路徑等,這些字符串參數(shù)都要根據(jù)自己實(shí)際設(shè)置的來填寫的。如果你的Nginx和Vsftpd安裝是按照我提供的鏈接來做的。那你只需要改ip地址即可。

Maven 的Web 項(xiàng)目

搭建Maven的Web 項(xiàng)目,之前有寫過。這里就不過多描述。

項(xiàng)目核心配置文件

首先是 Maven 的核心文件 pom.xml


    4.0.0
    com.itdragon.upload
    pictrue-service
    0.0.1-SNAPSHOT
    war

    
    
        4.12
        4.1.3.RELEASE
        3.2.8
        1.2.2
        1.2.15
        5.1.6
        1.6.4
        2.4.2
        1.0.9
        4.3.5
        1.2
        2.5
        2.0
        2.5
        3.3.2
        1.3.2
        3.3
        3.4.2
        0.9.1
        1.3.1
        2.7.2
        4.10.3
    
    
        
        
            joda-time
            joda-time
            ${joda-time.version}
        
        
        
            org.apache.commons
            commons-lang3
            ${commons-lang3.version}
        
        
            org.apache.commons
            commons-io
            ${commons-io.version}
        
        
            commons-net
            commons-net
            ${commons-net.version}
        
        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.version}
        
        
        
            org.apache.httpcomponents
            httpclient
            ${httpclient.version}
        
        
        
            junit
            junit
            ${junit.version}
            test
        
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
        
        
            org.mybatis
            mybatis
            ${mybatis.version}
        
        
            org.mybatis
            mybatis-spring
            ${mybatis.spring.version}
        
        
            com.github.miemiedev
            mybatis-paginator
            ${mybatis.paginator.version}
        
        
            com.github.pagehelper
            pagehelper
            ${pagehelper.version}
        
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
        
            com.alibaba
            druid
            ${druid.version}
        
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        
        
            org.springframework
            spring-aspects
            ${spring.version}
        
        
        
            jstl
            jstl
            ${jstl.version}
        
        
            javax.servlet
            servlet-api
            ${servlet-api.version}
            provided
        
        
            javax.servlet
            jsp-api
            ${jsp-api.version}
            provided
        
        
        
            commons-fileupload
            commons-fileupload
            ${commons-fileupload.version}
        
        
        
            redis.clients
            jedis
            ${jedis.version}
        
        
        
            org.apache.solr
            solr-solrj
            ${solrj.version}
        
    

    
        ${project.artifactId}
        
            
            
                org.apache.maven.plugins
                maven-resources-plugin
                2.7
                
                    UTF-8
                
            
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.2
                
                    1.7
                    1.7
                    UTF-8
                
            
        
        
            
                
                
                    org.apache.tomcat.maven
                    tomcat7-maven-plugin
                    2.2
                
            
        
    

說明:和文件上傳有直接關(guān)系的是:


            commons-fileupload
            commons-fileupload
        

然后是 Web 項(xiàng)目的核心文件 web.xml



    pictrue-service
    
    
        contextConfigLocation
        classpath:spring/applicationContext-*.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        CharacterEncodingFilter
        /*
    
    
    
        pictrue-service
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring/springmvc.xml
        
        1
    
    
        pictrue-service
        /
    

再是 SpringMVC 配置文件 springmvc.xml,需要添加文件上傳解析器


    
        
        
        
        
    

最后是 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
Service 層

上傳圖片的接口 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);
}

上傳圖片接口實(shí)現(xiàn)類 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 服務(wù)器ip地址
    @Value("${FTP_PORT}")
    private Integer FTP_PORT;        // ftp 服務(wù)器port,默認(rèn)是21
    @Value("${FTP_USERNAME}")
    private String FTP_USERNAME;    // ftp 服務(wù)器用戶名
    @Value("${FTP_PASSWORD}")
    private String FTP_PASSWORD;    // ftp 服務(wù)器密碼
    @Value("${FTP_BASE_PATH}")
    private String FTP_BASE_PATH;    // ftp 服務(wù)器存儲(chǔ)圖片的絕對路徑
    @Value("${IMAGE_BASE_URL}")
    private String IMAGE_BASE_URL;    // ftp 服務(wù)器外網(wǎng)訪問圖片路徑

    @Override
    public Map uploadPicture(MultipartFile uploadFile) {
        Map resultMap = new HashMap<>();
        try {
            // 1. 取原始文件名
            String oldName = uploadFile.getOriginalFilename();
            
            // 2. ftp 服務(wù)器的文件名
            String newName = oldName;
            //圖片上傳
            boolean result = uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, 
                    uploadFile.getInputStream(), FTP_BASE_PATH, newName);
            //返回結(jié)果
            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 服務(wù)器ip地址
     * @param port             ftp 服務(wù)器port,默認(rèn)是21
     * @param account         ftp 服務(wù)器用戶名
     * @param passwd         ftp 服務(wù)器密碼
     * @param inputStream    文件流
     * @param workingDir     ftp 服務(wù)器存儲(chǔ)圖片的絕對路徑
     * @param fileName      上傳到ftp 服務(wù)器文件名
     * @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. 創(chuàng)建一個(gè)FtpClient對象
        FTPClient ftpClient = new FTPClient();
        try {
            // 2. 創(chuàng)建 ftp 連接
            ftpClient.connect(ip, port);
            // 3. 登錄 ftp 服務(wù)器
            ftpClient.login(account, passwd);
            int reply = ftpClient.getReplyCode(); // 獲取連接ftp 狀態(tài)返回值
            System.out.println("code : " + reply);
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect(); // 如果返回狀態(tài)不再 200 ~ 300 則認(rèn)為連接失敗
                return result;
            }
            // 4. 讀取本地文件
//            FileInputStream inputStream = new FileInputStream(new File("F:hello.png"));
            // 5. 設(shè)置上傳的路徑
            ftpClient.changeWorkingDirectory(workingDir);
            // 6. 修改上傳文件的格式為二進(jìn)制
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            // 7. 服務(wù)器存儲(chǔ)文件,第一個(gè)參數(shù)是存儲(chǔ)在服務(wù)器的文件名,第二個(gè)參數(shù)是文件流
            if (!ftpClient.storeFile(fileName, inputStream)) {
                return result;
            }
            // 8. 關(guān)閉連接
            inputStream.close();
            ftpClient.logout();
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // FIXME 聽說,項(xiàng)目里面最好少用try catch 捕獲異常,這樣會(huì)導(dǎo)致Spring的事務(wù)回滾出問題???難道之前寫的代碼都是假代碼!?。?            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return result;
    }

}

說明:
① @Value 注解是Spring4 中提供的,@Value("${XXX}")
② 返回值是一個(gè)Map,并且key有error,url,message。這是根據(jù)KindEditer的語法要求來的。詳情見鏈接。http://kindeditor.net/docs/up...

Controller 層

負(fù)責(zé)頁面跳轉(zhuǎn)的 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;
    }
}

負(fù)責(zé)圖片上傳的 PictureController.java

package com.itdragon.controller;

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格式的字符串,為了減少因?yàn)闉g覽器內(nèi)核不同導(dǎo)致的bug,建議用json
            json = new ObjectMapper().writeValueAsString(result);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }
}

說明:
① @RestController 也是Spring4 提供的,是 @Controller + @ResponseBody 的組合注解。
② Controller層的返回值是一個(gè)json格式的字符串。是考慮到瀏覽器對json解析兼容性比較好。

Views視圖層

負(fù)責(zé)上傳圖片的jsp頁面 pic-upload.jsp

<%@ page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8"%>




ITDragon 圖片上傳

    
     
    
    
  
  
    

測試上傳圖片功能接口的form表單


借用KindEditor富文本編輯器實(shí)現(xiàn)批量上傳圖片

說明:pic-upload.jsp 分為兩個(gè)部分,第一個(gè)部分是為了測試上傳圖片功能的form表單。第二個(gè)部分是為了更好的體驗(yàn)上傳,批量上傳,回顯功能的KindEditer 富文本編輯器。

總結(jié)

Nginx 搭建服務(wù)器的思維

Java實(shí)現(xiàn) Ftp上傳圖片的功能

KindEditer 上傳圖片的功能

源碼:https://github.com/ITDragonBl...

Nginx 搭建圖片服務(wù)器到這里就結(jié)束了,有什么不足的地方,請賜教。如果覺得不錯(cuò),可以點(diǎn)個(gè)贊哦!

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/39693.html

相關(guān)文章

  • 阿里云搭建圖片務(wù)器圖片資源務(wù)器搭建

    摘要:阿里云搭建圖片服務(wù)器,圖片資源服務(wù)器搭建背景我第一次搭建圖片服務(wù)器是在我的虛擬機(jī)服務(wù)器下,后來虛擬機(jī)崩潰了,就在阿里云買了一臺(tái)輕量級服務(wù)器,然后安裝了圖片服務(wù)器。阿里云搭建圖片服務(wù)器,圖片資源服務(wù)器搭建背景我第一次搭建圖片服務(wù)器是在我的虛擬機(jī)服務(wù)器下,后來虛擬機(jī)崩潰了,就在阿里云買了一臺(tái)輕量級服務(wù)器,然后安裝了圖片服務(wù)器。不過我當(dāng)時(shí)并沒有寫一個(gè)文檔進(jìn)行記錄,直到我后來再次需要一臺(tái)圖片服務(wù)器,...

    techstay 評論0 收藏0
  • 阿里云搭建圖片務(wù)器,圖片資源務(wù)器搭建

    摘要:背景我第一次搭建圖片服務(wù)器是在我的虛擬機(jī)服務(wù)器下,后來虛擬機(jī)崩潰了,就在阿里云買了一臺(tái)輕量級服務(wù)器,然后安裝了圖片服務(wù)器。當(dāng)執(zhí)行完上面的命令之后,我們需要上傳安裝包到阿里云服務(wù)器,然后在阿里云創(chuàng)建一個(gè)目錄用來安裝。背景 我第一次搭建圖片服務(wù)器是在我的虛擬機(jī)服務(wù)器下,后來虛擬機(jī)崩潰了,就在阿里云買了一臺(tái)輕量級服務(wù)器,然后安裝了圖片服務(wù)器。不過我當(dāng)時(shí)并沒有寫一個(gè)文檔進(jìn)行記錄,直到我后來再次需要...

    20171112 評論0 收藏0
  • 圖片務(wù)器(nginx+vxftpd)的搭建

    摘要:一搭建圖片服務(wù)器在集群中,需要一個(gè)圖片服務(wù)器來統(tǒng)一存放讀取圖片,這里使用服務(wù)器來實(shí)現(xiàn)圖片的上傳用服務(wù)器實(shí)現(xiàn)圖片的訪問搭建服務(wù)器安裝環(huán)境是語言開發(fā),建議在上運(yùn)行,本教程使用作為安裝環(huán)境。內(nèi)容修改為檢查修改是否生效重啟的搭建參考了 一.搭建圖片服務(wù)器 在集群中,需要一個(gè)圖片服務(wù)器來統(tǒng)一存放/讀取圖片,這里使用FTP服務(wù)器來實(shí)現(xiàn)圖片的上傳,用nginx服務(wù)器實(shí)現(xiàn)圖片的訪問 showImg(h...

    Blackjun 評論0 收藏0
  • 搭建 Nginx 圖片務(wù)器(前臺(tái)到后臺(tái)完整流程及源代碼)

    摘要:介紹用搭建圖片服務(wù)器瀏覽器通過請求將圖片傳到服務(wù)器將圖片存儲(chǔ)后的重定向到代理到后臺(tái)服務(wù)器本人使用的后臺(tái)是后臺(tái)獲取圖片地址后存儲(chǔ)到。如若轉(zhuǎn)載,請注明出處一安裝安裝及上傳模塊安裝重定向支持和支持,如果不需要可以不安裝。 介紹: 用 Nginx 搭建圖片服務(wù)器瀏覽器通過 Ajax 請求將圖片傳到 Nginx 服務(wù)器Nginx 將圖片存儲(chǔ)后的重定向到代理到后臺(tái)服務(wù)器(本人使用的后臺(tái)是 SSM...

    張憲坤 評論0 收藏0

發(fā)表評論

0條評論

最新活動(dòng)
閱讀需要支付1元查看
<