jsp 上传图片

首先配置WEB-INF
下载 jspSmartUpload.jar 并解压放入
举例路径E:\WEB-INF\classes\的下面

也可以
 将jar包复制到 \WEB-INF\lib 
目录后重启服务器,jspsmart即可正常使用了
1.upload.jsp
<%@ page 
language="java"
contentType="text/html;charset=gb2312"
import = "java.util.*"
import = "java.sql.*"
import = "java.text.*" 
import = "java.io.*"
%>

<form action="uploadimage.jsp" method="post" enctype="multipart/form-data" name="form">
 请选择上传的图片
<input type="file" name="file">
<input type="submit" name="Submit" value="上传">
</form>
2.uploadimage.jsp
<%@ page
language="java"
contentType="text/html;charset=gb2312"
import="java.io.*"
import="java.awt.Image"
import="java.awt.image.*"
import="com.sun.image.codec.jpeg.*"
import="java.sql.*"
import="com.jspsmart.upload.*"
import="java.util.*"
%>
<%
try{
    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
    if (myFile.isMissing()){%>
   <SCRIPT language=javascript>
   alert("请先选择要上传的文件");
   window.location='upload.jsp';
   </script>
    <%}
    else{
      //String myFileName=myFile.getFileName(); //取得上载的文件的文件名
   ext= myFile.getFileExt();      //取得后缀名
   int file_size=myFile.getSize();     //取得文件的大小 
   double pic_size=file_size;
   String saveurl="";
   if(file_size<file_size_max){
    //更改文件名,取得当前上传时间的毫秒数值
    Calendar calendar = Calendar.getInstance();
    String filename = String.valueOf(calendar.getTimeInMillis());
    saveurl=request.getRealPath("/")+purl;
    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='upload.jsp;'");
    out.print("</SCRIPT>");
         }
  }
}catch (Exception e){

e.toString();

}
%> 
   

你可能感兴趣的:(JavaScript,java,sql,jsp,ext)