Java實現(xiàn)按比例縮小圖片
本文實例為大家分享了Java實現(xiàn)按比例縮小圖片的具體代碼,供大家參考,具體內(nèi)容如下
使用spring注解上傳文件@RequestParam(value="", required=true),required為true時不能傳入空值,為false時反之;UploadFile.getOriginalFilename()獲取上傳的文件的文件名;System.nanoTime()返回當(dāng)前時間的納秒,用做文件名;FileUtils.writeByteArrayToFile()上傳文件到本地目錄;使用BufferedImage將圖片加載到內(nèi)存中,然后對圖片進(jìn)行修改如大小變換、圖片變灰、設(shè)置透明等。
效果圖:
HTML:
<div class="form-group"> ?? ?<label class="col-lg-1 col-md-1 col-sm-2 col-xs-2 label-size"><span class="c-red">*</span>頭像:</label> ?? ?<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 img-padding-zero"> ?? ??? ?<div> ?? ??? ??? ?<span id="Upload_ph" class="img-hide"> ?? ??? ??? ??? ?<input type="file" title="" id="UploadFile" name="UploadFile" multiple="" class="img-input"> ?? ??? ??? ?</span> ?? ??? ??? ?<span> ?? ??? ??? ??? ?<img alt="" src="${ctx}/UserManages/reveal_photo.do?file=tree.png"> ?? ??? ??? ??? ?<a class="a-word" style="cursor:pointer;">上傳圖片</a> ?? ??? ??? ??? ?<a id="showApellation" class="a-word" style="font-size:12px;"></a> ? ? ? ? ? ?? ?? ?? ??? ??? ?</span> ?? ??? ?</div> ?? ??? ?<div> ?? ??? ??? ?<img src="${ctx}/UserManages/reveal_photo.do?file=noImg.png" class="img-padding" width="198" height="198" alt="" id="imgOperPhoto1" onload="AutoResizeImage(198, 198, this)" title="用于頭像顯示"> ?? ??? ??? ?<img src="${ctx}/UserManages/reveal_photo.do?file=noImg_tab.png" class="img-padding" width="28" height="40" alt="" id="imgOperPhoto3" title="用于列表顯示"> ?? ??? ?</div> ?? ?</div> </div>
form表單提交到j(luò)ava:
@SuppressWarnings("unused") @RequestMapping(value="/IntOpers") private void IntOper(@RequestParam(value="UploadFile", required=true) MultipartFile UploadFile, PrintWriter pw, HttpServletRequest request) throws IOException { ?? ?String StrReturn = "error", FileName = "", ext = ""; ?? ?long time = 0; ?? ?if (!UploadFile.isEmpty()) { ?? ??? ?if (UploadFile != null) { ?? ??? ??? ?String fullName = UploadFile.getOriginalFilename(); ?? ??? ??? ?ext = fullName.substring(fullName.lastIndexOf("."));//獲取擴展名稱 ?? ??? ??? ?time = System.nanoTime(); ?? ??? ??? ?FileName = time + ext;//設(shè)置文件保存的名稱 ?? ??? ??? ? ?? ??? ??? ?//原圖 ?? ??? ??? ?String upDir = "F:/java/upload-ssm";//文件上傳路徑 ?? ??? ??? ?FileUtils.writeByteArrayToFile(new File(upDir, FileName), UploadFile.getBytes()); ?? ??? ??? ?//縮小圖片 ?? ??? ??? ?Image srcFile = ImageIO.read(new File(upDir + "/" + FileName)); ?? ??? ??? ?BufferedImage tag = new BufferedImage(28, 40, BufferedImage.TYPE_INT_RGB); ?? ??? ??? ?tag.getGraphics().drawImage(srcFile, 0, 0, 28, 40, null); ? ?? ??? ??? ?String FileName_tab = time + "_tab" + ext;//設(shè)置文件保存的名稱 ?? ??? ??? ?FileOutputStream out = new FileOutputStream(upDir + "/" + FileName_tab); ?? ??? ??? ?JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); ?? ??? ??? ?JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag); ?? ??? ??? ? ?? ??? ??? ?jep.setQuality(1f, true); ?? ??? ??? ?encoder.encode(tag, jep); ?? ??? ??? ?out.close(); ?? ??? ??? ?StrReturn = "success"; ?? ??? ?} ?? ?} ?? ?pw.write(StrReturn); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中讀寫鎖ReadWriteLock的原理與應(yīng)用詳解
Java并發(fā)編程提供了讀寫鎖,主要用于讀多寫少的場景,今天我們就重點來講解讀寫鎖ReadWriteLock的原理與應(yīng)用場景,感興趣的可以了解一下2022-09-09Java中的StringTokenizer實現(xiàn)字符串切割詳解
這篇文章主要介紹了Java中的StringTokenizer實現(xiàn)字符串切割詳解,java.util工具包提供了字符串切割的工具類StringTokenizer,Spring等常見框架的字符串工具類(如Spring的StringUtils),需要的朋友可以參考下2024-01-01SpringBoot2 整合 ClickHouse數(shù)據(jù)庫案例解析
這篇文章主要介紹了SpringBoot2 整合 ClickHouse數(shù)據(jù)庫案例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10Mybatis plus 配置多數(shù)據(jù)源的實現(xiàn)示例
這篇文章主要介紹了Mybatis plus 配置多數(shù)據(jù)源的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java利用TCP協(xié)議實現(xiàn)客戶端與服務(wù)器通信(附通信源碼)
這篇文章主要介紹了Java利用TCP協(xié)議實現(xiàn)客戶端與服務(wù)器通信(附通信源碼),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java網(wǎng)絡(luò)編程TCP實現(xiàn)文件上傳功能
這篇文章主要為大家詳細(xì)介紹了Java網(wǎng)絡(luò)編程TCP實現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07SpringBoot指標(biāo)監(jiān)控功能實現(xiàn)
這篇文章主要介紹了SpringBoot指標(biāo)監(jiān)控功能實現(xiàn),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06