jsp圖片檔案上傳代碼

來源:互聯網
上載者:User

jsp圖片檔案上傳代碼
<%@ page contentType="text/html;charset=UTF-8" 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/";      //應保證在根目錄中有此目錄的存在
//初始化
mySmartUpload.initialize(pageContext);
//只允許上傳此類檔案
try {
 mySmartUpload.setAllowedFilesList("jpg,gif,bmp");
//上傳檔案
 mySmartUpload.upload();
} catch (Exception e){
%>
  <SCRIPT language=javascript>
  alert("只允許上傳.jpg、.bmp和.gif類型圖片檔案");
  window.location='upload.htm';
  </script>
<%
}
try{

    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
    if (myFile.isMissing()){%>
   <SCRIPT language=javascript>
   alert("請先選擇要上傳的檔案");
   window.location='upload.htm';
   </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;          //儲存路徑
 String newpic=url+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+"min_"+filename+"."+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();   

  out.print("<SCRIPT language='javascript'>");
    out.print("alert('檔案上傳成功');");
 out.print("parent.document.form1.img.value = '"+newpic+"';");
    out.print("window.location='upload.htm';");
    out.print("</SCRIPT>");
   }
   else{
    out.print("<SCRIPT language='javascript'>");
    out.print("alert('上傳檔案大小不能超過"+(file_size_max/1000)+"K');");
    out.print("window.location='upload.htm';");
    out.print("</SCRIPT>");
   }
  }
}catch (Exception e){

e.toString();

}
%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.