亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

慕課網(wǎng)_《Java實(shí)現(xiàn)圖片等比例縮略圖》學(xué)習(xí)總結(jié)

raledong / 1254人閱讀

摘要:時(shí)間年月日星期六說(shuō)明本文部分內(nèi)容均來(lái)自慕課網(wǎng)??梢愿訉W⒂跇I(yè)務(wù)邏輯開(kāi)發(fā),縮短項(xiàng)目開(kāi)發(fā)周期,提高項(xiàng)目開(kāi)發(fā)速度。

時(shí)間:2017年07月15日星期六
說(shuō)明:本文部分內(nèi)容均來(lái)自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com
教學(xué)源碼:無(wú)
學(xué)習(xí)源碼:https://github.com/zccodere/s...

第一章:課程介紹 1-1 課程介紹

在用戶進(jìn)行信息概略瀏覽的時(shí)候,提供縮略圖

提升程序性能
提升程序效率

課程內(nèi)容

Java圖片等比縮略圖實(shí)現(xiàn)方式介紹
課程項(xiàng)目案例介紹
實(shí)現(xiàn)圖片等比縮略圖的生成
1-2 實(shí)現(xiàn)方式

實(shí)現(xiàn)方式

Thumbnailator類庫(kù):
    推薦使用
    size()API方法
Java AWT類庫(kù):
    根據(jù)縮略比例計(jì)算縮略圖高度和寬度
    使用Image類獲得原圖的縮放版本
    使用ImageIO類保存縮略圖
    BufferedImage類
    ImageIO類
    Graphics類
第二章:案例介紹 2-1 案例介紹

案例介紹

基于springmvc框架的Java Web應(yīng)用程序,允許上傳圖片,并生成圖片的縮略圖

效果示意圖

第三章:案例實(shí)現(xiàn) 3-1 框架搭建

個(gè)人學(xué)習(xí)時(shí),使用springboot+freemaker+mavem搭建,POM文件如下


    4.0.0

    com.myimooc
    thumbnail
    0.0.1-SNAPSHOT
    jar

    thumbnail
    http://maven.apache.org

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.1.RELEASE
         
    

    
        UTF-8
        UTF-8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
            commons-io
            commons-io
            2.5
        

        
        
            net.coobird
            thumbnailator
            0.4.8
        

    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                
            
        
    

項(xiàng)目層級(jí)結(jié)構(gòu)如下

3-2 上傳頁(yè)面開(kāi)發(fā)

代碼演示:



上傳文件




    

圖片上傳

請(qǐng)上傳圖片

3-3 控制器開(kāi)發(fā)

代碼演示:

package com.myimooc.thumbnail.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.myimooc.thumbnail.service.ThumbnailService;
import com.myimooc.thumbnail.service.UploadService;

/**
 * Thumbnail 控制器類
 * @author ZhangCheng on 2017-07-19
 *
 */
@Controller
@RequestMapping("/")
public class ThumbnailController {
    
    private static Logger logger = LoggerFactory.getLogger(ThumbnailController.class);
    
    @Autowired
    private UploadService uploadService;
    @Autowired
    private ThumbnailService thumbnailService;
    
    @PostMapping("thumbnail")
    public ModelAndView thumbnail(MultipartFile image,HttpServletRequest request){
        
        ModelAndView mav = new ModelAndView();
        
        String uploadPath = "static/images/";
        String realUploadPath = getClass().getClassLoader().getResource(uploadPath).getPath();
        
        logger.info("上傳相對(duì)目錄:{}",uploadPath);
        logger.info("上傳絕對(duì)目錄:{}",uploadPath);
        
        String imageUrl = uploadService.uploadImage(image, uploadPath, realUploadPath);
        String thumImageUrl = thumbnailService.thumbnail(image, uploadPath, realUploadPath);
        
        mav.addObject("imageURL", imageUrl);
        mav.addObject("thumImageUrl", thumImageUrl);
        mav.setViewName("thumbnail");
        return mav;
    }
    
}
3-4 圖片上傳服務(wù)類開(kāi)發(fā)

代碼演示:

package com.myimooc.thumbnail.service;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 圖片上傳服務(wù)類
 * @author ZhangCheng on 2017-07-19
 *
 */
@Service
public class UploadService {
    
    private static Logger logger = LoggerFactory.getLogger(UploadService.class);
    
    public String uploadImage(MultipartFile file,String uploadPath,String realUploadPath){
        
        logger.info("上傳的相對(duì)路徑:{}",uploadPath);
        logger.info("上傳的絕對(duì)路徑:{}",realUploadPath);
        
        String filePath = realUploadPath + file.getOriginalFilename();
        
        try {
            File targetFile=new File(filePath);
            logger.info("將圖片寫入文件:"+ filePath);
            FileUtils.writeByteArrayToFile(targetFile, file.getBytes());
        } catch (IOException e) {
            logger.info("圖片寫入失敗");
            e.printStackTrace();
        }
        return uploadPath + "/" + file.getOriginalFilename();
    }
}
3-5 縮略圖生成服務(wù)類開(kāi)發(fā)

代碼演示:

package com.myimooc.thumbnail.service;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import net.coobird.thumbnailator.Thumbnails;

/**
 * 縮略圖服務(wù)類
 * @author ZhangCheng on 2017-07-19
 *
 */
@Service
public class ThumbnailService {
    
    public static final int WIDTH = 100;
    public static final int HEIGHT = 100;
    
    public String thumbnail(MultipartFile file,String uploadPath,String realUploadPath){
        try{
            String des = realUploadPath + "/thum_" + file.getOriginalFilename();
            Thumbnails.of(file.getInputStream()).size(WIDTH, HEIGHT).toFile(des);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return uploadPath + "/thum_" + file.getOriginalFilename();
    }
}
3-6 AWT服務(wù)類講解

代碼演示:

package com.myimooc.thumbnail.service;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 縮略圖服務(wù)類
 * 
 * @author ZhangCheng on 2017-07-19
 *
 */
@Service
public class ThumbnailAWTService {

    public static final int WIDTH = 100;
    public static final int HEIGHT = 100;

    @SuppressWarnings("static-access")
    public String thumbnail(MultipartFile file, String uploadPath, String realUploadPath) {

        OutputStream os = null;

        try {
            String des = realUploadPath + "/thum_" + file.getOriginalFilename();

            os = new FileOutputStream(des);

            Image image = ImageIO.read(file.getInputStream());
            int width = image.getWidth(null);// 原圖狂寬度
            int height = image.getHeight(null);// 原圖高度

            int rateWidth = width / WIDTH;// 寬度縮略比例
            int rateHeight = height / HEIGHT;// 高度縮略比

            // 寬度縮略比例大于高度縮略比例,使用寬度縮略比例
            int rate = rateWidth > rateHeight ? rateWidth : rateHeight;

            // 計(jì)算縮略圖最終的寬度和高度
            int newWidth = width / rate;
            int newHeight = height / rate;

            BufferedImage bufferedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);

            bufferedImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, image.SCALE_SMOOTH), 0,
                    0, null);

            String imageType = file.getContentType().substring(file.getContentType().indexOf("/") + 1);
            ImageIO.write(bufferedImage, imageType, os);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return uploadPath + "/thum_" + file.getOriginalFilename();
    }
}
3-7 展示頁(yè)面開(kāi)發(fā)

代碼演示:



處理結(jié)果




    

操作結(jié)果


返回

效果演示:

啟動(dòng)項(xiàng)目,訪問(wèn)主頁(yè)

點(diǎn)擊選擇圖片

點(diǎn)擊上傳,結(jié)果顯示如下

第四章:課程總結(jié) 4-1 課程總結(jié)

通過(guò)使用開(kāi)源組件,可以很方便的實(shí)現(xiàn)自己需要的功能。可以更加專注于業(yè)務(wù)邏輯開(kāi)發(fā),縮短項(xiàng)目開(kāi)發(fā)周期,提高項(xiàng)目開(kāi)發(fā)速度。

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/67450.html

相關(guān)文章

  • 課網(wǎng)_Java驗(yàn)證碼》學(xué)習(xí)總結(jié)

    時(shí)間:2017年07月09日星期日說(shuō)明:本文部分內(nèi)容均來(lái)自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:無(wú)學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:概述 1-1 課程概述 主要內(nèi)容 驗(yàn)證碼歷史 課程內(nèi)容 不同方案對(duì)比 設(shè)計(jì)與實(shí)現(xiàn) 總結(jié) 1-2 驗(yàn)證碼歷史 驗(yàn)證碼歷史 無(wú)驗(yàn)證碼:垃圾騷擾 Luis von Ahn:Captcha 不斷...

    haitiancoder 評(píng)論0 收藏0
  • 課網(wǎng)_《模式的秘密之單模式》學(xué)習(xí)總結(jié)

    時(shí)間:2017年08月27日星期日說(shuō)明:本文部分內(nèi)容均來(lái)自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:https://github.com/zccodere/s...學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:?jiǎn)卫J胶?jiǎn)介 1-1 簡(jiǎn)介 單例模式 概念及應(yīng)用場(chǎng)合 餓漢模式 懶漢模式 餓漢模式與懶漢模式的區(qū)別 什么是設(shè)計(jì)模式 是一套被反...

    afishhhhh 評(píng)論0 收藏0
  • 課網(wǎng)_Java生成二維碼》學(xué)習(xí)總結(jié)

    摘要:時(shí)間年月日星期五說(shuō)明本文部分內(nèi)容均來(lái)自慕課網(wǎng)。線性堆疊式二維碼示意圖矩陣式二維碼在一個(gè)矩形空間通過(guò)黑白像素在矩陣中的不同分布進(jìn)行編碼。 時(shí)間:2017年06月23日星期五說(shuō)明:本文部分內(nèi)容均來(lái)自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)示例源碼:無(wú)個(gè)人學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:二維碼的概念 1-1 二維碼概述...

    QLQ 評(píng)論0 收藏0
  • 課網(wǎng)_《如何使用高德云在線制作屬于你的地學(xué)習(xí)總結(jié)

    摘要:時(shí)間年月日星期日說(shuō)明本文部分內(nèi)容均來(lái)自慕課網(wǎng)。用戶可以在服務(wù)器端調(diào)用云存儲(chǔ)云檢索從而構(gòu)建自己的存儲(chǔ)和檢索服務(wù),甚至可以制作自己的數(shù)據(jù)管理臺(tái)。 時(shí)間:2017年08月13日星期日說(shuō)明:本文部分內(nèi)容均來(lái)自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:無(wú)學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:云圖產(chǎn)品介紹 1-1 云圖產(chǎn)品介紹...

    k00baa 評(píng)論0 收藏0
  • 課網(wǎng)_《如何使用高德云在線制作屬于你的地學(xué)習(xí)總結(jié)

    摘要:時(shí)間年月日星期日說(shuō)明本文部分內(nèi)容均來(lái)自慕課網(wǎng)。用戶可以在服務(wù)器端調(diào)用云存儲(chǔ)云檢索從而構(gòu)建自己的存儲(chǔ)和檢索服務(wù),甚至可以制作自己的數(shù)據(jù)管理臺(tái)。 時(shí)間:2017年08月13日星期日說(shuō)明:本文部分內(nèi)容均來(lái)自慕課網(wǎng)。@慕課網(wǎng):http://www.imooc.com教學(xué)源碼:無(wú)學(xué)習(xí)源碼:https://github.com/zccodere/s... 第一章:云圖產(chǎn)品介紹 1-1 云圖產(chǎn)品介紹...

    afishhhhh 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

raledong

|高級(jí)講師

TA的文章

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