道非道 非常道

          勤思、謹言、慎行、厚積、薄發

          統計

          web

          天圓

          經濟 政治 軍事

          鍵康

          [轉]生成縮略圖

          本例子使用了jspsmart組件進行上傳,這里可以免費下載該組件www.jspsmart.com
          下載解壓后,將jar包復制到 "WEB-INF"lib 目錄后重啟服務器,jspsmart即可正常使用了

          1、uploadimage.jsp

          <%@ page contentType="text/html;charset=gb2312" language="java" import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,
          java.sql.*,com.jspsmart.upload.*,java.util.*,cn.oof.database.*,cn.oof.house.*"%>
          <%
          SmartUpload mySmartUpload =new SmartUpload();
          long file_size_max=4000000;
          String fileName2="",ext="",testvar="";
          String url="uploadfile/images/";      //應保證在根目錄中有此目錄的存在
          //初始化
          mySmartUpload.initialize(pageContext);
          //只允許上載此類文件
          try {
          mySmartUpload.setAllowedFilesList("jpg,gif");
          //上載文件
          mySmartUpload.upload();
          } catch (Exception e){
          %>
            <SCRIPT language=javascript>
            alert("只允許上傳.jpg和.gif類型圖片文件");
            window.location=''upfile.jsp'';
            </script>
          <%
          }
          try{

              com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
              if (myFile.isMissing()){%>
             <SCRIPT language=javascript>
             alert("請先選擇要上傳的文件");
             window.location=''upfile.jsp'';
             </script>
              <%}
              else{
                //String myFileName=myFile.getFileName(); //取得上載的文件的文件名
             ext= myFile.getFileExt();      //取得后綴名
             int file_size=myFile.getSize();     //取得文件的大小  
             String saveurl="";
             if(file_size<file_size_max){
              //更改文件名,取得當前上傳時間的毫秒數值
              Calendar calendar = Calendar.getInstance();
              String filename = String.valueOf(calendar.getTimeInMillis());
              saveurl=request.getRealPath("/")+url;
              saveurl+=filename+"."+ext;          //保存路徑
              myFile.saveAs(saveurl,mySmartUpload.SAVE_PHYSICAL);
              //out.print(filename);
          //-----------------------上傳完成,開始生成縮略圖-------------------------    
              java.io.File file = new java.io.File(saveurl);        //讀入剛才上傳的文件
              String newurl=request.getRealPath("/")+url+filename+"_min."+ext;  //新的縮略圖保存地址
              Image src = javax.imageio.ImageIO.read(file);                     //構造Image對象
              float tagsize=200;
              int old_w=src.getWidth(null);                                     //得到源圖寬
              int old_h=src.getHeight(null);   
              int new_w=0;
              int new_h=0;                            //得到源圖長
              int tempsize;
              float tempdouble;
              if(old_w>old_h){
               tempdouble=old_w/tagsize;
              }else{
               tempdouble=old_h/tagsize;
              }
              new_w=Math.round(old_w/tempdouble);
              new_h=Math.round(old_h/tempdouble);//計算新圖長寬
              BufferedImage tag = new BufferedImage(new_w,new_h,BufferedImage.TYPE_INT_RGB);
              tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);       //繪制縮小后的圖
              FileOutputStream newimage=new FileOutputStream(newurl);          //輸出到文件流
              JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);       
              encoder.encode(tag);                                               //近JPEG編碼
               newimage.close();    

             }
             else{
              out.print("<SCRIPT language=''javascript''>");
              out.print("alert(''上傳文件大小不能超過"+(file_size_max/1000)+"K'');");
              out.print("window.location=''upfile.jsp;''");
              out.print("</SCRIPT>");
             }
            }
          }catch (Exception e){

          e.toString();

          }
          %>

          2 upload.htm
          <html>
          <head>
          請選擇上傳的圖片
          </head>
          <body>
          <table border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td height="45" align="center" valign="middle"><form action="uploadimage.jsp" method="post" enctype="multipart/form-data" name="form1">
          請選擇上傳的圖片
              <input type="file" name="file">
          <input type="submit" name="Submit" value="上傳">
              </form></td>
            </tr>
          </table>
          </body>
          </html>

          posted on 2008-12-03 17:46 星期五 閱讀(116) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 舞钢市| 平塘县| 曲阳县| 尚志市| 金塔县| 武胜县| 承德市| 濉溪县| 崇信县| 积石山| 玉环县| 庆云县| 潮安县| 济阳县| 泗水县| 灵山县| 新和县| 永川市| 东兰县| 嘉鱼县| 庐江县| 鄱阳县| 牙克石市| 沁水县| 军事| 来凤县| 睢宁县| 周至县| 邳州市| 柞水县| 苍南县| 泽州县| 巫山县| 大埔区| 乐安县| 汉源县| 奇台县| 凤山市| 集安市| 诏安县| 连城县|