程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java完成上傳圖片停止切割的辦法

java完成上傳圖片停止切割的辦法

編輯:關於JAVA

java完成上傳圖片停止切割的辦法。本站提示廣大學習愛好者:(java完成上傳圖片停止切割的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成上傳圖片停止切割的辦法正文


本文實例講述了java完成上傳圖片停止切割的辦法。分享給年夜家供年夜家參考。詳細剖析以下:

為何我要停止上傳的圖片停止切割呢,我這個項目標圖片是部分logo,每一個部分都可以選擇分歧的logo,然則要應對閱讀器的兼容和拉伸,我選擇了把一張圖片切成左、中、右和剩下的部門,由於右邊和中變能夠會有圖案或許字所以不克不及拉伸,拉伸的只是左邊的部門,剩下的部門自順應便可以了。所以用了javax的ImageReader來操作。最初以blob類型保留數據庫中。
起首要在form表單外面寫上enctype="multipart/form-data"
<form method="post" id="mainForm" action="${ctx }/admin/department!save.action" enctype="multipart/form-data">

上面在表單中寫上上傳組件
<tr> 
 <td > 
     <strong>上傳Logo:</strong> 
     <input type="hidden" name="suffix" id="suffix" value="${depart.departmentLogo.suffix }"/> 
 </td> 
 <td > 
     <input type="file" name="logoFile" id="logoFile" onchange="checkFile();"/> 
 </td> 
</tr> 
<tr> 
 <td ></td> 
 <td > 
     <span class="red">(圖片格局:jpg,jpeg,bmp,gif,png;</span><br/> 
     <span class="red">推舉年夜小:1120×80像素)</span> 
 </td> 
</tr>

圖片檢討的checkFile()辦法用於檢討圖片能否以劃定格局開頭和能否沒有選擇
function checkFile(){ 
         
        var value = $("#logoFile").val(); 
        if(!value){ 
            alert("請選擇您要上傳的圖片!"); 
            return false; 
        }else{ 
            if(value.lastIndexOf(".jpg") != -1){ 
                $("#suffix").val("jpg"); 
                return true; 
            }else if(value.lastIndexOf(".jpeg") != -1){ 
                $("#suffix").val("jpeg"); 
                return true; 
            }else if(value.lastIndexOf(".gif") != -1){ 
                $("#suffix").val("gif"); 
                return true; 
            }else if(value.lastIndexOf(".bmp") != -1){ 
                $("#suffix").val("bmp"); 
                return true; 
            }else if(value.lastIndexOf(".png") != -1){ 
                $("#suffix").val("png"); 
                return true; 
            }else{ 
                alert("對不起,您上傳文件格局有誤,請選擇指定格局的圖片文件上傳"); 
                return false; 
            } 
        } 
}

上面就是停止後台的save操作了。
public String save() throws Exception { 
        HttpServletRequest request = ServletActionContext.getRequest() ; 
        String departId = request.getParameter("id") ; 
        String departName = request.getParameter("name") ; 
        String companyId = request.getParameter("companyId") ; 
        //圖片後綴 
        String suffix = request.getParameter("suffix"); 
        List<Menu> listMenu = new ArrayList<Menu>() ; 
        Company company = new Company() ; 
        company.setId(Long.valueOf(companyId)) ; 
        if(this.logoFile != null && departName != null && companyId != null && suffix != null){ 
            //獲得解析圖片的ImageReader 
            Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByFormatName(suffix); 
            ImageReader imageReader = imageReaders.next(); 
            //把圖片以字撙節的情勢傳入 
            InputStream logoStream = new BufferedInputStream(new FileInputStream(this.logoFile)); 
            //轉為圖片輸出流 
            ImageInputStream imageInputStream = ImageIO.createImageInputStream(logoStream); 
            imageReader.setInput(imageInputStream, true); 
            int imageWidth = imageReader.getWidth(0); 
            //固定高度80 
            int imageHeight = 80; 
            //設置左中右和剩下的寬度 
            int leftWidth = imageWidth / 2; 
            int middleWidth = (imageWidth - leftWidth) / 3; 
            int rightWidth = 5; 
            int retainWidth = imageWidth - leftWidth -middleWidth - 5; 
             
            ImageReadParam readParam = imageReader.getDefaultReadParam(); 
            BufferedImage bImage = null; 
            //裁剪左半部門 
            //依據寬和高取得矩形 
            Rectangle leftImageRectangle = new Rectangle(0, 0, leftWidth, imageHeight); 
            readParam.setSourceRegion(leftImageRectangle); 
            bImage = imageReader.read(0, readParam);  
            //字節數組輸入流 
            ByteArrayOutputStream leftByteArrayOutputStream = new ByteArrayOutputStream(); 
            ImageIO.write(bImage, suffix, leftByteArrayOutputStream); 
            //取得字節數組 
            byte[] leftImageData = leftByteArrayOutputStream.toByteArray(); 
            leftByteArrayOutputStream.close(); 
            //Hibernate創立一個blob類型 
            Blob leftBlob = Hibernate.createBlob(leftImageData, this.departmentManager.getSession()); 
             
            //裁剪中部部門 
            Rectangle middleImageRectangle = new Rectangle(leftWidth, 0 , middleWidth, imageHeight); 
            readParam.setSourceRegion(middleImageRectangle); 
            bImage = imageReader.read(0, readParam); 
             
            ByteArrayOutputStream middleArrayOutputStream = new ByteArrayOutputStream(); 
            ImageIO.write(bImage, suffix, middleArrayOutputStream); 
            byte[] middleImageData = middleArrayOutputStream.toByteArray(); 
            middleArrayOutputStream.close(); 
            Blob middleBlob = Hibernate.createBlob(middleImageData, this.departmentManager.getSession()); 
             
            //裁剪右半部門 
            Rectangle rightImageRectangle = new Rectangle(leftWidth + middleWidth, 0, rightWidth, imageHeight); 
            readParam.setSourceRegion(rightImageRectangle); 
            bImage = imageReader.read(0, readParam); 
             
            ByteArrayOutputStream rightArrayOutputStream = new ByteArrayOutputStream(); 
            ImageIO.write(bImage, suffix, rightArrayOutputStream); 
            byte[] rightImageData = rightArrayOutputStream.toByteArray(); 
            rightArrayOutputStream.close(); 
            Blob rightBlob = Hibernate.createBlob(rightImageData, this.departmentManager.getSession()); 
             
            //保存部門 
            Rectangle retainRectangle = new Rectangle(leftWidth + middleWidth + rightWidth, 0, retainWidth, imageHeight); 
            readParam.setSourceRegion(retainRectangle); 
            bImage = imageReader.read(0, readParam); 
             
            ByteArrayOutputStream retainArrayOutputStream = new ByteArrayOutputStream(); 
            ImageIO.write(bImage, suffix, retainArrayOutputStream); 
            byte[] retainImageData = retainArrayOutputStream.toByteArray(); 
            retainArrayOutputStream.close(); 
            Blob retainBlob = Hibernate.createBlob(retainImageData, this.departmentManager.getSession()); 
             
            if(!departId.equals("") && departId!=null){ 
                Department d = this.departmentManager.findById(Long.valueOf(departId)) ; 
                if(this.checkedAuthIds != null){ 
                    for(int i=0;i<checkedAuthIds.size();i++){ 
                        Menu menu = new Menu() ; 
                        menu.setId(checkedAuthIds.get(i)) ; 
                        listMenu.add(menu) ; 
                    } 
                    d.setMenus(listMenu) ; 
                } 
                d.getDepartmentLogo().setLeftPartImage(leftBlob); 
                d.getDepartmentLogo().setMiddlePartImage(middleBlob); 
                d.getDepartmentLogo().setRightPartImage(rightBlob); 
                d.getDepartmentLogo().setRetainPartImage(retainBlob); 
                d.getDepartmentLogo().setCreateTime(new Date()); 
                d.getDepartmentLogo().setSuffix(suffix); 
                d.setName(departName) ; 
                d.setParentId(0L) ; 
                d.setNodeType(1) ; 
                d.setGrade(1) ; 
                d.setCompany(company) ; 
                this.departmentManager.save(d) ; 
            }else{ 
                Integer parentNodeType = 0 ; 
                Department dd = new Department() ; 
                if(this.checkedAuthIds!=null && this.checkedAuthIds.size() != 0){ 
                    for(int i=0;i<checkedAuthIds.size();i++){ 
                        Menu menu = new Menu() ; 
                        menu.setId(checkedAuthIds.get(i)) ; 
                        listMenu.add(menu) ; 
                    } 
                    dd.setMenus(listMenu) ; 
                }else{ 
                    dd.setMenus(null) ; 
                } 
                DepartmentLogo departmentLogo = new DepartmentLogo() ; 
                departmentLogo.setCreateTime(new Date()); 
                departmentLogo.setLeftPartImage(leftBlob); 
                departmentLogo.setMiddlePartImage(middleBlob); 
                departmentLogo.setRightPartImage(rightBlob); 
                departmentLogo.setRetainPartImage(retainBlob); 
                departmentLogo.setSuffix(suffix); 
                dd.setDepartmentLogo(departmentLogo); 
                dd.getDepartmentLogo().setDepartment(dd); 
                dd.setId(this.departmentManager.findMaxId()+1) ; 
                dd.setName(departName) ; 
                dd.setParentId(0L) ; 
                dd.setNodeType(1) ; 
                dd.setGrade(1) ; 
                dd.setOrderType(0) ; 
                dd.setCompany(company) ; 
                dd.setFlag(0) ; 
                this.departmentManager.save(dd) ; 
            } 
        } 
        return "reload"; 
}

上面就是顯示了,我是用的qui,所以在top.jsp下面的css中停止靜態顯示
<style type="text/css"> 
.welcome-hide{width: 210px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;} 
#leftLogo{background: url(${ctx }/admin/department-logo!showLogo.action?position=left) no-repeat;width: ${leftWidth}px;height: 80px;} 
#middleLogo{background: url(${ctx }/admin/department-logo!showLogo.action?position=middle) no-repeat;width: ${middleWidth}px;height: 80px;} 
#rightLogo,#topTableStyle{background: url(${ctx }/admin/department-logo!showLogo.action?position=right) repeat-x;height: 80px;} 
#retainLogo{background: url(${ctx }/admin/department-logo!showLogo.action?position=retain) no-repeat;width: ${retainWidth}px;height: 80px;} 
</style>

department-logo!showLogo.action中的showLogo辦法就是加載圖片的辦法
public String showLogo() { 
         
        HttpServletRequest request = ServletActionContext.getRequest(); 
        LoginUser loginUser = (LoginUser)((SecurityContext)request.getSession(). 
                getAttribute("SPRING_SECURITY_CONTEXT")).getAuthentication().getPrincipal(); 
        List<DepartmentLogo> logos = this.logoManager.findAll(); 
        for (DepartmentLogo departmentLogo : logos) { 
            if (loginUser.getUser().getDepartment().getId().equals(departmentLogo.getDepartment().getId())) { 
                String param = request.getParameter("position"); 
                Blob blob = null; 
                if (param != null) { 
                    try { 
                        if (param.equals("left")) { 
                            blob = departmentLogo.getLeftPartImage(); 
                            imageLogo = blob.getBinaryStream(); 
                            return "showLogo"; 
                        }else if (param.equals("middle")) { 
                            blob = departmentLogo.getMiddlePartImage(); 
                            imageLogo = blob.getBinaryStream(); 
                            return "showLogo"; 
                        }else if (param.equals("right")) { 
                            blob = departmentLogo.getRightPartImage(); 
                            imageLogo = blob.getBinaryStream(); 
                            return "showLogo"; 
                        }else if (param.equals("retain")) { 
                            blob = departmentLogo.getRetainPartImage(); 
                            imageLogo = blob.getBinaryStream(); 
                            return "showLogo"; 
                        } 
                    } catch (Exception e) { 
                        e.printStackTrace(); 
                    } 
                } 
            } 
        } 
        return null; 
}

固然前往的地址應當選擇struts2中的type="stream"
@Results({ 
    @Result(name = "showLogo", type = "stream", params = {"contentType", "image/jpeg," 
            + "image/bmp,image/png,image/gif,image/jpeg",  
            "inputName", "imageLogo", "bufferSize", "4096"}) 
})

那末top.jsp中的width是怎樣獲得的呢?
其實加載菜單的時刻獲得的,上面是menu中的辦法
HttpServletRequest request = ServletActionContext.getRequest(); 
List<DepartmentLogo> logos = this.logoManager.findAll(); 
for (DepartmentLogo departmentLogo : logos) { 
    if (user.getDepartment().getId().equals(departmentLogo.getDepartment().getId())) { 
 request.setAttribute("leftWidth", ImageIO.read(departmentLogo. 
  getLeftPartImage().getBinaryStream()).getWidth()); 
 request.setAttribute("middleWidth", ImageIO.read(departmentLogo. 
  getMiddlePartImage().getBinaryStream()).getWidth()); 
 request.setAttribute("retainWidth", ImageIO.read(departmentLogo. 
  getRetainPartImage().getBinaryStream()).getWidth()); 
 break; 
    } 
}

如許就完成了一個上傳顯示功效。

這個就是我上傳的測試圖片。

願望本文所述對年夜家的Java法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved